#!/usr/bin/perl
#
#NAME
#  htmlbkup - link html files into backup directories
#
#SYNOPSIS
#  htmlbkup [dir]..
#  htmlbkup `find . -type d`
#
#DESCRIPTION
#  This is a kludge to make backups of  .html  files  in  a  list  of
#  directories.   The default is the current directory.  We make sure
#  there's a .html.$host subdirectory, and link all the *.html  files
#  into  it.  Then, after we unpack a tar kit, we can check to see if
#  we need to restore any of the html files.
#
#ENVIRONMENT
#  We get the hostname preferentially from the environment  variables
#  'host'  and 'HOST', in that order.  If neither is defined, we call
#  `hostname` and `uname -n` to discover what the machine is called.
#
#TODO
#  We really should add a "recursive" feature.  What I do now is:
#    htmlbkup `find . -type d`
#
#AUTHOR
#  Copyright 2001 by John Chambers <jc@trillian.mit.edu>
#  You may use this for anything you wish, as long as you don't claim
#  that you wrote it.  If you add any cool features, let me know so I
#  can add them to my copy.

$| = 1;
push @INC, "$ENV{HOME}/pm", "$ENV{HOME}/pl", "$ENV{HOME}/sh";
require "Backup.pm";
@ARGV = ('.') unless @ARGV;
($host = $ENV{'host'} || $ENV{'HOST'} || `hostname` || `uname -n`)
	=~ s/[\s\r]+$//;

for $d (@ARGV) {
	next if $d =~ /\/\.html/;	# Don't back up .html directories
	$d =~ s'^\.\/'';
	print "$d:\n";
	($h = "$d/.html.$host") =~ s'^\.\/'';	# Path to backup directory
	for $p (glob "$d/*.html") {
		$p =~ s'^\.\/'';		# Strip away ./ directory
		($f = $p) =~ s".*/"";	# File name minus directories
		unless (-d $h) {		# Does .html backup directory exist?
			if (mkdir($h,0775)) {
				print "$h created.\n";
			} else {
				print STDERR "$h not created [$!]\n";
			}
		}
		print "$p\n" if $V>1;
		&Backup("$h/$f") if -f "$h/$f";
		system "/bin/cp -p '$p' '$h/$f'";
		if ($?) {
			print STDERR "$h/$f not copied (status $?)\n";
		} else {
			print "$p	-> $h/$f\n";
		}
#		if (link($p,"$h/$f")) {
#			print "$p	-> $h/$f\n";
#		} else {
#			print STDERR "$h/$f not linked ($!)\n";
#		}
	}
}
