#!/usr/bin/perl -w
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#NAME
#  last2hst - link last/fqdn to hst/fqdn
#  hrestore - link last/fqdn to hst/fqdn
#
#SYNOPSIS
#  last2hst [fqdn]..
#REQUIRES
	require "Backup.pm";
#
#DESCRIPTION
# Does some checking to be sure that files exist, and links a domain name's
# last/fqdn file to its hst/fqdn file, which is first moved to del/fqdn if
# it already exists.
#
#OPTIONS
# None yet, but there may be some soon.
#
#EXAMPLES
#
#FILES
#
#BUGS
#
#SEE ALSO
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;
$exitstat = 0;
($P = $0) =~ s".*/"";
$V = $ENV{"V_$P"} || $ENV{"D_$P"} || 1;	# Verbose level.

for $fqdn (@ARGV) {
	print STDERR "$P: --- $fqdn\n" if $V>1;
	if (-f ($lastfile = "last/$fqdn")) {
		print STDERR "$P: Found '$lastfile'\n" if $V>1;
		if (-f ($hstfile = "hst/$fqdn")) {
			print STDERR "$P: Found '$hstfile'\n" if $V>1;
			if (-f ($delfile = "del/$fqdn")) {
				print STDERR "$P: Backup '$delfile'\n" if $V>1;
				&Backup($delfile);
			}
			rename($hstfile, $delfile);	# Move ho
		}
		print STDERR "$P: $lastfile -> $hstfile\n" if $V>1;
		link($lastfile, $hstfile);
		system "tail -2 $hstfile" if $V>1;
	}
}

exit $exitstat;
