#!/usr/local/bin/perl
#Copyright 1992	Sherwood Botsford

$Label = $ARGV[0];
if ($Label eq "") {
	print "\nLeast worthy tape slave requests label you wish to erase.\n";
	print "Once provided, Humble Servant will:\n";
	print "\t1. Unregister the tape\n";
	print "\t2. Remove the tape's log files from \n";
	print "\t   Most Exhalted User's backup directory\n";
	print "\t3.	Write a new end of media mark on the tape.\n\n";
	exit;
	}

print "\nVerifying that indeed $Label is registered here.\n\n";

if ( ! -e "/usr/local/tapelabels/$Label" ) {
	print "\tRitual deferences, but $Label is not registered here.\n";
	print "\tWould Most Gracious User please check his typing.\n\n";
	exit;
	}

print "\nVerifying that you own this tape.\n\n";

if ( ! -o "/usr/local/tapelabels/$Label" ) {
	print ("
		Pardon intrusion by this mere servant,
		but you don't own $Label.
		Although unworthy, humble tape slave is honourable.
		Only the tape's owner can command erasing a tape.
		\n");

	exit;
	}


require("TapeLibrary.perl");

#
# 	Read Volume label
#
	$tapelabel = &CheckVolumeLabel($Code);
	print "Tape is labeled $tapelabel\n\n";
#
#	Check the label

#	A series of checks are made.  CheckVolumeLabel tries to read 
#	the first file on the tape.  If that returns blank, or more than a 
#	single line, or a single line that doesn't match with the name of tape, 
#	or a tape name that is not owned by the user, then the user is bumped. 

	if ($Code == 100 ) {
		print "Verified that this tape is registered on this server.\n";
		print "Verified that $user owns this tape. \n";
		}
	if ($Code == 102 ) {
  		print "Begging forgiveness for rude interuption but this tape has no label.\n";
		print "Humble tar slave is not permitted to write to an unlabeled tape.\n\n";
		&Quit(102);
		}
	if ( $Code == 103 ) {
  		print "Excuse Interuption, but this label is not registered\n";
		print "Cannot unregister an unregistered tape.  \n";
		&Quit(102);
		}
	if ($Code == 104 ) {
  		print "Most Humble Apologies, but you do not own this tape.\n";
		print "Only the Owner can unregister a tape.\n";
		&Quit(102);
		}

if ( $tapelabel ne $Label ) {
	print("
		Humble groveling servant begs to intrude:
		You have asked this most lowly one to erase label $label
		But on looking at the tape, it is labeled differently, 
		as $tapelabel.  Plenteous apologies, but my master has 
		decreed that they must match before I do this deed.
		\n");
	&Quit;
	}

&RewindTape;

print "Erasing Tape.\n\n";
&WriteMark(1);
print "Unregistering Tape \n\n";
system ("rm /usr/local/tapelabels/$tapelabel");
print "Removing Log files from user directory\n\n";
system "rm -f ~/backups/${tapelabel}-*";
print "$label has been erased\n";
