#! /bin/bash
#
# Copyright 2007 Parallel Quantum Solutions, Fayetteville, Arkansas, USA
#
# http://www.pqs-chem.com
#
# sales@pqs-chem.com
#
# Script to remove temporary files of PQS jobs.
# the script will look in the specified directories (the default is the
# current directory) and remove the temporary files for the specified jobs
# (the default is the 'pqsjob' job)
# If the job name is 'all', then all PQS temporary files will be deleted
# from the specified directory.
# The script will also eliminate the temporary files from the PQS_SCRDIR
# directory.
#
#WARNING:  'tidy all'  will remove all pqs scratch files from the PQS_SCRDIR
#                      directory. This will cause any currently running
#                      PQS job to crash
#
# 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: tidy [path1][job1], [path2][job2], ..., [path][all]"
  echo
  echo "Examples:"
  echo
  echo "tidy molecule-name            remove temporary files for molecule-name"
  echo "                              in the current directory"
  echo
  echo "tidy /workdir/molecule-name   remove temporary files for molecule-name"
  echo "                              in the workdir directory"
  echo
  echo "tidy all                      remove all temporary files in the current"
  echo "                              directory"
  echo "                          *** WARNING ***: do not do 'tidy all' if there"
  echo "                              are PQS jobs currently running in your system"
  echo "                              as this command will delete all PQS scratch"
  echo "                              files from the PQS_SCRDIR directory, causing"
  echo "                              the running jobs to crash"
  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
                all) basename=*;;
                "") basename=pqsjob;;
                *)  basename=$1;;
        esac
}
#
# 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 remove temporary files
#
if [ $# == 0 ]; then
         GetPath
         GetBasename $base
         RmTmpFiles
else
        for argv in "$@"; do 
                 GetPath "$argv"
                 GetBasename "$base"
                 RmTmpFiles
        done
fi
