# -*-perl-*-

# $Id$

# Front-end to add, remove, or list limits in the db.

# $Log$
# Revision 1.10  2010/03/28 18:44:51  koczan
# Make the help messages correct
#
# Revision 1.9  2010/02/16 19:39:05  koczan
# more descriptive messages
#
# Revision 1.8  2005/11/01 22:42:16  thomas
# change @_ to $_ to eliminate -d:ptkdb warnings
#
# Revision 1.7  2004/02/24 15:02:05  thomas
# commit before 0.3.12 fork
#
# Revision 1.6  2003/06/09 14:45:11  thomas
# schema version of database
#
# Revision 1.5  2002/09/23 22:03:00  thomas
# 0.3.2 to production
#
# Revision 1.4  2002/08/20 21:59:56  thomas
# installed 0.3.0
#
# Revision 1.3  2002/08/14 17:13:07  thomas
# 0.2.9 release
#
# Revision 1.2  2002/07/10 14:01:29  thomas
# sushi-0.2.4
#
# Revision 1.1  2002/07/08 15:56:34  thomas
# 0.2.3 release
#
# Revision 1.1.1.1  2002/05/21 14:13:34  thomas
# sushi - backup to disk
#

package Sushi::Command::AfsVol;

use strict;
use Getopt::Long;

my %subs = (
	    listfree  => \&listfree,
	    listmount => \&listmount,
	    mount     => \&mount,
	    umount    => \&unmount,
	    unmount   => \&unmount,
	    help      => \&printhelp,
	    '--help'  => \&printhelp
	   );


###############################################################################

sub main {
  my ($class) = shift;
  my $cmd = shift @ARGV;

  my $args;
  my $help = 0;

  Getopt::Long::Configure ("bundling");
  my $res;
  if (!($res = GetOptions (
			   'volume_name|v=s', \$args->{volume_name},
			   'available|a',     \$args->{available},
			   'deleted|d',       \$args->{deleted},
			   'total_bytes|t=s', \$args->{total_bytes},
			   'used_bytes|u=s',  \$args->{used_bytes},
			   'reason|r=s',      \$args->{reason},
			   'help|h=s',        \$help
			  ))) {
    $main::log->error (&help);
    return 0;
  }


  if ($cmd eq 'mount' || $cmd eq 'unmount' || $cmd eq 'umount') {
    defined ($ARGV[0]) and $args->{volume_name} = shift @ARGV;
    defined ($ARGV[0]) and $args->{reason}      = shift @ARGV;
  } else {
    defined ($ARGV[0]) and $args->{volume_name} = shift @ARGV;
    defined ($ARGV[0]) and $args->{available}   = shift @ARGV;
    defined ($ARGV[0]) and $args->{total_bytes} = shift @ARGV;
    defined ($ARGV[0]) and $args->{used_bytes}  = shift @ARGV;
  }

  if (defined $subs{$cmd}) {
    return $subs{$cmd}($args);
  }
  $main::log->error("Bad afsvol sub-command specified\n");
  return;
}

###############################################################################

sub listfree {
  my $rows = Sushi::GenDB::display (
				    $_[0],
				    { print_headings => 1},
				    $::table_defs->{afsvol_freespace}
				   );
  return;
}

###############################################################################

sub listmount {
  my $rows = Sushi::GenDB::display (
				    $_[0],
				    { print_headings => 1},
				    $::table_defs->{afsvol_mount}
				   );
  return;
}

###############################################################################

sub mount {
  my $args = shift;
  my $path = Sushi::AfsBits::mountVolume ($args->{volume_name}, $args->{reason});
  if (defined $path) {
    print "$args->{volume_name} is mounted at $path.  Please unmount when finished.\n";
  }
}

###############################################################################

sub unmount {
  my $args = shift;
  if (Sushi::AfsBits::unMountVolume ($args->{volume_name}, $args->{reason})) {
    print "Volume successfully unmounted.\n";
    return 1;
  } else {
    print "Volume unmount failed.\n";
    return 0;
  }
}

###############################################################################

sub aliases {
  return ();
}

###############################################################################

sub shortHelp {
  return "List afs volume status, mount or unmount volumes";
}

###############################################################################

sub printhelp {
  print help();
  return 1;
}

###############################################################################

sub help {
  my $help = <<__Eof__

afsvol\tlistfree <options>
\tlistmount
\tmount <volume> <reason>
\tumount <volume> <mount-reason>

__Eof__
;
}

###############################################################################

1;
