#!/bin/csh -f
#>
#>  PACT-DIST - make a tar file of the PACT distribution.
#>              Builds a self-extracting compressed tar file
#>              of the last revision Tag from the CVSROOT source
#>              repository for UNIX distribution.             
#>              
#>  Usage:
#>    pact-dist [-h] 
#>
#>    pact-dist [-c <CVSROOT>] [-s SCSdir] [[-P] | [-b]] [-t] 
#>              [-v | +v | -T]      [[-d] [-l [-x]] [-m]]
#>              [-f] [-p] [-r] [-k] [-Z]
#>  
#> Options: 
#>    -h  Help, prints this message
#>
#>    -c  Use the specified CVS repository (default CVSROOT)
#>    -s  Directory for Source Control System info file
#>
#>    -P  Get ONLY the PACT packages from the source repository.
#>
#>    -t  Get the documents too
#>
#>    -v  Get the distribution as of the given date mm/dd/yy
#>    +v  Get the distribution as of the given date mm/dd/yy_hh:mm:ss
#>    -T  Get the distribution for revision Tag Tyy_mm_dd_hh_mm_ss
#>        (for tag use 'pact-get-tag', 'pact-info -c' or 'pact-info -t')
#>
#>    -d  Prepare a distribution for DOS machines
#>    -l  Prepare a distribution for LLNL
#>    -x  Put the extensions in a separate tar file (with -l only)
#>    -m  Prepare a distribution for Apple Macintosh
#>
#>    -f  Prepare a flat TAR file - for non-UNIX sites
#>    -p  Pure compressed source only
#>
#>    -r  Export to remote source repository
#>
#>    -k  Keep the source directory
#>
#>    -Z  Use compress for compression. Default is gzip.
#>
#  
####################
# Source Version: 2.0
# Software Release #92-0043
#
# include "cpyright.h"
#

# see PCD for the reason for this
set Here = `pwd` ; cd ; set RealHome = `pwd` ; cd $Here ; unset $Here

if ($?USER == 0) then
   if ($?LOGNAME == 0) then
      set USER = "anonymous"
   else
      set USER = $LOGNAME
   endif
endif

set UserDir       = `pwd | sed "s|$RealHome|$home|"`
set Base          = `pwd | sed "s|$RealHome|$home|"`
set LogFile       = $Base/distrib.log
set Manager       = manager
set HelpCode      = $Base/$0
set DName         = ("pact"`./pact-get-tag -d`)
set SCSDir        = "."
set SCSRepository = ""
set SRCDate       = ("-r "`./pact-get-tag`)
set Docs          = FALSE
set PureSource    = FALSE
set WhoIAm        = $USER
set CompRoutine   = "gzip"
set CompTail      = "gz"
set HeaderFile    = "../pact-update"

set DOS  = FALSE
set MAC  = FALSE
set LLNL = FALSE
set FLAT = FALSE
set KEEP = FALSE
set help = FALSE
set Extn = FALSE
set RepositoryExport = FALSE

rm -f $LogFile

set Opt = "$HelpCode:t  $argv"


