#!/usr/local/bin/perl
#!/usr/local/bin/perl
#!/usr/local/bin/perl
#!/usr/local/bin/perl
#!/usr/local/bin/perl
#!/usr/bin/perl

=pod

NAME
  pstinstall: Install a PST distribution from a directory


USAGE
  pstinstall pst_distribution_directory


DESCRIPTION
  Copy a distribution contained in a local distribution directory into a
  user's directory space.  This will overwrite the user's existing files.


EXAMPLE
  pstinstall PST.2001.06.01.22.32


NOTES
  This script is called from "install".  It is not meant to be run
  directly by a user.



=cut

use Cwd;

# Determine which code
$CODE = $ARGV[0];
$cwdir=cwd();

# Determine home directory
$UDIR = $ENV{'HOME'};
chomp $UDIR;
$MODE=0777;
# Determine PST root directory

#$PSTDIR = $ENV{'PSTDIR'};
$PSTDIR = "";
$PSTDIRDEFAULT="$UDIR/PST";
$pst_ok=0;
print "\n   * * * * * * * * * * * * * * * * * * * * *\n";
print "   *                                       *\n";
print "   *    Installation of PST in progress    *\n";
print "   *                                       *\n";
print "   * * * * * * * * * * * * * * * * * * * * *\n";
print "\n    Enter the location to place toolkit elements.\n";
if ($PSTDIR ne "")
{
  print "\n    Enter \"0\" to choose current setting: $PSTDIR\n";
  print "          \"1\" for default: $PSTDIRDEFAULT\n";
  print "          \"2\" to specify a different location\n";
  print "          \"3\" to read more.\n";
  print "          \"4\" to exit install.\n";
}
else
{
  print "\n    Enter \"1\" for default: $PSTDIRDEFAULT\n";
  print "          \"2\" to specify a different location\n";
  print "          \"3\" to read more\n";
  print "          \"4\" to exit install\n";
}

while ( $pst_ok==0 )
{
  $tmp=readline(*STDIN);
  chomp $tmp;
  #this should no longer be called (CB 6/20/01)
  if ($tmp == 0 && $PSTDIR ne "")
  {
    $PSTSUGGEST=$PSTDIR;
    if ($PSTSUGGEST =~ /^[\/\.a-zA-Z][\/\.a-zA-Z0-9]*$/)
    {
      $PSTDIR=$PSTSUGGEST ;
      $pst_ok=1;
    }
    else
    {
      while ($pst_ok == 0)
      {
        print "    Current setting for \$PSTDIR has incorrect format.\n";
        print "    Please make a new entry: use a character or \"\/\" to start, and use only alpha-numeric\n    characters, \"\.\", and \"\/\".\n";
        print "\n    Enter \$PSTDIR:  ";
        $PSTSUGGEST=readline(*STDIN) ;
        chomp $PSTSUGGEST;
        if ($PSTSUGGEST =~ /^[\/a-zA-Z][\/a-zA-Z0-9]*$/ )
        {
          $PSTDIR=$PSTSUGGEST;
          $pst_ok=1;
        }
      }
    }
  }
  elsif ($tmp == 1)
  {
    $PSTDIR=$PSTDIRDEFAULT;
    $pst_ok=1;
  }
  elsif ($tmp == 2)
  {
    while ($pst_ok == 0)
    {
      print "    Enter location for PST:  ";
      $PSTSUGGEST=readline(*STDIN);
      chomp $PSTSUGGEST;
      if ($PSTSUGGEST =~ /^[\/\.a-zA-Z][\/\.a-zA-Z0-9]*$/)
      {
        $PSTDIR=$PSTSUGGEST;
        $pst_ok=1
      }
      else
      {
        print "    Please use a character or \"\/\" to start and only alpha-numeric\n    characters, \"\.\", and \"\/\".\n"
      }
    }
  }
  elsif ($tmp == 3)
  {
    print "     Your answer will set the root directory for PST.\n";
#    if ($PSTDIR eq "")
#    {
#      print "\n     Currently \$PSTDIR is not set.\n\n";
#    }
#    else
#    {
#      print "\n     Currently \$PSTDIR is set to $PSTDIR.\n\n";
#    }

    print "     The suggested default location for a user's installation is $PSTDIRDEFAULT.\n";
    print "     The suggested default location for a systemwide installation is /usr/local/pst \n";
  }
  elsif ($tmp == 4)
  {
    print "     Exit from install.\n\n";
    exit;
  }
  else
  {
    print "     Unexpected entry: Please enter again\n";
  }
}

