#!/usr/bin/perl
#
#NAME
#  Xcgi - download CGI scripts
#
#SYNOPSIS
# Xcgi name..
#
#DESCRIPTION
#  This is a kludge to handle the task of downloading .cgi files from
#  directories  where  they're  executable (and thus not downloadable
#  via the web).  What I do is link foo.cgi to  Xfoo.cgiX,  giving  a
#  name that won't be executed, and is thus downloadable. Then, after
#  downloading a directory (using  w3ld  for  example),  I  run  this
#  script.
#
#  What we do here is search for files with names of the form
	$pat  = '^X(.*)X$';
#  and link them to the name without the  X's.   We  also  make  them
#  executable  in the process, since that is also typically lost when
#  copying via either the web or ftp.
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>

$| = 1;
($me = $0) =~ s"^.*/"";	# Our name, minus directory.
$Vdfl = 1;
$V = $ENV{"V_$me"} || $ENV{"T_$me"} || $ENV{"D_$me"} || $Vdfl;
@ARGV = ('.') if !@ARGV;

for $x (@ARGV) {
	if ($x =~ /^X(.*)X$/) {
		&lnk($x,$1);
	} elsif (-d $x) {
		if (opendir(D,$x)) {
			@files = grep(!/^\./, readdir(D));
			close D;
			for $f (@files) {
				if ($f =~ /^X(.*)X$/) {
					&lnk("$x/$f","$x/$1");
				}
			}
		} else {
			print STDERR "$me: Can't read directory \"$x\" ($!)\n" if $V>0;
		}
	}
}

sub lnk {
	local($x,$f) = @_;
	if ($V > 1) {
		print "$me: Rm \"$f\"\n" if $V>1;
		system "Rm $f" if $V>1;
	} else {
		if (!unlink($f)) {
			print STDERR "$me: Can't unlink \"$f\" ($!)\n" if $V>0;
		}
	}
	print "$me: link(\"$x\",\"$f\")\n" if $V>1;
	if (!link($x,$f)) {
			print STDERR "$me: Can't unlink \"$x\" to \"$f\" ($!)\n" if $V>0;
	}
}
