#!/usr/bin/perl
#
# Archive remover for glimpseHTTP

##############################
#                            #
# no configuration is needed #
#                            #
##############################

################ SETTINGS ################
# index template
$WEBGLIMPSE_HOME = "/usr/lib/webglimpse";
$WEBGLIMPSE_LIB = "$WEBGLIMPSE_HOME/lib";

# name of madenh file
$MADENH = ".wg_madenh";
$MADENH_RE = "\.wg_madenh\$";

# to prevent deletion of siteconf stuff
$SITECONF_RE = "\.wgsiteconf\$";

# name of cron file
$CRONFILE = "wgreindex";

#added by Udi 11/20/96
#name of the central search file
$WGSEARCH = "wgindex.html";

# name of config file
$CONFIG = "archive.cfg";

# neighbor settings
$REMOTEDIR = ".remote";

# version of this program
$VERSION = "1.1b1";

##############################################

# permission information
$umaskval = umask;
$xmodval = 0777 - $umaskval;

#---------------------------------
# make my libraries more important
unshift(@INC, "$WEBGLIMPSE_LIB");
print ("INC: @INC\n");

require "webgutils.pl";
require "config.pl";
#---------------------------------

# print initial message
print
"This program is the WebGlimpse $VERSION archive remover.\n";
print
"For documentation, see http://glimpse.cs.arizona.edu/webglimpse.\n\n";


# prompt user for directory to remove archive from
$startpwd = `pwd`;
chomp($startpwd);
$archivedir = &prompt("Root directory for archive to be deleted ", $startpwd);

if($archivedir eq "") {
   $archivedir = ".";  # make it current dir
}

# try to change the directory to archivedir
$retval = chdir ($archivedir);
if($retval==0){
   print "Cannot change directory to $archivedir.  Quitting.\n";
   exit -3;
}

# get the 'real' path
$archivepwd = `pwd`;
chomp $archivepwd;

# make sure it has a configuration file
if(&TestConfig($archivepwd)==0){
   print "Cannot find configuration file for archive.  Quitting.\n";
   exit -4;
}

# list of files to delete
@dellist =();

print "\n\nAdding files to delete list...\n";

# delete the cron file and config
# and wgindex.html
$mycronfile = "$archivepwd/$CRONFILE";
$myconfig = "$archivepwd/$CONFIG";
$wgsearch = "$archivepwd/$WGSEARCH";
push(@dellist, $mycronfile) if -e $mycronfile;
push(@dellist, $wgsearch) if -e $wgsearch;

#--> removed, bgopal oct/12/96 (must do it after addsearch !!!!)
#push(@dellist, $myconfig) if -e $myconfig;

# delete the .glimpse_* files
# read the directory
opendir(DIR,"$archivepwd");
file: while ($file=readdir(DIR)) {
	# skip it if it's not a .glimpse_* file or .wg* or the eye.jpg
	next if ($file !~ /^\.glimpse_/) && 
				($file !~ /^\.wg/) && 
				($file !~ /^\.glimpse-eye\.jpg/);

	# also skip if it's the .wg_madenh file; we will delete this after the
	# call to addsearch -r
	next if ($file =~ /$MADENH_RE/);

	# ALSO skip if it's the .wgsiteconf!!!
	next if($file =~ /$SITECONF_RE/);

	push(@dellist, "$archivepwd/$file");
}
closedir(DIR);

# delete the remote files
opendir(DIR,"$archivepwd/$REMOTEDIR");
file: while ($file=readdir(DIR)) {
	# skip it if it doesn't start with a number
	next if $file !~ /^[0-9]+/;
	push(@dellist, "$archivepwd/$REMOTEDIR/$file");
}
closedir(DIR);

# list the files to delete
$numfiles = @dellist;
if($numfiles){
	print "\n\nThere are $numfiles files to delete: \n";
	foreach $file (@dellist){
		print "$file\n";
	}
}else{
	print "\n\nThere are no files to delete.  Directory is clean of WebGlimpse files.\n";
	exit(0);
}

# one last AYS?
$ays = &read_bool("Are you sure you want to delete all these files?", "n");
if(!$ays){
	print "Aborting.  No files deleted.\n";
	exit 0;
}
unlink(@dellist);
# remove the directory (if possible)
### TO DO -- check for success
rmdir("$archivepwd/$REMOTEDIR");

# remove the search boxes
$ays = &read_bool("Would you like to remove the search boxes from all HTML files?", "y");
if(!$ays){
	print "Aborting.  Search boxes not deleted.\n";
	print "$MADENH file retained for ease of deletion in the future.\n";
	exit 0;
}
# call addsearch -r to remove 'em
system("$WEBGLIMPSE_HOME/addsearch $archivepwd -r");
# now remove the MADENH file
unlink("$archivepwd/$MADENH");

# now remove the CONFIG file since addsearch has finished execution:
# bgopal, oct/12/96
unlink("$myconfig");


##############################################################################
## Subroutines
##############################################################################