# Create PSTDIR
if (! -e "$PSTDIR")
{
  mkdir "$PSTDIR", $MODE
}
else
{
  if (-f "$PSTDIR")
  {
    print "\n    You already have a file with name $PSTDIR!\n";
    print "    Run ./$0 again after renaming $PSTDIR\n";
    print "    or rerun ./$0 and specify a different \$PSTDIR\n";
    exit;
  }
  elsif (-d "$PSTDIR")
  {
    print "\n    Overwrite contents of $PSTDIR? (y/n) ";
    $overwrite=readline(*STDIN) ;
    chomp $overwrite;
    print "\n";
    if ($overwrite !~ /^[yY]/)
    {
      print "pstinstall: exiting without installing PST.\n" ;
      exit;
    }
    elsif ( ! -w "$PSTDIR" )
    {
      print "pstinstall: ERROR: Cannot write to $PSTDIR.\n";
      print "Please rerun ./$0 after modifying permissions.\n";
      exit;
    }
  }
  else
  {
    print "\n    $PSTDIR already exists, but it is neither a plain\n";
    print "    file nor a directory.  Please rerun ./$0 after renaming\n";
    print "    or deleting $PSTDIR.\n";
    exit;
  }
}

# Create PSTDIR/bin
mkdir "$PSTDIR/bin", $MODE;

# Cd to distribution directory
chdir "$cwdir/$CODE";

# Copy distribution to user home
print "Perl program conversion successful\n";
chdir "$cwdir/$CODE/PST";
$CSTAT = system("cp","-R",".","$PSTDIR");
if ($CSTAT != 0)
{
  die "pstinstall: ERROR: PST distribution copy failed.\n"
}

# Copy executables to $PSTDIR/bin
$dirs = `/bin/ls -R . | grep "./*:" 2>&1`;
@dirs = split /\n/,$dirs;
foreach $dir (@dirs)
{
  $dir=~s/:$//;
  if ($dir =~ /\/bin$/)
  {
    system("cp $dir/* $PSTDIR/bin")
  }
}

# Convert perl programs
chdir "$PSTDIR";
$dirs = `/bin/ls -R . | grep "./*:" 2>&1`;
@dirs = split /\n/,$dirs;
$SSTAT = 0;
foreach $dir (@dirs)
{
  $dir=~s/:$//;
  if ($dir =~ /other_sites/) {next};
  $CSTAT = system("./MD/bin/whichperl $dir $PSTDIR");
  if ($CSTAT != 0)
  {
    print "pstinstall: ERROR: Perl program conversion failed in $dir.\n";
  }
  $SSTAT = $SSTAT + $CSTAT;
}

if ($SSTAT != 0)
{
  die "pstinstall: ERROR: Perl program conversion failed.\n"
}

# Update local.pm to include $PSTDIR
$tmp=`./MD/bin/pstinstall2 TUSC archive.pm PSTDIR=$PSTDIR`;

# Massage (slightly) output from pstinstall2
$tmp=~s/^\$PST_SITE=(.*?)\n//m;
$CENTER=$1;
$tmp=~s/^\s*PSTset:.*\n//mg;

# Report details of successful install,
# and issue instructions to user.
print "copy $CODE files to $PSTDIR successful\n\n";
print "* " x 37;
print "\n";
print "$tmp";
print "* " x 37;
print "\n";
print "  FINAL STEP for installation:\n\n";
print "  Add \"$PSTDIR/bin\" to your path!\n\n";
#print "  Also, PST looks for the following environment variables:\n\n";
#print "\$PSTDIR=$PSTDIR\n";

__END__
