#!/usr/bin/perl

=pod

NAME
  pstdistrib: Prepare a PST code distribution. [obsolete? See NOTES. --MJG]


USAGE
   pstdistrib temporary_dest_dir  [maybe; see NOTES --MJG]


DESCRIPTION
  Collect all routines into a directory tree.  Tar the result into an
  archive file whose name includes the date.


EXAMPLE
   pstdistrib /tmp/werne  [I think this is wrong; see NOTES. --MJG]


NOTES
  [I spent quite a while trying to decypher this code, but much of it
  remains enigmatic to me.

  My guesses:

    () This script first copies the source heirarchy to another
       directory, then tars and gzips that temporary dest directory.

    () The command-line argument to this script is the root of the
       temporary destination directory heirarchy.

    () The intermediate step of copying to a temporary destination dir
       avoid copying the CVS subdirs and other things not explicitly
       part of the "distriution".

    () Somehow the current directory matters.  The script seems to store
       the destination dir heirarchy and the destination .tar.gz file
       in the current directory.

    () CAUTION: This script apparently assumes that "PST" is a subdir
       of the user's home directory!!!!

  If all of the above guesses are correct, then what purpose does the
  command-line argument of this script serve?  I found no reference to
  it (although with Perl's obscure variable names I can never be sure),
  and the original code lacked sufficient comments.

  Most of the comments in this code are my guesses.  Do not take them too
  seriously, because I did not write this script, I do not understand it,
  and I can not get it to work.

  Perhaps this script is old (i.e. not maintained) and no longer does
  anything useful.

  --MJG]


SEE ALSO
  PST/MD/bin/makeDist


=cut

use Cwd;

# Get current time and date
($min,$hour,$mday,$mon,$year)=(localtime)[1,2,3,4,5];
$year=$year+1900;
$mon=$mon+1;


#---------------------------------------------------- Define directories
$DED   = cwd();                      # Current dir; root of dest distrib(?)
$UDIR  = $ENV{'HOME'}; chomp $UDIR;  # User home directory...???!?
$RDIR  = "PST";                               # PST source root dir name
$MDIR  = "$RDIR/MD";                          # PST/MD subdir
$TDIR  = "$RDIR/TUSC";                        # PST/TUSC subdir
$SDIR  = "$RDIR/SET";                         # PST/SET subdir
$form  = "PST.%4.4d.%2.2d.%2.2d.%2.2d.%2.2d"; # dest filename format
$NAME  = sprintf $form,($year,$mon,$mday,$hour,$min);
$DDIR  = "$DED/$NAME";                        # temporary dest dir (?)


# Make sure expected source directories exist.
@DIRS = ("$MDIR","$TDIR","$SDIR");
push @USERDIRS,(@DIRS,$DED);   # Why build a stack in USERDIRS array? --MJG
foreach $dir (@USERDIRS)
{
  if (! -e $_ || ! -d $dir )
  {
    system "pwd" ;
    if (! -e $_)
    {
      print "pstdistrib: ERROR: no underscore = '$_'\n" ;
      print "  [Explain to user what this means.]\n" ;
    }
    if (! -d $dir )
    {
      print "pstdistrib: ERROR: [source(?)] directory '$dir' does not exist.\n" ;
    }
    die   "  [Suggest a solution.]\n" ;
  }
}


# Create output directories
# [I guess "output" means "temporary destination". --MJG]
$MODE = 0777;
foreach $dir (@DIRS)
{
  $MSTAT=MKDIRS("$DDIR/$dir"     ,$MODE); if ($MSTAT != 0) {exit 1};
  $MSTAT=MKDIRS("$DDIR/$dir/bin" ,$MODE); if ($MSTAT != 0) {exit 1};
}


# Copy files in ~/PST/bin/ , ~/PST/TUSC/
# [to temporary dest dir(?) --MJG]
@FILES=MDFILES();   COPYFILES($UDIR,$MDIR,$DDIR,\@FILES);
@FILES=SETFILES();  COPYFILES($UDIR,$SDIR,$DDIR,\@FILES);
@FILES=TUSCFILES(); COPYFILES($UDIR,$TDIR,$DDIR,\@FILES);


# Tar/compress temp dest dir heirarchy to distrib archive file.
chdir $DED;  # [Change to temporary dest dir(?) --MJG]

$TSTAT = system("tar cvf - $NAME > $NAME.tar");
if ($TSTAT == 0)
{
  print "pstdistrib: $NAME tar successful.\n";
  system("/bin/rm","-R","$NAME")
}
$GSTAT = system("gzip $NAME.tar");
if ($GSTAT == 0)
{
  print "pstdistrib: $NAME gzip successful.\n"
}


#============================================================= COPYFILES
sub COPYFILES
{
  # Extract subroutine arguments.
  my $udir=$_[0];  # user home dir... parent source directory...(?)
  my $edir=$_[1];  # subdirectory common to source and dest...(?)
  my $ddir=$_[2];  # parent destination directory...(?)
  my $h   =$_[3];  # intermediate variable to assign files array (?)
  my @files=@$h;

  my $pwd = `pwd`; # Remember initial dir to return to it later. (?)
  chomp $pwd;

  chdir "$udir/$edir";
  foreach $file (@files)
  {
    if ($file =~ /^bin\//)
    {
      system "cp $file $ddir/$edir/bin"
    }
    else
    {
      system "cp $list $ddir/$edir"
    }
  }
  chdir "$pwd";  # Return to initial dir. (?)

  # [This return value is ignored.  Okay to omit?  --MJG]
  return 0
}


#================================================================ MKDIRS
# [I think this subroutine simply creates a directory and all parents,
# i.e. I think this is the same as "mkdir -p".
# If so, why call this once for "$DDIR/$dir" then again for
# "$DDIR/$dir/bin"?  seems redundant. --MJG]
sub MKDIRS
{
  my $dir =$_[0];
  my $mode=$_[1];

  # I think this line splits "dir" into component subdirs.
  my @dirs = split /\//,$dir;
  if ($dirs[0] eq "")
  {
    shift @dirs;
    $dirs[0]="/$dirs[0]"
  }
  my $pwd = `pwd`;
  chomp $pwd;
  if (! -e $dir)
  {
    foreach (@dirs)
    {
      if (! -e $_) {mkdir $_, $mode}
      chdir $_;
    }
  }
  else
  {
    print "pstdistrib::MKDIRS: $dir already exists\n";
    return 1
  }
  chdir $pwd;
  return 0
}


#============================================================= TUSCFILES
# Why not just assign this array to a variable named TUSCFILES? --MJG
sub TUSCFILES
{
  my @B=(
           "archive.pm"         ,
           "local.pm.erdc"      ,
           "local.pm"           ,
           "bin/test_archive"   ,
        );
  return @B
}


#============================================================== SETFILES
# Why not just assign this array to a variable named SETFILES? --MJG
sub SETFILES
{
  my @B=(
           "Set.pm"             ,
        );
  return @B
}


#=============================================================== MDFILES
# Why not just assign this array to a variable named MDFILES? --MJG
sub MDFILES
{
  my @B=(
           "bin/install"        ,
           "bin/pstinstall"     ,
           "bin/pstinstall2"    ,
           "bin/pstdistrib"     ,
           "bin/whichperl"      ,
        );
  return @B
}
#=================================================================== END

__END__
