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

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

################ SETTINGS ################
# index template
$TEMPLATE=".ghtemplate.html";

# name of the index file
$HTMLINDEX="ghindex.html";

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

# gif file
$EYEGIF = ".gheye.gif";
##############################################

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

# config is the file to create
$dir = `pwd`;
chop $dir;

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

# print initial message
print
"This program is the GlimpseHTTP archive remover.\n";
print
"It is part of GlimpseHTTP v2.0.  For documentation,\n";
print
"see http://glimpse.cs.arizona.edu/ghttp.\n\n";

# prompt user for directory to remove archive from
$indexdir = &read("Root directory for archive to be deleted ", $dir);
$config = $indexdir."/archive.cfg";

# ARE YOU SURE??
# $ays = &read_bool("This will remove all the GlimpseHTTP files".
		# " from the archive rooted at ".
		# $indexdir.".  Are you SURE you want to do this?","n");
# if(!$ays){
	# print "Aborting.\n";
	# exit 0;
# }

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

# delete the cron file and config
$mycronfile = $indexdir."/".$CRONFILE;
$mytemplate = $indexdir."/".$TEMPLATE;
$eyegif = $indexdir."/".$EYEGIF;
push(@dellist, $mycronfile) if -e $mycronfile;
push(@dellist, $config) if -e $config;
push(@dellist, $mytemplate) if -e $mytemplate;
push(@dellist, $eyegif) if -e $eyegif;

# delete the .glimpse_* files
# read the directory
opendir(DIR,"$indexdir");
file: while ($file=readdir(DIR)) {
	# skip it if it's not a .glimpse_* file
	next if $file !~ /^\.glimpse_/;
	push(@dellist, $file);
}
closedir(DIR);

# delete the ghindices
&del_ghindices($indexdir);

# 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 GlimpseHTTP 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);



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

sub del_ghindices {
	local($absdir) = @_;
	local($file, $dir, @subdirs);

	# add $HTMLINDEX to dellist
	push(@dellist, "$absdir/$HTMLINDEX") if -e "$absdir/$HTMLINDEX";

	@subdirs = ();

	# read the directory
	opendir(DIR,"$absdir");
	file: while ($file=readdir(DIR)) {
		next if $file =~ /^\./;
		next if $file eq $HTMLINDEX;
		if (-d $file) {
			# it's a subdir
			push(@subdirs, $file);
		} else {
			# it's a file
			# ignore it
		}
	}
	closedir(DIR);

	# process the subdirs
	foreach $dir (@subdirs) {
		&del_ghindices("$absdir/$dir");
	}
}

sub read {
	local($prompt,$def) = @_;
	print $prompt,"[",$def,"]:";
	$| = 1;
	$_ = <STDIN>;
	chop;
	return $_?$_:$def;
}


sub read_bool {
	local($prompt,$def) = @_;
	$ans = -1;
	until($ans >= 0){
		print $prompt,"[",$def,"] (y/n):";
		$| = 1;
		$_ = <STDIN>;
		chop;
		$_ = $_ ? $_ : $def;
		$_ = substr($_, 0, 1);
		if ($_ eq "y" || $_ eq "Y"){
			$ans = 1;
		}elsif ($_ eq "n" || $_ eq "N"){
			$ans = 0;
		}else {
			print "Please enter y or n.\n";
		}
	}
	return $ans;
}

