#!/bin/sh
# Archives all required ELM source code to tar archives in two locations: 
# an uncompressed one in $ELM_HOME, and a compressed one in a remote directory.
# Used for ELM-version distributions.

# It uses a SFWMD-specific, hard-coded path for the remote archive (change "MyLocation").
# This is a "back-up" tool to the CVS versioning system
#
# Used as a stand-alone script
#
# Calls the following ELM scripts:
# PathELM_HOME

# As with all ELM scripts, stay with Bourne shell (/bin/sh) for universal application.


if test $# = 0
  then
    echo "###### Error: give the NameVersion of the Project code. "
    exit
fi
if test $# = 1
  then
    echo "###### Error: give the Name/date to designate the second backup source archive. "
    exit
fi

ProjName=$1
ArchName=$2

# set where you want to save the compressed backup
MyLocation=/e_evg/eb_elmmod/src/arc_src/

####
#### ELM_HOME Path
# determines if the (fundamental) ELM_HOME environment variable appears valid
PathELM_HOME

######
# ensure you don't overwrite an old compressed backup
if
    test -f $MyLocation/$ProjName.src.$ArchName.tar.gz
then
    echo "The backup tar file already exists: "
    echo "$MyLocation/$ProjName.src.$ArchName.tar.gz"
    echo "Choose another NameVersion and/or Name/date to designate the second backup."
    exit
fi

# the tar is made with relative paths for portability to any file system
cd $ELM_HOME

# open a file to provide notes on the changes to the sources - MAKE THE NOTES USEFUL!!!!
vi  ./$ProjName.src.$ArchName.Notes.txt
cat ./$ProjName.srcNotes_* 	./$ProjName.src.$ArchName.Notes.txt > ./tempNotes
rm  ./$ProjName.srcNotes_*
mv  ./tempNotes 	./$ProjName.srcNotes_$ArchName


# tar every source (C, shell scripts) required to install an ELM project (and no junk sources)
# the tar file for a given ProjName is routinely replaced
tar -cvf ./$ProjName.src.tar  \
./SME/SMDriver/Sources/Driver_Sources/Generic_Driver.c \
./SME/SMDriver/Sources/Driver_Sources/Driver_Utilities.c  \
./SME/SMDriver/Sources/Driver_Sources/Driver.make \
./SME/SMDriver/Sources/SpatMod/Fluxes.c \
./SME/SMDriver/Sources/SpatMod/Success.c  \
./SME/SMDriver/Sources/SpatMod/WatMgmt.c  \
./SME/SMDriver/Sources/SpatMod/evap_inp.c \
./SME/SMDriver/Sources/SpatMod/rain_inp.c \
./SME/SMDriver/Sources/SpatMod/stage_inp.c \
./SME/SMDriver/Sources/Tools/grid_io.c \
./SME/SMDriver/Sources/Tools/gridmap.c \
./SME/SMDriver/Sources/Tools/MultiRun.c \
./SME/SMDriver/Sources/Tools/Serial.c \
./SME/SMDriver/Sources/UnitMod/BudgStats.c  \
./SME/SMDriver/Sources/UnitMod/UnitMod.c \
./SME/scripts/aaELM_setenv_instruct_notScript.txt  \
./SME/scripts/ArchiveData  \
./SME/scripts/ArchiveRun  \
./SME/scripts/ArchiveSrc \
./SME/scripts/build \
./SME/scripts/Check \
./SME/scripts/CopyInput  \
./SME/scripts/ELMinstall.sh  \
./SME/scripts/finishOutList  \
./SME/scripts/getDSSflow  \
./SME/scripts/go \
./SME/scripts/mkOutDirs  \
./SME/scripts/PathArchive  \
./SME/scripts/PathELM_HOME  \
./SME/scripts/PathModel  \
./SME/scripts/PathOSTYPE  \
./SME/scripts/PathOutput  \
./SME/scripts/PatternOutStep \
./SME/scripts/PatternRunTime \
./SME/scripts/README.txt  \
./SME/scripts/rmAnim \
./SME/scripts/Run  \
./SME/scripts/StrNames  \
./include/sme/budgstats.h  \
./include/sme/budgstats_birvars.h  \
./include/sme/budgstats_statvars.h  \
./include/sme/driver_utilities.h  \
./include/sme/evap.h  \
./include/sme/fluxes.h  \
./include/sme/generic_driver.h  \
./include/sme/globals.h  \
./include/sme/grid_io.h  \
./include/sme/gridmap.h  \
./include/sme/multi_run.h  \
./include/sme/rain.h  \
./include/sme/serial.h  \
./include/sme/stage.h  \
./include/sme/success.h  \
./include/sme/unitmod.h  \
./include/sme/unitmod_globparms.h  \
./include/sme/unitmod_habparms.h  \
./include/sme/unitmod_vars.h  \
./include/sme/watmgmt.h  \
./gridmap/*.cpp \
./gridmap/*.h \
./gridmap/*.cfg \
./gridmap/Makefile \
./gridmap/doc/gridmap.doc  


# end of the sources put in the tar file

echo "Making a uniquely-named copy of the archive..."
cp -p ./$ProjName.src.tar ./$ProjName.src.$ArchName.tar
echo "Compressing the uniquely-named copy of the archive..."
gzip ./$ProjName.src.$ArchName.tar 
echo "Moving that compressed archive..."
mv   ./$ProjName.src.$ArchName.tar.gz    $MyLocation/
mv   ./$ProjName.src.$ArchName.Notes.txt $MyLocation/

if test -f "$MyLocation/$ProjName.src.$ArchName.tar.gz"
  then
	echo "Successful archive of sources from $ELM_HOME/"
	echo "This archived all required source code (incl. scripts) for ELM, making two tar archives."
	echo "One is an uncompressed tar file that gets routinely replaced in the ELM_HOME directory:"
	echo "$ELM_HOME/$ProjName.src.tar"
	echo "The 2nd is a gzipped tar file put on the SFWMD ha1-fs1 file system with a unique name:"
	echo "$MyLocation/$ProjName.src.$ArchName.tar.gz"
  else
	echo "###### Error:  Something went wrong with trying to create"
	echo "$MyLocation/$ProjName.src.$ArchName.tar.gz"
fi

  
