#!/bin/tcsh -f
#------------------------------------------------------------------------------
#  assumptions: 
#  ------------
#  1. current directory is the top-most directory of the test release
#  2. 
#
#  call : create_tarball.csh version[:patch] [optmode]
#
#  "patch" is used only to label the tarball, no extra checkouts 
#
#  examples: 
#             create_tarball.csh 4.10.4:e  . 
#             create_tarball.csh 5.1.0pre9 maxopt
#
#  to create private (maxopt) version ProductionExe_5.3.1pasha_maxopt.tar.gz
#
#             create_tarball.csh 5.3.1:pasha maxopt
#------------------------------------------------------------------------------
if ($1 == '') then
    echo "usage:   create_tarball.csh tag "
    echo "         tag = {version}{patch}"
    echo "         version - version of the frozen release"
    echo "         patch   - version of the patch"
    echo "example: create_tarball.csh 3.15.0int3 maxopt"
    echo "         will create tarball ProductionExe_3.15.0int3_maxopt.tar.gz"
    exit
endif


source ~cdfsoft/cdf2.cshrc

set WORK_DIR = $PWD
set version  = `echo $1 | awk -F : '{print $1}'`
set patch    = `echo $1 | awk -F : '{print $2}'`
set tag      = ${version}${patch}
set optmode  = default
set caf      =

if ( .$2 != "." ) then
  if ( .$2 != ".." ) set optmode=$2
endif

if ( .$3 != "."                ) set     caf=$3

set label = ${tag}
if ( .$optmode != ".default" ) set label=${label}_${optmode}
# if ( .$caf     != "."        ) set label=${label}_${caf}

set v = `cat ./.base_release`
if ( $v != $version ) then
  echo inconsistent version of offline: $v != $version
endif
#-----------------------------------------------------------------------
#  the name of the tarball and the name of the executable will 
#  diverge soon - prepare for that (today they are the same)
#-----------------------------------------------------------------------
echo "########################################"
echo "Going to create tarball for "$tag" release"
echo "########################################"
echo "Setup cdfsoft2 " $version optimization = $optmode

setup cdfsoft2  $version
srt_setup -a SRT_QUAL=$optmode

# if ( .$caf == ".caf" ) then
  setenv  PRODUCTION_EXE    bin/${BFARCH}/ProductionExe
  setenv  CONCATENATION_EXE bin/${BFARCH}/AC++Dump
# else
#   setenv  PRODUCTION_EXE    ./ProductionExe_$label
#   setenv  CONCATENATION_EXE ./AC++Dump
# endif

set               tarball=${WORK_DIR}/ProductionExe_$label.tar
set              test_tcl=./ProductionExe_$label.tcl
#-----------------------------------------------------------------------
set    production_exe=bin/${BFARCH}/ProductionExe
set concatenation_exe=bin/${BFARCH}/AC++Dump

if ( ! -e $production_exe ) then 
  make Production._prod
  set rc = $?
  if ( $rc != 0 ) then
    echo "$production_exe did not compile. Aborting."
    exit
  endif
endif

echo $production_exe : OK

if ( ! -f $concatenation_exe ) then
  if ( ! -d FrameMods ) addpkg FrameMods

  make  FrameMods.bin
  if ($? != 0) then
    echo "$concatenation_exe did not compile. Aborting."
    exit
  endif
endif

echo $concatenation_exe : OK
#------------------------------------------------------------------------------
#  start building the tarball 
#------------------------------------------------------------------------------
echo .... checkpoint 1
if ( $production_exe != $PRODUCTION_EXE ) then
  cp $production_exe     $PRODUCTION_EXE
  cp $concatenation_exe  $CONCATENATION_EXE
endif
cp ${CDFSOFT2_DIR}/bin/${BFARCH}/DFCFarmTool ${WORK_DIR}/bin/${BFARCH}/
cp ${CDFSOFT2_DIR}/bin/${BFARCH}/DFCFileTool ${WORK_DIR}/bin/${BFARCH}/
cp ${CDFSOFT2_DIR}/bin/${BFARCH}/Edm_DescribeFile ${WORK_DIR}/bin/${BFARCH}/

