#!/usr/bin/perl -w

use Getopt::Long;
use POSIX "sys_wait_h";
use File::Basename;

#globals
my $session_file;
my %config;
my $rc = 0;
my $pid;

if (init()) {
    exit 1;
}

$config{PVFS2TAB_FILE} = $config{WORKINGDIR} . "/pvfs2tab";
foreach $key (keys(%config)) {
    if (ref($config{$key}) eq 'ARRAY') {
	$aref = $config{$key};
	print "$key=";
	
# Print the compute node file 
	if (($key eq 'COMPNODES') && (exists $config{'COMP_NODEFILE'})) {
	    system("rm $config{'COMP_NODEFILE'}");
	    open FH, ">$config{'COMP_NODEFILE'}" 
		or die "Couldn't open $config{'COMP_NODEFILE'}";
	    if (@$aref > 0)  # leave empty if no compute nodes
	    {
		for ($i=0; $i<@$aref-1; $i++) {
		    print FH "$aref->[$i]\n";	    
		}
		print FH "$aref->[$i]\n";
	    }
	    close FH;
	}

	if (@$aref > 0)  # leave empty if no compute nodes
	{
	    for ($i=0; $i<@$aref-1; $i++) {
		print "$aref->[$i],";
	    }
	    print "$aref->[$i]\n";
	}
    } else {
	print "$key=$config{$key}\n";
    }
}
print "PVFS2TAB_FILE=$config{PVFS2TAB_FILE}\n";

sub init {
    GetOptions(\%args,
	       'c|config:s',
	       's|session:s',
	       'r|root:s',
	       'm|machinefile:s',
	       'h|help:s'
	       );

    if ($args{'h'}) {
	usage();
	return(1);
    }

    $config_file = $args{'c'} || "~/auto_pvfsvol.conf";
    if (!-f $config_file) {
	print STDERR "ERROR: cannot find config file: $config_file\n";
	usage();
	return(1);
    }

    %config = ('PVFSPORT' => 7000,
	       'WORKINGDIR' => "/tmp",
	       'IONCOUNT' => 4,
	       'NODEFILE' => "machine_file",
	       'PROTOCOL' => 0,
	       'UNIQUEMETA' => 0,
	       'STORAGE' => "/tmp/data",
	       'SERVERLOG' => "/tmp/log",
	       'MOUNTPOINT' => "/pvfs_auto",
	       'BINDIR' => "/tmp/bin",
	       'SERVER' => "pvfs2-server",
	       'PINGPROG' => "pvfs2-ping",
	       'PROGROOT' => "./"
	       );
    
    ($config{'USERNAME'}) = getpwuid($>);
    my ($gid) = split(/\s*/, $();
    ($config{'GROUPNAME'}) = getgrgid($gid);

    $config{'PROGROOT'} = $args{'r'} || real_dir($0) || $config{'PROGROOT'};
    my $prog_root = $config{'PROGROOT'};
    require "$prog_root/pav_lib.pl";

    $rc = read_configfile($config_file, \%config);
    if ($rc) {
	print STDERR "ERROR: reading config file\n";
	return($rc);
    }


    $session_file = $args{'s'} || "$config{'WORKINGDIR'}/pvfs_autosession";
    if (!-f $session_file) {
	print STDERR "ERROR: cannot find session file: $session_file\n";
	usage();
	return(1);
    }
    
    $rc = read_sessionfile($session_file, \%config);
    if ($rc) {
	print STDERR "ERROR: reading session file\n";
	return($rc);
    }

    $config{'PROGROOT'} = $args{'r'} || $config{'PROGROOT'};
    $config{'NODEFILE'} = $args{'m'} || $config{'NODEFILE'};
    
    $pid = $$;
 
    return(0);
}

sub real_dir {
   my ($file, $reldir, $suffix) = fileparse(shift);
   my ($realdir, $envpwd);

   if ($reldir =~ /^\//) {
      $realdir = $reldir;
   } else {
      if (!$ENV{PWD}) {
         chomp($envpwd = `pwd`);
      } else {
         $envpwd = $ENV{PWD};
      }
      $realdir = $envpwd . "/$reldir";
   }
   $realdir .= '/';
   $realdir =~ s#/./#/#g;
   $realdir =~ s#//#/#g;
   return($realdir);
}
