#!/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


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



# if [ `grep -c pm3 $inputfile` -gt 0 ]
# then
#     sleep 300 
#     gpid=`ps x|grep ${workingfile}.txt|grep -v grep|awk '{print $1}'`
#     father=`ps -ef |grep $gpid|grep -v grep|awk '{print $3}'`
#     m.nodekill  $father
#     rm G*$gpid*
#     mv $inputfile ${file}.err

#     exit
# fi&

#if [ `grep -c pm3 $inputfile` -gt 0 ]
#then
#overtime_pm3& 
#fi
#overtime_pm3&
Normal=0
times=0
if [ `grep -c pm3 $inputfile` -eq 0 ]
        then
                sleep 10

		kill $$
        fi&

until [ $Normal -gt 0 -o $times -gt 1 ]
do
	g03 ${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` -eq 0 ];then 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

#
