#!/bin/sh

# Script to run Crystal03 (Property) Density of States
# NOTE: the location of the binaries is expected to be in your PATH
# The arguments are (assuming job control has copied things correctly)
# 1 - scratch directory
# 2 - input file
# 3 - wavefunction file name
# 4 - output file name
# 5 - error file name
# 6 - output file 1

mytrap()
{
  kill $!
  exit
}

trap "mytrap" 2
cd $1
rm -f $4 $5 $6
if [ -e fort.7 ]; then
  if [ -w fort.7 ]; then
    rm -f fort.7
  else
    echo >$5 " ERROR: Unable to remove existing error file"
    exit 1
  fi
fi
if [ $3 != "fort.98" ]; then
  if [ -e fort.98 ]; then
    if [ -w fort.98 ]; then
      rm -f fort.98
    else
      echo >$5 " ERROR: Unable to remove existing wavefunction file"
      exit 1
    fi
  fi
  ln -s $3 fort.98
fi

crystal=`$DLV_ROOT/scripts/crystal/locate_crystal properties`
if [ -z "$crystal" ]; then
  echo >$4 " ERROR: Unable to locate CRYSTAL binaries"
  exit 1
fi

${crystal}/properties <$2 >$4 2>&1 &
wait

if [ -e fort.7 ]; then
  mv -f fort.7 $5
else
  touch $5
fi

if [ -e DOSS.DAT ]; then
  mv -f DOSS.DAT $6
else
  mv -f fort.24 $6
fi
rm -f fort.*

