#!/bin/csh

# makeframework.csh Version 1 Jan. 2024

# >>>> TEMPORARY FIX THAT ELIMINATES nice COMMANDS

# Create BIRCH framework file framework.tar.gz
# Install it in the FTP directory.
# Think of this script as a camera that, at any time,
# can take a snapshot of the current BIRCH system,
# which can be re-created on any other Unix system.

# makeframework.csh     makes complete BIRCH framework
#                   -m              makes a mini BIRCH framework for 
#                                   teaching purposes
#                   -v  version     set version. 
#                                   Otherwise, version is Development Version

#A number of directories in the BIRCH hierarchy contain symbolic
#links to other directories. When archiving with 'tar', the links
#are replaced by a copy of each directory. This is especially
#a problem when big directories such as bin or GenBank are linked to.
#Therefore, makeframework.csh runs tar using the 'X' option, 
#reading a list of files or directories that should NOT be 
#copied from 'makeframework.excludefile'.	
		
#=======  GLOBAL VARIABLES ==============
set BUILDDIR = /home/psgendb/BIRCHDEV/build
set FTPDIR = /var/spool/ftp/psgendb/BIRCH

set BUILDTYPE = 'complete'
#set TIMESTAMP = `date | tr " " "_"`
set VERSION = "D"


#####################################################################
# Read options from the command line and set parameters,
####################################################################
set numargs = $#argv
if ($numargs != 0) then
  #---------------------------------------------------
  #parameters given in command line
  set files = ""
  set mode  = command
  set index = 1
  while ($index <= $numargs) 
   set a = $argv[$index]
   switch ($a)
     case "-m":
       set BUILDTYPE = 'mini'
       breaksw
     case "-v":
       @ index++
       set VERSION = $argv[$index] 
       breaksw
     default:
       echo '>>> Invalid option '$argv[1]
       echo 'Usage: makeframework.csh [-m] [-v version]'
       echo
       echo '>>> Exiting program' 
       exit        
       breaksw
     endsw
     @ index++
  end #while

endif

if ($VERSION == "D") then
   set TARGETDIR = $FTPDIR/Development
else
   set TARGETDIR = $FTPDIR/$VERSION
   if ( ! -d $TARGETDIR ) then
     mkdir $TARGETDIR
     chmod a+rx $TARGETDIR
     # getbirch needs a VERSION file to tell it which version is in the target directory
      echo "$VERSION" > $TARGETDIR/VERSION
     chmod a+r $TARGETDIR/VERSION
  else
    echo '>>> '"$TARGETDIR already exists. Aborting makeframework."
     exit
   endif
endif

# ------------------------------------------------------------
#  Write version to:
#      BIRCH.version
#      local-generic/admin/Login_Message
#      local-generic/doc/WHATSNEW.html
#      install-scripts
echo $VERSION > ../install-scripts/BIRCH.version
chmod a+r ../install-scripts/BIRCH.version
echo `java jstamp` > ../install-scripts/BIRCH.version.timestamp
chmod a+r ../install-scripts/BIRCH.version.timestamp
cp -p ../install-scripts/BIRCH.version.timestamp $TARGETDIR/TIMESTAMP
echo `java jstamp` > ../install-scripts/BIRCH.version.timestamp
chmod a+r ../install-scripts/BIRCH.version.timestamp
cp -p ../install-scripts/BIRCH.version.timestamp $TARGETDIR/TIMESTAMP

set RAWDATE = `date | tr -d ','`
set DAY = $RAWDATE[3]
set MONTH = $RAWDATE[2]
#set YEAR = `echo $RAWDATE[6] | cut -c3-4`
set YEAR = `echo $RAWDATE[4] | cut -c3-4`
set TIMESTAMP = "$DAY $MONTH $YEAR"


if ( $VERSION == D ) then
   set MESSAGE = "$TIMESTAMP The current version of BIRCH is today's Development version"
else
   set MESSAGE = "$TIMESTAMP The current version of BIRCH is $VERSION"
endif
echo $MESSAGE > templates/message.txt
cd templates
cat Login_Message.head message.txt Login_Message.tail > ../../local-generic/admin/Login_Message
cat WHATSNEW.html.head message.txt WHATSNEW.html.tail > ../../local-generic/doc/WHATSNEW.html




# Add a file to local-generic/admin telling whether this is a complete
# or a mini-BIRCH framework file.
if ( $BUILDTYPE == 'complete') then
   echo 'false' > ../../local-generic/admin/BIRCH.mini
else
   echo 'true' > ../../local-generic/admin/BIRCH.mini
endif

cd ..
#Getbirch now expects that there is a copy of UNINSTALL-birch.py in $TARGETDIR
cp -p ../install-scripts/UNINSTALL-birch.py $TARGETDIR

# pause, so that we can see the initial messages before tarring begins
sleep 5


# -------------------------------------------------------------
# Write the .tar.gz files to $TARGETDIR

if ($BUILDTYPE == 'complete') then
      set INCLUDEFILE = "makeframework.includefile"
      set EXCLUDEFILE = "makeframework.excludefile"
      set FRAMEWORK = "framework_"$VERSION
else
      set INCLUDEFILE = "mini.includefile"
      set EXCLUDEFILE = "mini.excludefile"
      set FRAMEWORK = "framework.mini_"$VERSION
endif
echo "FRAMEWORK: "$FRAMEWORK

# Write tar archive to TARGETDIR
cd .. 

# The line below is for the standard Unix tar eg. Solaris
#nice +10 tar cvpfX $TARGETDIR/$FRAMEWORK.tar $BUILDDIR/$EXCLUDEFILE -I $BUILDDIR/$INCLUDEFILE

# The line below is for GNU tar, gtar, which is available both on Solaris and Linux
gtar cvpfX $TARGETDIR/$FRAMEWORK.tar $BUILDDIR/$EXCLUDEFILE --files-from=$BUILDDIR/$INCLUDEFILE
set success = $status
if ($success == 0) then
  echo tar succeeded
else
   echo '!!! tar xvfp ' $TARGETDIR/$FRAMEWORK.tar ' failed' 
endif  


cd $TARGETDIR
#gzip -f $FRAMEWORK.tar
pigz -f $FRAMEWORK.tar
chmod a+r $FRAMEWORK.tar.gz

# Copy the development binaries to new files for the stable binaries
if ( ( $BUILDTYPE == 'complete' ) && ( $VERSION != 'D' ) ) then  
    cd $FTPDIR/Development  
    cp -p bin-linux-x86_64_D.tar.gz $TARGETDIR/bin-linux-x86_64_$VERSION.tar.gz      
    cp -p bin-osx-x86_64_D.tar.gz $TARGETDIR/bin-osx-x86_64_$VERSION.tar.gz
    cp -p bin-macos-arm64_D.tar.gz $TARGETDIR/bin-macos-arm64_$VERSION.tar.gz
endif


