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

#Structure:  Tape labels are actually a short tar file.  
#	Tapes are registered in /usr/local/tapelabels. 
#	The directory is world writable, 'sticky', so that only the owner can
#	delete a file.  Tape labels are zero length files.

#	This file is contained in the first tar file on the tape.  
#	Thus a gnutar +test can determine if the label on a tape corresponds to
# the one  in the registry.
# 
# This first module creates an appropriate file in /usr/local/tapelabels
# And writes the file out to tape.
#

$newtape = $ARGV[0];
if ($newtape eq "") {
	print "\nsyntax: TapeLabel NameOfTape\n\n";
	print "\tSuggest that NameOfTape include your name, and \n";
	print "\tsomething of the nature of the contents of the tape.\n\n";
	&Quit;
	}

if ( -e "/usr/local/tapelabels/$newtape") {
	print "\n\tA label of that name already exists.\n";
	print "\tWe don't allow duplicate tape names. \n\n";
	print "\tThe following names are currently registered:\n\n";
	system "ls /usr/local/tapelabels";
	print "\n";
	&Quit;
	}
print "Will attempt to label this tape as $newtape\n";

require("TapeLibrary.perl");

#
# 	Attempt Read Volume label
#
	$tapelabel=&CheckVolumeLabel($Code);

	if ($Code == 100 ) {
		print "This is already a registered tape.\n";
		print "It is labeled, $tapelabel.\n";
		print "You must erase it before you can relabel it. \n";
		&Quit;
		}
	if ($Code == 102 ) {
  		print "The tape appears to have no label.  Proceeding. \n";
		}
	if ( $Code == 103 ) {
  		print "There appears to be data on this tape.\n";
		print "See your sysadmin if you need to register it here.\n";
		&Quit(103);
		}
	if ($Code == 104 ) {
  		print "Most Humble Apologies, but you do not own this tape.\n";
		print "You must persuade the owner to erase it himself.\n";
		&Quit(104);
		}

#We attempted a read.  So rewind first.

&RewindTape;

# now actually write the label on the host, and on the tape
print "Registering tape on server\n";
system ("touch /usr/local/tapelabels/$newtape");
print "Writing label on tape\n";
system("gnutar -c -b16 -C /usr/local/tapelabels $newtape");
if ($? != 0 ) {
	print "Error on writing tape.  Cleaning up and exiting\n";
	system "rm /usr/local/tapelabels/$newtape ";
	&Quit;
	}
print "Tape has been labeled and registered as $newtape\n";

sub Quit {
exit;
}
#
