#! /bin/bash
#
# Copyright 2007 Parallel Quantum Solutions, Fayetteville, Arkansas, USA
#
# http://www.pqs-chem.com
#
# sales@pqs-chem.com
#
# Script to archive a PQS calculation
# the script will look in the specified directories (the default is the
# current directory) and archive the temporary files for the specified jobs
# (the default is the 'pqsjob' job)
# The script will also eliminate the temporary files from the PQS_SCRDIR
# directory.
#
# Default PQS_SCRDIR value
#
if [ -z "${PQS_SCRDIR}" ]; then
        export PQS_SCRDIR=/scr/${USER}
fi
#
# prefix for PQS scratch files
#
SCR_PREFIX=pqs_scr_
#
# extensions of the files to be removed
#
EXTENSIONS=".control .coord .basis .basis2 .grad .hess .opt .optchk .sym
            .hint .deriv .zmat .hesschk .opt2 .hprim .mos .mob .los .ffchk
            ffchk1 .path .scan .qmmm .grid .pot .tim .cosmo_* .potM .potS .aat"
#
# function to print the help message
#
PrintHelp()
{
  echo
  echo "USAGE: archive [path1][job1], [path2][job2], ..."
  echo
  echo "Examples:"
  echo
  echo "archive molecule-name         archive temporary files for molecule-name"
  echo "                              in the current directory"
  echo
  echo "archive /workdir/molecule-name   archive temporary files for molecule-name"
  echo "                                 in the workdir directory"
  echo
  exit 0
}
#
# function for determining the path of the files to be removed
#
GetPath()
{
  base=${1/#*\/}
  path=${1/%${base}}
}
#
# function for determining the basename of the files
# to be deleted
#
GetBasename()
{
        case $1 in
                "") basename=pqsjob;;
                *)  basename=$1;;
        esac
}
#
# function for archiving temporary files
#
Archive()
{
  archname=${basename}.tgz
  if [ -e "${archname}" ]; then
          NO=0
          while [ -e "${basename}_${NO}.tgz" ]; do
                  ((NO= ${NO} + 1 ))
          done
          archname=${basename}_${NO}.tgz
  fi
  if [ -n "${path}" ]; then
          list=("$(ls "${path}${basename}".*)")
          list=("${list[@]//${path}}")
          if [ -e pqs_archive.list ]; then
                  rm -f pqs_archive.list
          fi
          for memb in "${list[@]}"; do
                  echo "${memb}" >> pqs_archive.list
          done
          tar -czf "${archname}" -T pqs_archive.list -C "${path}" 
          rm -f pqs_archive.list
  else
          tar -czf "${archname}" "${basename}".*
  fi
  echo
  echo "Archive file: ${archname}"
  echo
}
#
# function for removing temporary files
#
RmTmpFiles()
{
  echo -e "removing\c"
  case ${basename} in
          "*") echo -e " all temporary files\c";;
          *) echo -e " temporary files for '${basename}'\c";;
  esac
  if [ -n "${path}" ]; then
          echo -e " in '${path}'\c"
  fi
  echo
  case ${basename} in
          "*")
            for ext in ${EXTENSIONS}; do
                    rm -f "${path}"*"${ext}"
            done
            rm -f "${PQS_SCRDIR}/${SCR_PREFIX}"*
            ;;
          *)
            for ext in ${EXTENSIONS}; do
                    rm -f "${path}${basename}${ext}"
            done
            rm -f "${PQS_SCRDIR}/${SCR_PREFIX}${basename}".*
            ;;
  esac
}
#
# argument processing (for -h option)
#
for argv in $@; do 
         case $argv in
            -h | -H | -help | --help ) PrintHelp;;
         esac
done
#
# now process arguments and archive temporary files
#
if [ $# == 0 ]; then
         GetPath
         GetBasename $base
         Archive
         RmTmpFiles
 else
        for argv in "$@"; do 
                 GetPath "$argv"
                 GetBasename "$base"
                 Archive
                 RmTmpFiles
        done
fi
