#!/bin/sh
#
# Make a tar compressed version of Mn_Fit
# Can make tar for executable, source, manual or tutorial.
#
DIRSAV=`pwd`
MACHINE=`uname -s`
[ -z "$TMPDIR" ] && TMPDIR=/tmp
#
MN_FIT_TOP=${HOME}/mn_fit
#
TGKS='x'
#
# Check for arguments
#
if [ $# -lt 1 ]; then
  echo "Usage: mn_fit_tar what version"
  echo "You must say what you want to tar:"
  echo "  s[ource]      vnum for the source files"
  echo "  e[xecutable]  vnum for the executable"
  echo "  m[anual]      vnum for the manual"
  echo "  t[utorial]    for the tutorial"
  exit
fi
#
# Process the arguments
#
while [ "$#" -gt 0 ]; do
  case $1 in
    s*|S*)
      TARNAME="src"
      ;;
    m*|M*)
      TARNAME="manual"
      ;;
    e*|E*)
      TARNAME="exe"
      ;;
    t*|T*)
      TARNAME="tutorial"
      ;;
    v*)
      VERS=$1
      ;;
  esac
  shift
done
#
if [ -z "$TARNAME" ]; then
  echo "You did not give any valid files to tar"
  exit
fi
echo "Current directory: `pwd`"
echo "Files to tar:      <$TARNAME>"
echo "Version to tar:    <$VERS>"
#
# Set CVS repository for source
#
case $TARNAME in
  src)
    if [ -z "$VERS" ]; then
      echo "You must give the version number"
      exit 1
    fi
    CVSREPOSITORY=${HOME}/CVS
    if [ -n "$MN_FIT_CVS" ]; then
      CVSREPOSITORY=$MN_FIT_CVS
    elif [ ! -d "$CVSREPOSITORY" ]; then
      echo "CVS repository $CVSREPOSITORY does not exist"
      exit 1
    fi
    ;;
  exe)
    if [ -z "$VERS" ]; then
      echo "You must give the version number"
      exit 1
    fi
    ;;
  manual)
    if [ -z "$VERS" ]; then
      echo "You must give the version number"
      exit 1
    fi
    ;;
