#
#  MchangeCmd.pm
#  --command line interface to mchange
#
#  Brian Hackbarth
#  hackbart@cs.wisc.edu
#  

package MchangeCmd;

use strict;
use Mchange;
use lib "/afs/.cs.wisc.edu/s/mchange-2.2/common/perl5";

sub usage {
  my $string =  <<__END__
Usage: mchange [args] [-rt rtnum] [-run] [hostname]

Note: If you do not specify any args, the GUI version of mchange will appear

Options:
  -a        Add a part to a machine

The following attributes can be used as command line arguments (i.e -os, etc)

  Arg:      Valid options:
--------------------------------------- 
  -os        any valid os name from csdb
  -status    any valid machine status from csdb
  -room      any valid room number (ex: M1234, 2222)
  -lock      any valid lock ( 0422 )
  -billtype  digits representing billtype (1-6)
  -condor    1 or 0
  -mnotes    80 character of text in quotes (ex: 'hi, I take care of mail')
  -validos   any valid os name from csdb

The following affect both the GUI and command line version

  Arg:      Valid option:
---------------------------------------
  -rt       any valid rt number
  -run       no args--runs the script after completion
  
__END__
;
  print $string;

}

sub run {

  my ( $self, %params ) = @_;

  my $hostname = $params{mname};
  my $run = $params{run};
  my $rt = $params{rt};
  my $opts = $params{opts};

   #check to see if they requested help
  if ( $opts->{h} ) {
    $self->usage;
    exit;
  }
  
  my $mc = new Mchange( error => 'text' );

  $mc->set_rt( $rt );
  
  $hostname = (gethostbyname($hostname))[0] || $hostname;
  my $machine = $mc->register_machine( $hostname );

  foreach ( @{$opts->{a}} ) {
    $mc->move_part( $hostname, $_ );
  }

  delete $opts->{a};
  
  #move on to the rest
  while ( my ($attrib, $val) = each( %$opts ) ) {
    my $ret = $mc->set_attrib( $hostname, $attrib, $val );
    exit( 0 ) if $ret eq 'error';
  }
  
  #write the script out
  $mc->do_script( $run );
  
}

1;
