#!/usr/bin/perl
#=============================================================== DISTRIB
=pod

Prepare a PST code distribution.  Collect all routines into a directory
tree.  Tar the result into an archive file whose name includes the date.

   pstdistrib /tmp/werne 

=cut

use Cwd;
#---------------------------------------------------------- Get the time
($min,$hour,$mday,$mon,$year)=(localtime)[1,2,3,4,5];
$year=$year+1900; $mon=$mon+1;
#---------------------------------------------------- Define directories
$DED   = cwd();
$UDIR  = $ENV{'HOME'}; chomp $UDIR;
$RDIR  = "PST";
$MDIR  = "$RDIR/MD";
$TDIR  = "$RDIR/TUSC";
$SDIR  = "$RDIR/SET";
$form  = "PST.%4.4d.%2.2d.%2.2d.%2.2d.%2.2d";
$NAME  = sprintf $form,($year,$mon,$mday,$hour,$min);
$DDIR  = "$DED/$NAME";
#------------------------------------------- Make sure directories exist
@DIRS = ("$MDIR","$TDIR","$SDIR"); push @USERDIRS,(@DIRS,$DED);
foreach $dir (@USERDIRS) {
  if (! -e $_ || ! -d $dir ) { die "$dir directory does not exist\n" }
}
#--------------------------------------------- Create output directories
$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/
#
@FILES=MDFILES();   COPYFILES($UDIR,$MDIR,$DDIR,\@FILES);
@FILES=SETFILES();  COPYFILES($UDIR,$SDIR,$DDIR,\@FILES);
@FILES=TUSCFILES(); COPYFILES($UDIR,$TDIR,$DDIR,\@FILES);
#------------------------------------------------------------- Tar files
chdir $DED;
$TSTAT = system("tar cvf - $NAME > $NAME.tar");
if ($TSTAT == 0) {
  print "$NAME tar successful\n";
  system("/bin/rm","-R","$NAME")
}
$GSTAT = system("gzip $NAME.tar");
if ($GSTAT == 0) {print "$NAME gzip successful\n"}

#============================================================= COPYFILES
sub COPYFILES{
  my $udir=$_[0]; 
  my $edir=$_[1]; 
  my $ddir=$_[2]; 
  my $h   =$_[3]; 
  my @files=@$h;
  my $pwd = `pwd`; 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 0
}
#================================================================ MKDIRS
sub MKDIRS{
  my $dir =$_[0]; 
  my $mode=$_[1];
  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 "$dir already exists\n"; return 1 }
  chdir $pwd;
  return 0
}
#============================================================= TUSCFILES
sub TUSCFILES{
  my @B=(
           "archive.pm"         ,
           "local.pm.erdc"      ,
           "local.pm"           ,
           "bin/test_archive"   ,
        );
  return @B
}
#============================================================== SETFILES
sub SETFILES{
  my @B=(
           "Set.pm"             ,
        );
  return @B
}
#=============================================================== MDFILES
sub MDFILES{
  my @B=(
           "bin/install"        ,
           "bin/pstinstall"     ,
           "bin/pstinstall2"    ,
           "bin/pstdistrib"     ,
           "bin/whichperl"      ,
        );
  return @B
}
#=================================================================== END

__END__
