#!/bin/sh
# pm3 opt ONE Gaussian input file
# $1: input file of pm3 opt
# working.txt  working.out :working temp file
# ${$1%.*}.out  correct outfile

exec 2>/dev/null
exec 1>/dev/null

if [ ! -s $1 ] ;then exit;fi
localpash=`pwd`
cd
#source .g03
cd $localpash

inputfile=$1
file=${inputfile%.*}
workingfile=${file}_$$
cp $inputfile ${workingfile}.txt
outputfile=${file}.out
cp $inputfile ${file}.err && inputfile=${file}.err


exit_handler(){

  self=$!
  child=`pid_tree.py $$`
  mv $inputfile ${file}.err
  rm ${workingfile}.txt
  rm ${workingfile}.out
  echo $child
  for i in $child; do
    if [ $i != $self ]; then
      echo killing $i
      kill -15 $i
      kill -9 $i
      rm Gau*$i*
    fi
  done

  echo $workingfile	
  exit 0
}
trap exit_handler SIGINT SIGTERM SIGQUIT  

###### main #####

Normal=0
times=0
if [ `grep -c pm3 $inputfile` -gt 0 ]
then
  sleep 1000
  exit_handler
fi&

until [ $Normal -gt 0 -o $times -gt 0 ]
do
  g09 ${workingfile}.txt ${workingfile}.out
  Normal=`tail ${workingfile}.out|grep -c "Normal"`
  if [ $Normal -gt 0 ]
  then
    mv ${workingfile}.out $outputfile
    rm ${workingfile}.txt
    if [ `grep -c chk $inputfile` -ne 0 ]
    then
      echo
    else
      outinfo "$outputfile"
    fi
    rm $inputfile

  else
    recompute "${workingfile}.out" 2>/dev/null
  fi
  times=`expr $times + 1`
done



if [ $Normal -eq 0 ] ;then
  mv $inputfile ${file}.err
  rm ${workingfile}.txt
  rm ${workingfile}.out
fi

