#!/bin/bash
#Author: akiyama
#Facebook: b98202054@ntu.edu.tw
#Please put this .sh file into the directory containing a molecule geomety file
#Generate input files(.in) in a new directory($filename.$exchange)

read -p "Please enter the geometry file:" filename
read -p "Please enter the jobtype:" jobtype
read -p "Please enter the exchange functional:" exchange
read -p "Please enter the basis:" basis
read -p "Please enter the xc_grid:" xcgrid
read -p "Please enter the max_scf_cycles:" cycles
path=`pwd`
test ! -r $filename && echo "The file is no exist." && exit 0

datum=$(grep -A1 ^[1-9] $filename)
cat >roll<<OVER
$datum
OVER

list=$(grep ^[A-Z] roll|cut -d ' ' -f1)
echo "$list"
a=0
for i in $list
do
a=$(($a+1))
echo "NO.$a"
echo "Molecule:$i"
name=$i
netcharge=$(grep -w $i roll|cut -d ' ' -f2)
echo "$netcharge"
multi=$(grep -w $i roll|cut -d ' ' -f3)
echo "$multi"
count=$(grep -B1 -w $i roll | head -n1)
echo "$count"
coordinate=$(grep -A$count -w $i $filename|tail -$count)
mkdir -p $filename.$exchange
cd $path/$filename.$exchange
cat >$name.in<<OVER
\$molecule
$netcharge $multi
$coordinate
\$end

\$rem
jobtype $jobtype
exchange $exchange
basis $basis
max_scf_cycles $cycles
xc_grid $xcgrid
scf_algorithm diis
scf_convergence 8
thresh 14
scf_final_print = 1
symmetry = false
sym_ignore = true
\$end
OVER
cd ..
done
rm roll