esac
#
# Tar the file
#
case $TARNAME in
  src)
    echo "Exporting version ${VERS} of Mn_Fit source"
    MN_FIT_DIR="$TMPDIR"/mn_fit_${VERS}
    echo "Mn_Fit directory:  <$MN_FIT_DIR>"
    TARFILE="$TMPDIR"/mn_fit_${VERS}.tar
    cd "$TMPDIR"
    test -d $MN_FIT_DIR && \rm -r $MN_FIT_DIR
    test -d $MN_FIT_DIR || mkdir $MN_FIT_DIR
    cvs -d ${CVSREPOSITORY} export -r ${VERS} -d mn_fit_${VERS} mn_fit
    tar cvf $TARFILE mn_fit_${VERS}
    cd $MN_FIT_DIR
    sed  -e '2 s/Name: //' \
        -e '2 s/\$//' -e '2 s/ \$$//' \
	mn_fit_dbase >$MN_FIT_TOP/tar/mn_fit.dbase
    cp README INSTALL INSTALL.unix INSTALL.vms $MN_FIT_TOP/tar/
    ;;
  exe)
    if [ ! -d `uname -s` ]; then
      echo "You do not appear to be at the top level of the Mn_Fit tree"
      exit 1
    fi
    cd ..
    if [ ! -d mn_fit_${VERS} ]; then
      echo "Mn_Fit is not in the directory mn_fit_${VERS}"
      exit 1
    fi
    echo "Exporting version ${VERS} of Mn_Fit executable"
    TARFILE="$TMPDIR"/mn_fit_${TGKS}.exe.${MACHINE}-`uname -r`_gcc-`gcc -dumpversion`_${VERS}.tar
    MNDIR1=mn_fit_${VERS}
    MNDIR2=${MNDIR1}/${MACHINE}
    MNEXE=${MNDIR2}/exe/mn_fit_$TGKS.exe
    MNMAIN=${MNDIR2}/obj/programs/mn_main_k.o
    if [ -f ${MNDIR2}/exe/mn_root_$TGKS.exe ]; then
      MNEXE="$MNEXE ${MNDIR2}/exe/mn_root_$TGKS.exe"
      MNMAIN="$MNMAIN ${MNDIR2}/obj/programs/mn_main_cxx.o"
    fi
    tar cvf $TARFILE \
     ${MNDIR1}/Makefile ${MNDIR1}/Makefile.user \
     ${MNDIR1}/src/bin/mn_fit \
     ${MNEXE} \
     ${MNMAIN} \
     ${MNDIR2}/lib/lib*.a \
     ${MNDIR1}/inc \
     ${MNDIR2}/help \
     ${MNDIR1}/README ${MNDIR1}/INSTALL ${MNDIR1}/INSTALL.* \
     ${MNDIR1}/AUTHORS ${MNDIR1}/COPYING ${MNDIR1}/COPYRIGHT ${MNDIR1}/NEWS \
     ${MNDIR1}/doc/intro.txt ${MNDIR1}/doc/mn_news.txt \
     ${MNDIR1}/doc/testing.txt ${MNDIR1}/doc/todo.txt \
     ${MNDIR1}/doc/ChangeLog \
     ${MNDIR1}/test/*.mnf ${MNDIR1}/test/*.mnd \
     ${MNDIR1}/test/*.for ${MNDIR1}/test/*.fpp \
     ${MNDIR2}/test_data
    ;;
  manual)
    if ! [ -d manual ]; then
      echo "You do not appear to be at the top level of the Mn_Fit tree"
      exit 1
    fi
    #cd ..
    #if ! [ -d mn_fit_${VERS} ]; then
    #  echo "Mn_Fit is not in the directory mn_fit_${VERS}"
    #  exit 1
    #fi
    TARFILE="$TMPDIR"/mn_manual_${VERS}.tar
    #MNDIR1=mn_fit_${VERS}
    MNDIR1=.
    #gunzip ${MNDIR1}/manual/mn_fit*.ps.gz
    tar cvf $TARFILE \
     ${MNDIR1}/manual/mn_fit*.ps ${MNDIR1}/manual/mn_fit*.pdf \
     ${MNDIR1}/manual/mn_fit_html
    #gzip ${MNDIR1}/manual/mn_fit*.ps
    ;;
  tutorial)
    if ! [ -d tutorial ]; then
      echo "You do not appear to be at the top level of the Mn_Fit tree"
      exit 1
    fi
    TARFILE="$TMPDIR"/mn_tutorial.tar
    MNDIR1=.
    cp ${MNDIR1}/${MACHINE}/test_data/hbook_example.his tutorial/
    tar cvf $TARFILE \
     tutorial/mn_tutorial.ps tutorial/mn_tutorial.pdf \
     tutorial/README tutorial/hbook_example.his \
     tutorial/*.mnf tutorial/*.f tutorial/*.mnd
    rm tutorial/hbook_example.his
    ;;
  *)
    echo "Unknown thing to tar: <$TARNAME>"
    exit
    ;; 
esac
echo "Tarfile: <$TARFILE>"
#
# Now gzip it
#
test -f $TARFILE.gz && rm $TARFILE.gz
gzip $TARFILE
#
# Put the files in the tar directory
#
test -d ${MN_FIT_TOP}/tar/${VERS} || mkdir ${MN_FIT_TOP}/tar/${VERS}
mv $TARFILE.gz ${MN_FIT_TOP}/tar/${VERS}/
#
# Clean up
#
case $TARNAME in
  src)
    rm -r $MN_FIT_DIR
    ;;
esac
#
cd $DIRSAV
exit
