#
# Copyright (c) 1992 The Ohio State University.
# All rights reserved.
#
# Redistribution and use in source and binary forms are permitted
# provided that: (1) source distributions retain this entire copyright
# notice and comment, and (2) distributions including binaries display
# the following acknowledgement:  ``This product includes software
# developed by The Ohio State University and its contributors''
# in the documentation or other materials provided with the distribution
# and in all advertising materials mentioning features or use of this
# software. Neither the name of the University nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#

$options = "l:d:hH:";
$usage = 
"usage: movetape [-l location] [-H home] [-h] [-d debuglevel] tapeid+
    -l location		move tapes to this location.
    -H home		set the home location.
    -h			display this message.
    -d debuglevel	turn on debugging output.
    tapeid		one or more tape IDs, to be checked out.\n";

require 'global.defs';
require 'local.defs';
require $yagrip_perl;
require $backuplib_perl;

&read_config();			# Read backup.config file

&init();			# Deal with args and get necessary info...

if (&lock($tape_db_file) != $LOCK_OK) {
    die "error (move-tape): cannot lock the tape database $tape_db_file.\n";
}

&open_db(*TAPEDB, $tape_db_file, $db_mode) || 
  &sigh("cannot open the tape database $tape_db_file.\n");

tapeid_loop:
  foreach $tapeid (@ARGV) {
      if (! &read_db(*TAPEDB, $tapeid, *tape_db_value, *tape_db_entry)) {
	  print "warning (move-tape): there's no entry for $tapeid in the tape database - skipping it.\n";
	  next tapeid_loop;
      }

      $current_location = $tape_db_entry{"location"};
      $home_location = $tape_db_entry{"home"};

      if ($destination eq "") {
	  if ($current_location eq $home_location) {
	      print "Tape $tapeid is already at its home location ($home_location).\n";
	      next tapeid_loop;
	  } else {
	      $tape_db_entry{"location"} = $home_location;
	  }
      } else {
	  if ($current_location eq $destination) {
	      print "Tape $tapeid is already at $destination.\n";
	      next tapeid_loop;
	  } else {
	      if ($current_location ne $home_location) {
		  $answer = &askdef("Tape $tapeid is at $current_location.  Do you want to move it to $destination", "yes", 
"Type 'yes' if you want to move the tape anyway, 'no' if you don't want 
to change the tape's location.\n");

		  next tapeid_loop if $answer ne "yes";
	      }
	      $tape_db_entry{"location"} = $destination;
	  }
      }
      $new_location = $tape_db_entry{"location"};

      if (defined($opt_H)) {
	  $tape_db_entry{"home"} = $opt_H;
	  &make_log_entry("move-tape: changed home for $tape_id to $opt_H");
      } 

      &write_db(*TAPEDB, $tapeid, *tape_db_entry, $tape_db_file, "tape_db");

      &make_log_entry("move-tape: moved $tape_id from $current_location to $new_location");
  }

# fixme: should check errors
&close_db(*TAPEDB, $tape_db_file);

&unlock_all();

exit 0;

sub init {
    #
    # Make sure that we clean up after ourselves if someone wants to kill us.
    #
    $SIG{'INT'} = 'handle_interrupts';
    $SIG{'HUP'} = 'handle_interrupts';
    $SIG{'TERM'} = 'handle_interrupts';
    $SIG{'QUIT'} = 'handle_interrupts';

    die $usage if ! &getopt($options);
    die $usage if $#ARGV == -1;
    die $usage if defined($opt_h);

    if (defined($opt_l)) {
	$destination = $opt_l;

	if (! defined($known_locations{$destination})) {
	    die "error (move-tape): $destination isn't a known location.  Check your backup.config file.\n";
	}
    }

    if (defined($opt_H)) {
	$answer = &askdef("Are you sure that you want to set the home location of these tapes",
			  "no",
"Enter 'yes' if you want to change the home location, 'no' if you want to quit now.\n");
	if ($answer ne "y" && $answer ne "yes") {
	    exit(1);
	}
	if (! defined($known_locations{$opt_H})) {
	    die "error (move-tape): $opt_H isn't a known location.  Check your backup.config file.\n";
	}
    }
}