while ($#argv >= 1)
   switch ($1)
      case -c:
               shift
               set SCSRepository = "-d $1"
               breaksw
      case -d:
               set DOS = TRUE
               breaksw
      case -f:
               set FLAT = TRUE
               breaksw
      case help:
      case -h*:
               set help = TRUE
               breaksw
      case -k:
               set KEEP = TRUE
               breaksw
      case -l:
               set LLNL = TRUE
               breaksw
      case -m:
               set FLAT = TRUE
               set MAC  = TRUE
               breaksw
      case -p:
               set PureSource = TRUE
               breaksw
      case -r:
               set RepositoryExport = TRUE
               breaksw
      case -s:
               shift
               set SCSDir = $1
               breaksw
      case -t:
               set Docs = TRUE
               breaksw
      case -v:
               shift
               set SRCDate = "-D $1"
               set DName   = ("pact"`echo $1 | awk -F'/' '{print $3 "_" $1 "_" $2}'`)
               breaksw
      case +v:
               shift
               set SRCDate = "-D `echo $1 | sed 's|_| |'`"
               set t       = "`echo $1 | sed 's|_|/|'`"
               set DName   = ("pact"`echo $t | awk -F'/' '{print $3 "_" $1 "_" $2}'`)
               breaksw
      case -T:
               shift
               set SRCDate = "-r $1"
               set t       = `echo $1 | sed 's/^[A-Z]//' | sed 's/_[0-9][0-9]$//'`
               set d       = `echo $t | sed 's/_[0-9][0-9]$//' | sed 's/_[0-9][0-9]$//'`
               set DName   = "pact"`echo $d`
	       breaksw
      case -x:
               set Extn = TRUE
               breaksw
      case -Z:
               set CompRoutine = "compress"
               set CompTail    = "Z"
               breaksw
      default:
               echo " "
               echo ">>>>>>>>>>  Illegal option $1  <<<<<<<<<<"
               echo " "
               set help = TRUE
               breaksw
   endsw
   shift
end

groups | grep dpact > /dev/null
if ($status == 1) then
   echo ""
   echo "You must be in the group of PACT developers to run PACT-DIST"
   echo ""
   exit(27)
endif

if ($help == "TRUE") then
   awk '($1 == "#>") {print}' $HelpCode      #Print Usage:
   exit(1)
endif

if (!(-e $SCSDir/pact-scs)) then
   echo " "
   echo "Can't find Source Control System Information in $SCSDir"
   echo " "
   exit(25)
endif

# Source Control System Variables
source $SCSDir/pact-scs

#
# check if repository locked
#
if ($ExistLock == TRUE) then
   echo ""
   echo " *** REPOSITORY IS LOCKED ***"
   echo ""
   CatLock
   echo ""
   echo ""
   exit(26)
endif

#
# lock repository
#
Makelock
echo "  pact-dist in progress - " $USER  `date`>> $SCSLock
echo "" >>& $SCSLock

echo " "
if (-d distribution) then
   echo "Distribution directory exists"
else
   echo "Creating distribution directory"
   echo "Creating distribution directory" >>& $SCSLock
   mkdir distribution
endif

cd distribution

echo " "
if ($DOS == "TRUE") then
   echo "PACT Distribution for DOS platforms"
else if ($MAC == "TRUE") then
   echo "PACT Distribution for MAC platforms"
else
   echo "PACT Distribution for UNIX platforms"
endif
if ($FLAT == "TRUE") then
   echo "   Flat tar file"
endif
if ($LLNL == "TRUE") then
   echo "   LLNL specific material included"
endif
if ($KEEP == "TRUE") then
   echo "   The sources will be kept out"
endif
echo " "

if (-d pact) then
   rm -rf pact
endif

echo "Distribution Log		Host: `uname -n` running `uname`" >>&! $LogFile
echo "User: $WhoIAm		Date: `date`" >>& $LogFile
echo " "  >>& $LogFile
echo "Operation: $Opt "  >>& $LogFile
echo " "  >>& $LogFile


echo "Getting out the sources"
echo "Getting out the sources" >>& $SCSLock
echo "Getting out the sources" >>& $LogFile

set PactList = (`cat ../pact.package`)
mkdir pact
cd pact
echo "$CheckOut  manager $PactList" >>& $LogFile
echo " "  >>& $LogFile
$CheckOut manager $PactList >>& $LogFile
KillZombie

if ($Docs == "TRUE") then
   echo " "  >>& $LogFile
   echo "Getting the documents"
   echo "Getting the documents" >>& $LogFile
   echo "$CheckOut doc" >>& $LogFile
   echo " "  >>& $LogFile
   $CheckOut doc >>& $LogFile
   KillZombie
endif

echo " "  >>& $LogFile
cd ..

#
# release the lock
#
Unlock

# remove the $SCSName sub-directories
echo "Cleaning up the sources"
echo "Cleaning up the sources" >>& $LogFile
set List = `find . -name $ADM -print`
foreach i ($List)
   rm -rf $i
end

cd pact
echo $DName > $Manager/.pact-date


echo Current Directory: `pwd | sed "s|$RealHome|$home|"` >>& $LogFile
echo Manager Directory: $Manager >>& $LogFile

# determine whether local tar supports o option
set TarArgs = cvof
mkdir futmp
cd futmp >>& /dev/null
echo "foo" > tmp.file
tar $TarArgs tmp.tar tmp.file >>& /dev/null
if ($status != 0) then
   set TarArgs = cvf
   set status = 0
endif
cd .. >>& /dev/null
rm -r futmp

# add read to permissions for debugging purposes
find . -exec chmod a+r {} \;

# handle the extensions before later phases get rid of them
if (($Extn == "TRUE") && ($LLNL == "TRUE")) then

   echo "Copying PACT extensions"
   echo "Copying PACT extensions" >>& $LogFile
   mkdir foo
   foreach i (*/applications/extensions/*)
      cp $i foo
   end

   echo "Archiving PACT extensions"
   echo "Archiving PACT extensions" >>& $LogFile
   cd foo
   tar $TarArgs ../pact-ext.tar * >>& $LogFile
   cd ..
   rm -r foo

   echo "Compressing PACT extensions using $CompRoutine"
   echo "Compressing PACT extensions using $CompRoutine" >>& $LogFile
   $CompRoutine pact-ext.tar

   mv pact-ext.tar.$CompTail ../$DName-ext.tar.$CompTail

endif

# remove the local extensions directories
if ($LLNL != "TRUE") then
   find .. -type d -name extensions -exec rm -rf {} \;
endif

# for non export distributions
if ($RepositoryExport == "FALSE") then

# handle the DOS specific material
   if ($DOS == "TRUE") then

      echo "Setting up DOS distribution"
      echo "Setting up DOS distribution" >>& $LogFile
      cd $Manager >>& $LogFile

      set DOS_converter = ""
      set DOS_converter = `./pwhich to_dos`

      mv cpyright.h dos
      rm * >>& $LogFile
      mv dos/* . >>& $LogFile
      foreach dir (dos mac configs.local)
         rm -rf $dir >>& $LogFile
      end
      cd .. >>& $LogFile
      cd ultra
      rm ultra.ini.mac
      mv ultra.ini.user ultra.usr
      cd ..

      if (-d sx/spokes/acf) then
         rm -rf sx/spokes/acf
      endif

# write scvers.c 

      set STDOUT = score/scvers.c

      rm -f $STDOUT
      touch $STDOUT

      echo "/*" >>& $STDOUT
      echo " * SCVERS.C - PACT version" >>& $STDOUT
      echo " *" >>& $STDOUT
      echo " * Source Version: 2.0" >>& $STDOUT
      echo " * Software Release #92-0043" >>& $STDOUT
      echo " *" >>& $STDOUT
      echo " */" >>& $STDOUT
      echo "" >>& $STDOUT

      echo "char" >>& $STDOUT
      set QUOTE = \"
      set THE_DATE = `code-date`
      echo ' SC_version_string[] = '$QUOTE$THE_DATE$QUOTE';' >>& $STDOUT
      echo "" >>& $STDOUT

      rm */pre-Make >>& $LogFile
      rm */*.SunCore >>& $LogFile

      if ($DOS_converter != "") then
         echo "DOS_converter = $DOS_converter"
       else
         echo "No Unix to DOS file converter found--files will not be converted"
       endif

      if ($DOS_converter != "") then
         echo "Converting files to DOS format"
         set Pact_files = `find . -name '*' -print`
         foreach i ($Pact_files)
            if ( -f $i ) then
               $DOS_converter $i $i.dos
               mv $i.dos $i
            endif
         end
      endif

# handle the MAC specific material
   else if ($MAC == "TRUE") then

      echo "Setting up MAC distribution"
      echo "Setting up MAC distribution" >>& $LogFile
      cd $Manager >>& $LogFile

      cp cpyright.h DISCLAIMER ..
      rm * >>& $LogFile
      cp mac/*.mac .
      cp mac/*.gz .
      cp mac/README.MAC .
      cp mac/trconfig.h ../sx
      foreach dir (dos mac configs.local configs.std)
         rm -rf $dir >>& $LogFile
      end
      
      cd .. >>& $LogFile

      echo "Finding required sources for MAC distribution"
      echo "Finding required sources for MAC distribution" >>& $LogFile
      foreach dir (*)
  	 if ((-d $dir) && ($dir != "manager")) then
            cd $dir
            pact inform
	    touch ../manager/source.$dir
	    foreach i (`cat required.objs`)
               echo $i:r.c >> ../manager/source.$dir
            end
            rm required.objs
            cd ..
         endif
      end
      echo " gsdv_qd.o gspr_qd.o" >> manager/source.pgs
      echo " pcnil.o" >> manager/source.ppc

      cd ultra
      rm ultra.ini
      mv ultra.ini.mac ultra.ini
      cd ..
      rm -rf sx/spokes

      rm */pre-Make >>& $LogFile
      rm */*.SunCore >>& $LogFile
      rm */Makefile >>& $LogFile
      rm */pre-make.dos >>& $LogFile
      rm */*.tld >>& $LogFile

# NOTE: write scvers.c since there will be no pact-config done
#       on the MacIntosh

      echo scvers.c >> manager/source.score
      set STDOUT = score/scvers.c

      rm -f $STDOUT
      touch $STDOUT

      echo "/*" >>& $STDOUT
      echo " * SCVERS.C - PACT version" >>& $STDOUT
      echo " *" >>& $STDOUT
      echo " * Source Version: 2.0" >>& $STDOUT
      echo " * Software Release #92-0043" >>& $STDOUT
      echo " *" >>& $STDOUT
      echo " */" >>& $STDOUT
      echo "" >>& $STDOUT

      echo "char" >>& $STDOUT
      set QUOTE = \"
      set THE_DATE = `code-date`
      echo ' SC_version_string[] = '$QUOTE$THE_DATE$QUOTE';' >>& $STDOUT
      echo "" >>& $STDOUT

# handle the UNIX world
   else

      echo "Setting up UNIX distribution"
      echo "Setting up UNIX distribution" >>& $LogFile
      set NoGoDirs = ( "manager/dos" "manager/mac" "scheme/runtime" )
      if ($LLNL != "TRUE") then
         set NoGoDirs = ( $NoGoDirs manager/configs.local )
      endif
      foreach dir ($NoGoDirs)
         rm -rf $dir >>& $LogFile
      end
      rm ultra/ultra.ini.mac >>& $LogFile
      rm */pre-make.dos >>& $LogFile
      rm */*.tld >>& $LogFile

   endif

# handle the LLNL specific material
   if ($LLNL != "TRUE") then

      if (-d sx/spokes) then
         rm -rf sx/spokes/acf
      endif

   endif

else

      echo "Setting up Repository export"
      echo "Setting up Repository export" >>& $LogFile
      set NoGoDirs = ( manager/configs.local sx/spokes/acf )
      foreach dir ($NoGoDirs)
         rm -rf $dir >>& $LogFile
      end

endif

# check to see if the distribution should be flattened
if ($FLAT == "TRUE") then

   echo "Flattening the distribution"
   echo "Flattening the distribution" >>& $LogFile
   foreach dir (*)
     if (-d $dir) then
        if ($dir == "$Manager") then
           mv $dir/* . >>& $LogFile
        else
           mv $dir/*.[chf] . >>& $LogFile
           if (-d $dir/applications) then
              cp $dir/applications/*.scm .
           endif
           if ($dir == "ultra") then
              mv $dir/ultra.ini . >>& $LogFile
              mv $dir/*.scm  . >>& $LogFile
           endif
        endif
        rm -r $dir
     endif
   end

   echo "Archiving the distribution"
   echo "Archiving the distribution" >>& $LogFile
   tar $TarArgs pact.tar * >>& $LogFile
   mv pact.tar ..

   cd ..

else

   cd ..

   echo "Archiving the distribution"
   echo "Archiving the distribution" >>& $LogFile
   if ($DOS == "TRUE") then
      tar $TarArgs pact.tar pact >>& $LogFile
#      find pact -print | cpio -o -cv -O pact.tar >>& $LogFile
   else
      tar $TarArgs pact.tar pact >>& $LogFile
   endif

endif

echo "Compressing the distribution using $CompRoutine"
echo "Compressing the distribution using $CompRoutine" >>& $LogFile
$CompRoutine pact.tar

if ($KEEP == "FALSE") then
   echo "The sources will NOT be kept out"
   echo "The sources will NOT be kept out" >>& $LogFile
   rm -r pact
else
   echo "The sources will be kept out"
   echo "The sources will be kept out" >>& $LogFile
endif

if (($PureSource == "TRUE") || ($RepositoryExport == "TRUE")) then
   mv pact.tar.$CompTail ../$DName.tar.$CompTail

else if ($DOS == "TRUE") then
   mv pact.tar.$CompTail ../$DName-dos.tar.$CompTail

else if ($MAC == "TRUE") then
   mv pact.tar.$CompTail ../$DName-mac.tar.$CompTail

else
   set Signature = "-----------------------***   SEPARATOR   ***-----------------------"

   rm -f pact-src
#   cat ../pact-update > pact-src
   cat $HeaderFile > pact-src
   echo "$Signature" >> pact-src
   echo "pact.tar.$CompTail" >> pact-src
   cat pact.tar.$CompTail >> pact-src
   chmod a+x pact-src
   rm -f pact.tar.$CompTail
   if ($LLNL == "TRUE") then
      mv pact-src ../$DName-llnl-src
   else
      mv pact-src ../$DName-src
   endif

endif

touch tmp
rm -f *  >>& /dev/null

echo ""
echo "See the file $LogFile for more information"
echo ""

exit($status)
