#!/usr/bin/perl
# fetch_notebook_data, Boone, 01/21/00
# Retrieve updates to notebook data from LISTSERV host in BRS format
#
# Description:
# End Description
#
# Modifications:
# 01/24/00 Boone      Initial coding
# End Modifications

# Libraries

	use LWP::UserAgent;
	use HTTP::Request;

# Command line

	($now, $reload, $list, $file) = @ARGV;

# Initialize

	$banner = "/bin/banner";
	$load = "/h-net/search/lsvn/workfiles/" . $now . "/load." . $file;
	$defer = "/h-net/search/lsvn/workfiles/" . $now . "/defer." . $file;
	$purge = "/h-net/search/lsvn/workfiles/" . $now . "/purge." . $file;
	$docn = "/h-net/search/lsvn/workfiles/" . $now . "/docnums." . $file;
	$log = "/h-net/search/lsvn/workfiles/" .
		$now . "/log." .
		$file;

	$brsload = "/h-net/search/brs/Bin/brsload LSVN -add -file " . $load .
		" >> " . $log;
	$brssearch = "| /h-net/search/brs/Bin/brsearch LSVN" .
		" >> " . $log;
	$brsdel = "/h-net/search/brs/Bin/brsload LSVN -delete -file " . $docn .
		" >> " . $log;
	$brsdefer = "/h-net/search/brs/Bin/brsload LSVN -add -file " . $defer .
		" >> " . $log;

	$divider = "/bin/echo ==================================================" .
		" >> " . $log;

	$LOAD = 1;
	$DEFER = 2;

# Open intermediate files

	open(LOAD, "> $load") ||
		die "unable to open $brsload: $!";
	open(DEFER, "> $defer") ||
		die "unable to open $defer: $!";
	open(PURGE, "> $purge") ||
		die "unable to open $purge: $!";

# Set up the HTTP session

	$ua = new LWP::UserAgent;
	$ua -> agent("fnb/1.0 " . $ua -> agent);
	$ua -> timeout(420);						# 420 = 7 minutes

	$req = new HTTP::Request(
		POST => 'http://h-net.msu.edu/cgi-bin/brsxfer.pl');

	$req -> content_type('application/x-www-form-urlencoded');

	$req -> content(
		"trx=getfiles&list=$list&file=$file&pw=aK7Gd9LuuP&reload=$reload");

	my $res = $ua -> request($req);

# Deal with the response

	if ($res -> is_success)
	{
		&process;
	}
	else
	{
		$code = $res -> code;
		$msg = $res -> message;
		die "unable to retrieve: $code $msg";
	}

# Close intermediate files

	close(PURGE);
	close(DEFER);
	close(LOAD);

# Load records which are "simple"

	system($divider);
	system($divider);
	print "Loading simple records...\n";
	system($divider);
	system($divider);
	system($brsload);

# Purge records which came from files that were edited

	system($divider);
	system($divider);
	print "Looking up and purging reloader records...\n";
	system($divider);
	system($divider);
	&purge;

# Load records we deferred until after the purge

	system($divider);
	system($divider);
	print "Loading deferred records...\n";
	system($divider);
	system($divider);
	system($brsdefer);

# Done

	system($divider);
	system($divider);
	print "Finished with $list\n";
	print "===========================================================\n";
	exit(0);

# Subs

	# Main result processor

	sub process
	{
		my $dest = $LOAD;

#		system($banner . " " . $file . " > " . $log);
#		system($banner . " " . $file);
		print "Writing data files...\n";

		@lines = split(/\n/, $res -> content);
		foreach $line (@lines)
		{
			if		($line =~ /wipefile: (.*)/)
			{
				$dest = $DEFER;
				print PURGE $1, "\n";
				push @purgelist, $1;
			}
			elsif	($line =~ /endwipefile:/)
			{
				$dest = $LOAD;
			}
			elsif	($line =~ /xferdate: (.*)/)
			{
			}
			elsif	($line =~ /data: (.*)/)
			{
				if ($dest == $LOAD)
				{
					print LOAD $1, "\n";
				}
				elsif ($dest == $DEFER)
				{
					print DEFER $1, "\n";
				}
			}
		}
	}

	# Process the purge list

	sub purge
	{
		open(BRS, $brssearch);
		print BRS "..set pagelen=0\n";
		print BRS "..redirect sys($docn)\n";
		foreach $purgefn (@purgelist)
		{
			print BRS "\"" . $purgefn . "\".FNAM.\n";
			print BRS "..print update,docn/all\n";
			print BRS "..s\n";
		}
		print BRS "..s\n";
		print BRS "..off\n";
		close(BRS);

		system($brsdel);
	}
