#!/usr/local/bin/perl

#####################################################################
##
## Rcs_ID="$RCSfile: clam,v $"
## Rcs_ID="$Revision: 1.8 $ $Date: 2000/03/24 21:04:36 $"
##
#####################################################################

use lib "/usr/openv/local/lib" ;
require "nbu.pl" ;

open(DEFAULTPRINTER, "lpstat -d|");
while (<DEFAULTPRINTER>) {
	next unless ( $_ =~ /default destination/ ) ;
	($junk,$junk,$junk,$printer)= split(/\s/,$_) ;
	chomp $printer; 
}
close(DEFAULTPRINTER) ;


#This whole program is enclosed in a while loop.  If the user gives an
#inappropriate answer, he/she is returned to the menu.  If he/she gives
#an appropriate answer, he/she is returned to the menu after that executes
#(An appropriate answer is 1-7.)

$robot_num="undefined";
$|=1 ;

# Pre-defined variables

MAIN: while ( 1 ) {

	#Display the main menu
	&Print_clam_menu ;

	#Get their answer to the menu's question.
	$main_menu_option=<STDIN> ;
	chomp $main_menu_option ;

	#Check to make sure the user gives a valid answer
	&Validate_options("1","7","7","exit","$main_menu_option") ;
	
	#The clean drive option does not use storage units...
	if ( $robot_num eq "undefined" or $robot_num eq "" ) {
		unless ( $main_menu_option == 5 or 
			 $main_menu_option == 4 or
			 $main_menu_option == 1) {

			#Get a list of all storage units
			&Get_robots() ;
			
			#Choose a storage unit to work with
			&Choose_robots() ;
			}
		}

	if ( $debug eq Y ) {
		print "
	The following is debug info.  To turn it off, set debug to \"N\".
	We will work with the following storage unit, then:
		Media Type Number = $media_type_num
		Media Type        = $media_type
		Robot Number      = $robot_num 
		Robot Host        = $robot_host 
		Robot Type Number = $robot_type_num 
		Robot Type        = $robot_type
		LC Robot Type     = $lc_robot_type
		Density Number    = $density_num 
		Density           = $density
		Number of Drives  = $number_of_drives  
		On Demand         = $demand 
		Max MPX           = $maxmpx 
		Max Fragment Size = $frag_size
		High Slot Number  = $high_slot\n"
		}
	
	if    ( $main_menu_option == 1 ) {
		$robot_num='';
		&Clear_screen ;
		&Get_robots ;
		&List_media("null") ;
		&Look_for_offsite_tapes() ;

		print "\nThis option has completed.\n" ;
		print "Press enter to return to the main menu." ;
		$worthless_answer=<STDIN> ;
					
		}  # of the main_menu_option being 1
	
	elsif ( $main_menu_option == 2 ) {
		#Eject volumes from the library based on search criteria

		EJECT: while ( 1 ) {

			if ( $debug ne Y ) { &Clear_screen }


			print <<EOF ;
Please select one of the following operations by entering its number below.
1. Eject the $maxslots oldest, non-expired volumes
2. Eject the $maxslots oldest, non-expired volumes from a particular pool 
3. Eject one or more volumes by name
4. Exit this menu (Q or q also works.)

EOF

print "Which of the above operations would you like to perform? " ;

			$eject_opt=<STDIN>;
			chomp $eject_opt ;

			&Validate_options("1","4","4","next MAIN","$eject_opt") ;

			if ( $debug ne Y ) { &Clear_screen }

			if ( $eject_opt == 1 ) {
				#First, let's get a list of all media in this library
				&List_media() ;

				@tapes_to_eject=() ;
				@tapes_to_look_at = (sort {$media_updated{$a} 
					<=> $media_updated{$b}} keys %media_updated) ;

				$looked_at = 0;
				foreach $media_id (@tapes_to_look_at) {
					if ( $looked_at == $maxslots ) { last } 
					if (defined ($media_ids{$media_id})) {
							push (@tapes_to_eject,$media_id) ;
							$looked_at++ ;
						}
					}

				&Display_tapes_to_eject(@tapes_to_eject) ;
				@tapes_to_eject=() ;
				&Update_contents("l") ;

				}

			elsif ( $eject_opt == 2 ) {

			#Choose a pool to work with
			&Choose_pool ;

			#First, let's get a list of all media in this library
			&List_media("-p $pool")  ;

			@tapes_to_eject=() ;
			@tapes_to_look_at = (sort {$media_updated{$a} 
					<=> $media_updated{$b}} keys %media_updated) ;

			$looked_at = 0;
			foreach $media_id (@tapes_to_look_at) {
				if ( $looked_at == $maxslots ) { last } 
				if (defined ($media_ids{$media_id})) {
						push (@tapes_to_eject,$media_id) ;
						$looked_at++ ;
					}
				}

				&Display_tapes_to_eject(@tapes_to_eject) ;
				@tapes_to_eject=() ;
				&Update_contents("l") ;

			}


			elsif ( $eject_opt == 3 ) {
				
				print <<EOF ;

This menu option allows you to eject one or more volumes by their name.
To do this, enter the names of one or more volumes you want to eject.
(You may separate the names by spaces, commas, or both.  Please enter the names
EOF

				print "here: " ;
				$tapes_to_eject=<STDIN> ;
				chomp $tapes_to_eject ;
				$tapes_to_eject =~ s/,\s*/ /g ;
				if (! ($tapes_to_eject) ) { last }
				@tapes_to_eject = split(/\s/,$tapes_to_eject) ;
				%media_ids=();
				for $media_id (@tapes_to_eject) {
					#First see if it's in the robot
					$vmquery_cmd="vmquery -b -rn $robot_num" ;
					if ( $show_commands eq Y ) {print "($vmquery_cmd)\n"} ;
					open(LOCATIONLIST, "$vmquery_cmd|");
					while (<LOCATIONLIST>) {
						#print $_ ;
						if ( $_ =~ /^$media_id\s/ ) { $media_ids{$media_id} = 1}
						}
					close(LOCATIONLIST);

					if ( defined ($media_ids{$media_id})) {
						$vmquery_cmd="vmquery -b -m $media_id" ;
						if ( $show_commands eq Y ) {print "($vmquery_cmd)\n"} ;
						open(LOCATIONLIST, "$vmquery_cmd|");
						while (<LOCATIONLIST>) {
							if ( $_ =~ /^$media_id\s/ ) {
								@tapelist=split(/\s+/,$_) ;
								$thisdens = $tapelist[1];
								$thisslot = $tapelist[4];
								$thisside = $tapelist[5];
								if ( $thisslot =~ /^-$/ ) {
									print "Volume $media_id is not physically in robot $robot_num!\n";
									next ;
									}
								else {
									$media_ids{$media_id} = 1 ;
									$media_slots{$media_id} = $thisslot ;
									$density_num = &Get_density_num($thisdens) ;
									$media_density{$media_id} = $density_num ;
									$media_sides{$media_id} = $thisside ;
									$media_kbytes{$media_id} = 'N/A' ;
									$media_updated{$media_id} = 'N/A' ;
									$media_expires{$media_id} = 'N/A' ;
									}
								}
							}
							close (LOCATIONLIST) ;
						}
						#Else the tape is not in this robot!
						else {
							print "Volume $media_id is not even assigned to robot $robot_num!\n" ;
							}
					}

					@media_ids=(keys %media_ids) ;
					if ( $#media_ids != -1 ) {
						&Display_tapes_to_eject(keys %media_ids) ;
						@tapes_to_eject=() ;
						&Update_contents("l") ;
					}
					else {
						print "\nNONE of the tapes that you specified are in robot $robot_num!\n";
						}
				}

				print "\nThis option has completed.\n" ;
				print "Press enter to return to the main menu." ;
				$worthless_answer=<STDIN> ;
					
			}

		}
	
	elsif ( $main_menu_option == 3 ) {
	#Insert volumes into the library that have been requested by a restore

		print "\nThis option allows you to insert volumes into the library, and\n" ;
		print "satisfy any mount requests that may be looking for those volumes...\n\n";

		&Look_for_empties ;
		&Insert_any_tapes ;

		print "\nIf you inserted tapes, would you like to see\n";
		print "if there are any pending requests that are looking for them (Y|N)? ";
		$find=<STDIN>;
		chomp $find;
		
		if ( $find eq "Y" or $find eq "y" ) {
			print "\nLooking for pending requests...\n";
			&Find_requests ;
			&Satisfy_requests ;
		}
		

		print "\nThis option has completed.\n" ;
		print "Press enter to return to the main menu." ;
		$worthless_answer=<STDIN> ;
					
		
		}
	
	elsif ( $main_menu_option == 4 ) {

		print "This option allows you to select a new storage unit to work with.\n" ;
		#Get a list of all storage units
		&Get_robots() ;

		#Choose a storage unit to work with
		&Choose_robots() ;

		print "\nYou have now selected robot: $robot_num.\n";

		print "\nThis option has completed.\n" ;
		print "Press enter to return to the main menu." ;
		$worthless_answer=<STDIN> ;

		}
	
	elsif ( $main_menu_option == 5 ) {
	#Cleaning tape maintenance

	print "Netbackup's tpclean command only works if you are logged\n" ;
	print "into the server where the drives are located.  If you need\n";
	print "to clean drives that are not physically connected to this\n";
	print "server, you will need to log into that server and run\n" ;
	print "this program there.\n\n" ;

	&Drive_num_to_name ;
	&Clean_drives ;

	print "\nThis option has completed.\n" ;
	print "Press enter to return to the main menu." ;
	$worthless_answer=<STDIN> ;

		}
	
	elsif ( $main_menu_option == 6 ) {
	#Netbackup Inventory
		&Update_contents("m") ; 
		print "\nThis option has completed.\n" ;
		print "Press enter to return to the main menu." ;
		$worthless_answer=<STDIN> ;

		}
	

} #End of while loop

sub Print_clam_menu {

	if ( $debug ne Y ) { &Clear_screen }  
	print
"***************************************************************************
*                       CURTIS' LIBRARY AUTOMATION MENU                    *
*                           (or CLAM, for short)                           *
****************************************************************************

>> Current Master - $master, Robot: $robot_num

CLAM automates all your automated library operations:

1. Eject ALL volumes that bpvault says should go offsite (from all libraries)

2. Eject volumes from the library based on search criteria

3. Insert volumes into the library for a restore or for reuse by Netbackup

4. Work with a different library

5. Clean drives (or see if they need to be cleaned)

6. Re-inventory the current library

7. Exit CLAM (Q or q also works)

Please choose one of the options above by entering its number (1-7): "
}
