#! /usr/bin/perl
$bindir        = "/afs/cad.njit.edu/u/a/l/alexg/BSP/bin";
$includedir    = "/afs/cad.njit.edu/u/a/l/alexg/BSP/include";
$libdir        = "/afs/cad.njit.edu/u/a/l/alexg/BSP/lib";
$mandir        = "/afs/cad.njit.edu/u/a/l/alexg/BSP/man";





















@Files_to_tidy     = ();
$Tmpfile_prefix = "bsp$$";

# Set up the signal handler
$SIG{'INT'}  = 'tidy_up_and_die';
$SIG{'QUIT'} = 'tidy_up_and_die';

if (!$ENV{'PWD'} || ($ENV{'PWD'} eq "")) {
  $ENV{'PWD'} = &backtick("pwd");
  chop($ENV{'PWD'});
}
      







sub FormatManual {
  local($manpage)=$_[0];
  local($pager) = "more";
 
  $pager = $ENV{'PAGER'} if ($ENV{'PAGER'});
  if      (-e $mandir . "/cat1/$manpage") {
    &run_something("$pager " . $mandir . "/cat1/$manpage","Catman manual");
  } elsif (-e $mandir . "/man1/$manpage") {
    &run_something("nroff -man " . $mandir . "/man1/$manpage | $pager",
                   "Nroff manual");
  } else {
    print STDERR "Unable to find manual page in directory ",
                 $mandir , "/man1/$manpage\n";
  }

}






sub grab_next_arg {
  local($option) = $_[0];
  local($temp);
  if ($#ARGV >= 0) {
    $temp = $ARGV[0]; shift(@ARGV); 
    return($temp);
  } else {
    print STDERR "$Pgm: no argument following $option option\n";
    $Status++;
  }
}








sub run_something {
  local($str_to_do, $tidy_name) = @_;
  local($return_val) = 0;
  local($die_msg,$shcmd);

  print STDERR "\n$tidy_name:\n\t" if $Verbose;
  print STDERR "$str_to_do\n"      if $Verbose;

  if (length($str_to_do)<256) {
    system("$str_to_do");
  } else {
    $shcmd = "$Tmpfile_prefix" . ".sh";
    if (!(open(SHELLOUT,"> $shcmd"))) {
      $die_msg = "\n\n$Pgm fatal error: ";
      $die_msg.= "a system call contains >256 characters ";
      $die_msg.= "(".length($str_to_do)." chars)\n";
      $die_msg.= "\t$str_to_do\n";
      &tidy_up_and_die(1,$die_msg);
    }
    print SHELLOUT "#!/bin/sh\n";
    print SHELLOUT $str_to_do , "\n";
    close(SHELLOUT);
    chmod 0755 , $shcmd;
    system("./$shcmd");
  }
  $return_val = $?;
  if ($return_val != 0) {
    $die_msg = "\n\n$Pgm: execution of the $tidy_name had trouble";
    $die_msg .= " (program not found)" if $return_val == 255;
    $die_msg .= " ($!)" if $Verbose && $! != 0;
    $die_msg .= "\n";
    &tidy_up_and_die($return_val, $die_msg);
  }
}






sub backtick {
  local($cmd)=$_[0];
  local($return_val, $return_str,$die_msg);

  $return_str = `$cmd`;
  $return_val = $?;
  if ($return_val != 0) {
    $die_msg = "\n\n$Pgm: execution of the following command had trouble:\n";
    $die_msg .= "\t$cmd\n";
    $die_msg .= " ($!)" if $Verbose && $! != 0;
    $die_msg .= "\n";
    &tidy_up_and_die($return_val, $die_msg);
  }
  return $return_str;
}






sub tidy_up {
  local($return_val, $msg) = @_;
  print STDERR $msg;
  print STDERR "deleting... @Files_to_tidy\n" if $Verbose && 
                                                 $#Files_to_tidy >= 0;
  unlink @Files_to_tidy                       if  $#Files_to_tidy>= 0 &&
                                                 !$KeepTemps;
  system("rm -f $Tmpfile_prefix*")            if !$KeepTemps;
}

sub tidy_up_and_die {
   &tidy_up(@_);
   exit(1);
}



sub getNumber {
  local($string)=$_[0];

  $string =~ /^(\d+)\.(\d+)$/ && do {return ("$1.$2"/1.0);};
  $string =~ /^\.(\d+)$/      && do {return ("0.$1"/1.0);};
  $string =~ /^(\d+)$/        && do {return ("$1.0"/1.0);};
}






sub readHostNames {
  local($filename);
  local(@res)=();
  local($filename);

  $filename = $ENV{'HOME'} . "/". $_[0];
  print STDERR "$Pgm: opening hostfile \"$filename\"\n" if $Verbose;
  if (!open(HOSTNAMES,$filename)) {
     print STDERR "$Pgm: unable to read hostfile \"$filename\"\n";
     exit(0);
  }
  while (<HOSTNAMES>) {
    if (/^\s*host\(([^\)]*)\)/) {
       push(@res,$1);
    }
  }
  close(HOSTNAMES);
  return @res;
}






sub div0 {
  local($x,$y)=@_;

  if ($y==0.0) {
    return 0.0;
  } else {
    return $x/$y;
  }
}



1;