#-----------------------------------------------------------------------
#  start building the tarball
#-----------------------------------------------------------------------
echo .... checkpoint 2
cd $WORK_DIR
tar -chf $tarball $PRODUCTION_EXE $CONCATENATION_EXE .base_release GNUmakefile
tar -rhf $tarball Production/*.tcl Production/strip_event.csh cdfopr
tar -rhf $tarball Production/farm_concatenation.csh
tar -rhf $tarball bin/${BFARCH}/DFCFarmTool
tar -rhf $tarball bin/${BFARCH}/DFCFileTool
tar -rhf $tarball bin/${BFARCH}/Edm_DescribeFile
tar -rhf $tarball TclUtils/scripts/*.tcl

# set symlinks for the libraries

ln -s /usr/lib/libreadline.so.4 lib/libreadline.so.3
ln -s /usr/lib/libhistory.so.4  lib/libhistory.so.3
ln -s /usr/lib/libncurses.so.5  lib/libncurses.so.4

echo .... checkpoint 3
tar -rf $tarball lib/libreadline.so.3 lib/libhistory.so.3 lib/libncurses.so.4
rm lib/libreadline.so.3 lib/libhistory.so.3 lib/libncurses.so.4
#------------------------------------------------------------------------------
# do it only for KCC
#------------------------------------------------------------------------------
if ( .`echo $BFARCH | grep KCC` != "." ) then
  echo tarring up KAI libraries from ${KCC_BASE}/lib
  cd $KCC_BASE ; tar -hrf $tarball lib/libKCC.so
endif


echo tarring up Oracle libraries from ${ORACLE_HOME}/lib
set oracle_version = `echo $SETUP_ORACLE_CLIENT | awk '{ print $2 }' | cut -d "_" -f1 `

if ( ${oracle_version} == "v9" ) then
    cd $ORACLE_HOME ; tar -hrf $tarball lib/libclntsh.so.9.0 lib/libwtc9.so
endif 

if ( ${oracle_version} == "v8" ) then
    cd $ORACLE_HOME ; tar -hrf $tarball lib/libclntsh.so.8.0 lib/libwtc8.so
endif 

echo tarring up ROOT libraries and include files from ${ROOTSYS}

cd $ROOTSYS
tar -rhf $tarball lib/libNew.so lib/libCore.so lib/libRint.so lib/libCint.so \
                  lib/libDCache.so lib/libGraf.so lib/libGraf3d.so lib/libHist.so \
                  lib/libHtml.so lib/libMatrix.so lib/libMinuit.so \
                  lib/libPostscript.so lib/libProof.so lib/libTree.so lib/libEG.so \
                  lib/libPhysics.so lib/libGpad.so lib/libGui.so lib/libGX11.so \
                  lib/libTreeViewer.so lib/libX3d.so \
                  cint/include/bool.h cint/MAKEINFO include/RQ_OBJECT.h
#-----------------------------------------------------------------------
#  copy DCACHE libs
#-----------------------------------------------------------------------
echo tarring up DCACHE libs
if ( `env | grep DISKCACHE_I_DIR` !="" ) then
  cd $DISKCACHE_I_DIR ; tar -hrf $tarball lib/*.so
endif

if ( `env | grep DCAP_DIR` !="" ) then
  cd $DCAP_DIR ; tar -hrf $tarball lib/libdcap.so ;
endif
#-----------------------------------------------------------------------
#  copy FRONTIER libs
#-----------------------------------------------------------------------
echo tarring up FRONTIER libs
if ( `env | grep FRONTIER_CLIENT_DIR` !="" ) then
  cd $FRONTIER_CLIENT_DIR ; tar -hrf $tarball lib/*.so*
endif
#-----------------------------------------------------------------------
#  copy .cafrc 
#-----------------------------------------------------------------------
if ( -e $PWD/.cafrc ) then
  echo tarring up $PWD/.cafrc
  tar -hrf $tarball .cafrc
else if ( -e $HOME/.cafrc ) then
  echo tarring up $HOME/.cafrc
  cd $HOME ; tar -hrf $tarball .cafrc
endif

#-----------------------------------------------------------------------
# copy scattered database-type files into one directory
#-----------------------------------------------------------------------
set build_dir = $WORK_DIR/tmp/scratch

mkdir -p ${build_dir}/dbt
mkdir -p ${build_dir}/CLHEP/HepPDT/data
mkdir -p ${build_dir}/DBManager/farmsonly

cp $CDFGEOM                                    ${build_dir}/dbt/geom99.dab
cp $TNS_ADMIN/tnsnames.ora                     ${build_dir}/dbt/tnsnames.ora
cp $BFIELDM                                    ${build_dir}/dbt/bfieldmap.dbt
cp $HEPTBL                                     ${build_dir}/dbt/heptabl.dbt
cp $PARTICLEDB_DIR/src/partprop.dbt            ${build_dir}/dbt/partprop.dbt
cp $PROJECT_DIR/CLHEP/HepPDT/data/pdg_mass.tbl ${build_dir}/CLHEP/HepPDT/data/.
#------------------------------------------------------------------------------
# iomap.txt: this file should be used for farms executables:
#------------------------------------------------------------------------------
echo " creating iomap file specific for farms use "
echo " ------------------------------------------ "
cp $DBMANAGER_DIR/iomap_cdfofprd_direct.txt    ${build_dir}/DBManager/farmsonly/

cd $build_dir
tar -rhf $tarball *

#
echo " tarring up RCP database "
echo " -------------------- "
cd $BFDIST
tar -rhf $tarball rcpdb 
#-----------------------------------------------------------------------
# Creating source_file
#-----------------------------------------------------------------------
cd $WORK_DIR
echo 'setenv WORK_DIR            $PWD'                   >  source_me
echo 'setenv PROJECT_DIR         $PWD'                   >> source_me
echo "setenv BFARCH              $BFARCH"                >> source_me
echo "setenv SRT_BASE_RELEASE    $SRT_BASE_RELEASE"      >> source_me
echo "setenv SRT_PRIVATE_CONTEXT $PWD"                   >> source_me
echo "setenv PRODUCTION_EXE      $PRODUCTION_EXE"        >> source_me
echo "setenv CONCATENATION_EXE   $CONCATENATION_EXE"     >> source_me
echo "setenv EXEFILE             $PRODUCTION_EXE"        >> source_me
echo 'setenv LD_LIBRARY_PATH     $PWD/lib'               >> source_me
echo 'setenv LD_LIBRARY_PATH     $PWD/lib'               >> source_me
echo 'setenv BFIELDM             $PWD/dbt/bfieldmap.dbt' >> source_me
echo 'setenv CDFGEOM             $PWD/dbt/geom99.dab'    >> source_me
echo 'setenv ROOTSYS             $PWD'                   >> source_me
echo 'setenv TNS_ADMIN           $PWD/dbt'               >> source_me
echo 'setenv RCP_SCRIPT_BASE     $PWD/rcpdb'             >> source_me
echo 'setenv RCP_FILESYSTEMDB_READONLY_DIR $PWD/rcpdb'  >> source_me
echo 'setenv RCP_DB_NAMES_FILE   $PWD/rcpdb/dbnames.txt' >> source_me
echo 'setenv RCP_DATABASE_PATH   level3/FileSystemDB:official/FileSystemDB/' >> source_me
echo "setenv TCL_TAG             $TCL_TAG"               >> source_me
echo 'setenv HEPTBL              $PWD/dbt/heptabl.dbt'   >> source_me
echo 'setenv CDFPARP             $PWD/dbt/partprop.dbt'  >> source_me
echo 'setenv IOMAP_FILE $PWD/DBManager/farmsonly/iomap_cdfofprd_direct.txt' >> source_me

cat source_me
tar -rf $tarball ./source_me
#------------------------------------------------------------------------------
#  quick test that ProductionExe runs at all
#------------------------------------------------------------------------------
echo "# Running an essentiality test"
mkdir -p $WORK_DIR/tmp/test
cd $WORK_DIR/tmp/test
tar -xf $tarball
source source_me

#setenv DEBUG xx
echo "exit"       >    $test_tcl
#------------------------------------------------------------------------------
#  so far we're in $WORK_DIR/tmp/test subdirectory, run test there
#------------------------------------------------------------------------------
$PRODUCTION_EXE $test_tcl > /dev/null
set rc = $?
if ($rc != 0) then
  echo "$PRODUCTION_EXE did not run sucessfully."
  exit -1
else
  echo "$PRODUCTION_EXE run sucessfully."
endif

cd $WORK_DIR
rm -rf $WORK_DIR/tmp/test $WORK_DIR/tmp/scratch

if ($rc == 0) then 
#------------------------------------------------------------------------------
# ProductionExe runs, gzip the tarball
#------------------------------------------------------------------------------
  echo "# everything is OK, gzipping the tarball"
  gzip    $tarball
endif
#------------------------------------------------------------------------------
# now the current directory is again the topmost dir of the test release
#------------------------------------------------------------------------------
if ( $rc != 0) exit $rc
