#!/bin/csh

# makeframework.csh Version 19 Oct 2011

# 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/test

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

echo 'VERSION: '$VERSION

# ------------------------------------------------------------
#  Write version to:
#      BIRCH.version
#      local-generic/admin/Login_Message
#      local-generic/doc/WHATSNEW.html

echo $VERSION > ../install-birch/BIRCH.version
chmod a+r ../install-birch/BIRCH.version
# echo `date` > ../install-birch/BIRCH.version.timestamp
echo `java jstamp` > ../install-scripts/BIRCH.version.timestamp
chmod a+r ../install-birch/BIRCH.version.timestamp
cp -p ../install-birch/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 ..

#exit
# -------------------------------------------------------------
# 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
nice +10 gtar cvpfX $TARGETDIR/$FRAMEWORK.tar $BUILDDIR/$EXCLUDEFILE --files-from=$BUILDDIR/$INCLUDEFILE

cd $TARGETDIR
nice +10 gzip -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  
    nice cp -p bin-linux-intel_D.tar.gz $TARGETDIR/bin-linux-intel_$VERSION.tar.gz
    nice cp -p bin-linux-x86_64_D.tar.gz $TARGETDIR/bin-linux-x86_64_$VERSION.tar.gz    
    nice cp -p bin-solaris-amd64_D.tar.gz $TARGETDIR/bin-solaris-amd64_$VERSION.tar.gz
    nice cp -p bin-solaris-sparc_D.tar.gz $TARGETDIR/bin-solaris-sparc_$VERSION.tar.gz    
    nice cp -p bin-osx-x86_64_D.tar.gz $TARGETDIR/bin-osx-x86_64_$VERSION.tar.gz
endif


