#!/bin/sh
# 
# ***************************************************************************
# *   Copyright (C) 2005 by Georgi Todorov                                  *
# *   terahz (at) geodar.com                                                *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU General Public License as published by  *
# *   the Free Software Foundation; either version 2 of the License, or     *
# *   (at your option) any later version.                                   *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU General Public License for more details.                          *
# *                                                                         *
# *   You should have received a copy of the GNU General Public License     *
# *   along with this program; if not, write to the                         *
# *   Free Software Foundation, Inc.,                                       *
# *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
# ***************************************************************************
# Version: 2.1
# Change Log:
# Initial release(1.9.95) -> Georgi Todorov (27 Nov 2005)
# 		     Based on available tests in the
#		     Makefiles of the tested software
# 1.9.96 -> Georgi Todorov (28 Nov 2005)
#           Added environment variables for some paths 
#	    Thanks to Herbert J. Bernstein for the hints.
# 1.9.97 -> Georgi Todorov (30 Nov 2005)
#           cif2pdb now implemented.
#	    Checking for the existence of the tested programs
#           before starting the test.
#           Added double quotes to all variables in the if tests
#           for AIX compatability.
# 2.0    -> H. J. Bernstein (30 January 2006)
#           release cleanup
# 2.1    -> H. J. Bernstein (3 September 2006)
#           use cif2cbf to filter diffs for cif2cif
#           adapt to ciftbx_3.0.4 outputs
#           reduce log clutter
#
#Edit below!
########----------  CIFTEST2  -------------############
xvd="${CIFTEST2DIR:-.}"   #ciftest2 data directory 
                          #(the one with directories "orig","gens", "outs", "args" and "errs")
                          #Will use $CIFTEST2DIR environment variable by default
pref="${PREFIX:-/usr/local}" #Will read $PREFIX environment variable by default
                          #A common prefix for all executables eg: /usr/local
sed="/usr/bin/sed"
########---------- VCIF    -------------############
vcif="${VCIF:-$pref/bin/vcif}"
########---------- CIFFOLD -------------############
ciffold="${CIFFOLD:-$pref/bin/ciffold}" #CIFFOLD executable to test

########---------- CIFtbx3 -------------############
c2c="${CIF2CIF:-$pref/bin/cif2cif}"          #cif2cif executable to test
c2p="${CIF2PDB:-$pref/bin/cif2pdb}"          #cif2pdf executable to test
c2x="${CIF2XML:-$pref/bin/cif2xml}"          #cif2xml executable to test
tbxex="${CIFTBX_EX:-$pref/bin/tbx_ex}"       #tbx_ex executable to test
tbxexm="${CIFTBX_EXM:-$pref/bin/tbx_exm}"    #tbx_exm executable to test
testrle="${CIFTBX_TESTRLE:-$pref/bin/testrle}" #testrle executable to test
cyc="${CYCLOPS:-$pref/bin/cyclops}"          #cyclops executable to test
cif2cbf="${CIF2CBF:-$pref/bin/cif2cbf}"      #cif2cbf (vcif2) exceutable to test

#You should NOT need to edit below!
current=`pwd`
cd $xvd
vd=`pwd`
cd $current

echo "Performing CIFTEST in directory $current"
echo "  with ciftest directory         $vd"
echo "       vcif binary at            $vcif"
echo "       ciffold binary at         $ciffold"
echo "       cif2cif binary at         $c2c"
echo "       cif2pdb binary at         $c2p"
echo "       cif2xml binary at         $c2x"
echo "       tbx_ex binary at          $tbxex"
echo "       tbxexm binary at          $tbxexm"
echo "       testrle binary at         $testrle"
echo "       cyclops binary at         $cyc"
echo "       cif2cbf binary at         $cif2cbf"
echo

if [ ! -d "$vd" ] 
then
 echo "\$CIFTEST2DIR not defined!"
 echo $vd
 echo "Both \$CIFTEST2DIR and \$PREFIX must be specified"
 echo "or \$vd and \$pref in $0 must be edited!"
 exit 1
elif [ ! -d "$pref" ]
then
 echo "\$PREFIX not defined!"
 echo "Both \$CIFTEST2DIR and \$PREFIX must be specified"
 echo "or \$vd and \$pref in $0 must be edited!"
 exit 1
fi

usage ()
{
 echo "   ciftest2 Usage:"
 echo "#########################################################################"
 echo "#  $0 vcif    -> performs vcif tests"
 echo "#  $0 cif2cbf -> performs cif2cbf (vcif2) tests"
 echo "#  $0 ciffold -> performs CIFFOLD tests"
 echo "#  $0 cif2cif -> performs cif2cif tests"
 echo "#  $0 cif2pdb -> performs cif2pdb tests"
 echo "#  $0 cif2xml -> performs cif2xml tests"
 echo "#  $0 ciftbx  -> performs ciftbx  tests"
 echo "#  $0 cyclops -> performs cyclops tests"
 echo "#  $0 ciftbx3 -> performs all ciftbx3 tests"
 echo "#  $0 all     -> perfoms all tests"
 echo "#  $0 clean   -> removes all output and diff files"
 echo "#########################################################################"
}

testvcif ()
{
 if [ ! -x "$vcif" ]
 then
  echo "$vcif is not an executable file or does not exist.  Please fix \$vcif in $0"
 else
 echo "Performing VCIF tests.  Please wait..."
  for file in $vd/origs/vcif/*; do {
    bn=`basename $file`
    $vcif `cat ${vd}/args/vcif/${bn}.args` $file > $vd/gens/vcif/${bn}.result;
    } ;
 done
 echo "diffing..."
  for file in $vd/origs/vcif/*; do {
  bn=`basename $file`
  diff $vd/outs/vcif/${bn}.result $vd/gens/vcif/${bn}.result > $vd/errs/vcif/${bn}.diff;
  if [ $? != "0" ]
  then
    echo "========================================"
    echo "Discrepancy in $bn result"
    cat  $vd/descriptions/vcif/${bn}.purpose
    echo "Differences:"
    cat $vd/errs/vcif/${bn}.diff
    echo "========================================"

  fi
  } ;
 done
 echo "done."
fi
}


testcif2cbf ()
{
 if [ ! -x "$cif2cbf" ]
 then
  echo "$cif2cbf is not an executable file or does not exist.  Please fix \$cif2cbf in $0"
 else
 echo "Performing cif2cbf tests.  Please wait..."
  cd $vd/origs/cif2cbf/
  for file in $vd/origs/cif2cbf/*.cif; do {
    bn=`basename $file`
    echo $cif2cbf `cat ${vd}/args/cif2cbf/${bn}.args` $file \> $vd/gens/cif2cbf/${bn}.result 2\>\&1;
    $cif2cbf `cat ${vd}/args/cif2cbf/${bn}.args` $file > $vd/gens/cif2cbf/${bn}.result 2>&1;
    } ;
 done
 echo "diffing..."
  for file in $vd/origs/cif2cbf/*.cif; do {
  bn=`basename $file`
  diff $vd/outs/cif2cbf/${bn}.result $vd/gens/cif2cbf/${bn}.result > $vd/errs/cif2cbf/${bn}.diff;
  if [ $? != "0" ]
  then
    echo "========================================"
    echo "Discrepancy in $bn result"
    cat  $vd/descriptions/cif2cbf/${bn}.purpose
    echo "Differences:"
    cat $vd/errs/cif2cbf/${bn}.diff
    echo "========================================"

  fi
  } ;
 done
 echo "done."
fi
}

testciffold ()
{
 if [ ! -x "$ciffold" ]
 then
  echo "$ciffold is not an executable file or does not exist. Please fix \$ciffold in $0"
 else
 echo "Performing CIFFOLD tests. Please wait..." 
 $ciffold -w -i $vd/origs/ciffold/longcomments.cif -o $vd/gens/ciffold/longcomments_out.cif
 $ciffold -w -i $vd/origs/ciffold/longtext.cif -o $vd/gens/ciffold/longtext_out.cif
 $ciffold -w -i $vd/origs/ciffold/1ejg.cif -o $vd/gens/ciffold/1ejg_out.cif
 $ciffold -w -i $vd/origs/ciffold/1zrt.cif -o $vd/gens/ciffold/1zrt_out.cif
 $ciffold -u -i $vd/gens/ciffold/1ejg_out.cif -o $vd/gens/ciffold/1ejg_rebuilt.cif
 $ciffold -u -i $vd/gens/ciffold/1zrt_out.cif -o $vd/gens/ciffold/1zrt_rebuilt.cif
 $ciffold -w -n -i $vd/origs/ciffold/longcomments.cif -o $vd/gens/ciffold/longcomments_out2.cif
 $ciffold -w -n -i $vd/origs/ciffold/longtext.cif -o $vd/gens/ciffold/longtext_out2.cif
 $ciffold -w -n -i $vd/origs/ciffold/1ejg.cif -o $vd/gens/ciffold/1ejg_out2.cif
 $ciffold -w -n -i $vd/origs/ciffold/1zrt.cif -o $vd/gens/ciffold/1zrt_out2.cif
 $ciffold -u -i $vd/gens/ciffold/1ejg_out2.cif -o $vd/gens/ciffold/1ejg_rebuilt2.cif
 $ciffold -u -i $vd/gens/ciffold/1zrt_out2.cif -o $vd/gens/ciffold/1zrt_rebuilt2.cif
 echo "diffing..."
 diff -c $vd/outs/ciffold/longcomments_out.cif  $vd/gens/ciffold/longcomments_out.cif > $vd/errs/ciffold/longcomments_out.diff
 diff -c $vd/outs/ciffold/longtext_out.cif      $vd/gens/ciffold/longtext_out.cif > $vd/errs/ciffold/longtext_out.diff
 diff -c $vd/outs/ciffold/1ejg_out.cif          $vd/gens/ciffold/1ejg_out.cif > $vd/errs/ciffold/1ejg_out.diff
 diff -c $vd/outs/ciffold/1zrt_out.cif          $vd/gens/ciffold/1zrt_out.cif > $vd/errs/ciffold/1zrt_out.diff
 diff -c $vd/outs/ciffold/1ejg_rebuilt.cif      $vd/gens/ciffold/1ejg_rebuilt.cif > $vd/errs/ciffold/1ejg_rebuilt.diff
 diff -c $vd/outs/ciffold/1zrt_rebuilt.cif      $vd/gens/ciffold/1zrt_rebuilt.cif > $vd/errs/ciffold/1zrt_rebuilt.diff
 diff -c $vd/outs/ciffold/longcomments_out2.cif $vd/gens/ciffold/longcomments_out2.cif > $vd/errs/ciffold/longcomments_out2.diff
 diff -c $vd/outs/ciffold/longtext_out2.cif     $vd/gens/ciffold/longtext_out2.cif > $vd/errs/ciffold/longtext_out2.diff
 diff -c $vd/outs/ciffold/1ejg_out2.cif         $vd/gens/ciffold/1ejg_out2.cif > $vd/errs/ciffold/1ejg_out2.diff
 diff -c $vd/outs/ciffold/1zrt_out2.cif         $vd/gens/ciffold/1zrt_out2.cif > $vd/errs/ciffold/1zrt_out2.diff
 diff -c $vd/outs/ciffold/1ejg_rebuilt2.cif     $vd/gens/ciffold/1ejg_rebuilt.cif > $vd/errs/ciffold/1ejg_rebuilt.diff
 diff -c $vd/outs/ciffold/1zrt_rebuilt2.cif     $vd/gens/ciffold/1zrt_rebuilt.cif > $vd/errs/ciffold/1zrt_rebuilt.diff
 cat $vd/errs/ciffold/*.diff
 echo "done."
fi
}

testcif2cif ()
{
if [ ! -x "$c2c" ]
 then
  echo "$c2c is not an executable file or does not exist. Please fix \$c2c in $0"
 else
 echo "Performing cif2cif tests. Please wait..."
 $c2c -t y < $vd/origs/ciftbx/xtalt2.cif > $vd/gens/ciftbx/xtalt2.new
 $c2c -e 9 < $vd/origs/ciftbx/xtalt2.cif > $vd/gens/ciftbx/xttne9.new
 $c2c -t y -e 29 < $vd/origs/ciftbx/xtalt2.cif > $vd/gens/ciftbx/xte29.new
 $c2c -q $vd/origs/ciftbx/qtest.req -i $vd/origs/ciftbx/qtest.cif > $vd/gens/ciftbx/qtest.new 2> $vd/gens/ciftbx/qtest.lst
 cd $vd/origs/ciftbx/ #needed for some stderr consistency
 $c2c -t y -d cif_mm.dic -p .._ < $vd/origs/ciftbx/4ins.cif > $vd/gens/ciftbx/4ins.new 2> $vd/gens/ciftbx/4ins.lst
 cd $current #back to normal
 $c2c -gn -u -m2048 -i $vd/origs/ciftbx/4ins.cif > $vd/gens/ciftbx/4insuw.new 2> $vd/gens/ciftbx/4insuw.lst
 cd $vd/origs/ciftbx/ #needed along with the next line for some stderr consistency
 cp $vd/gens/ciftbx/4insuw.new 4insuw.new
 $c2c -d cif_mm.dic -w 120 -i 4insuw.new > $vd/gens/ciftbx/4insw.new 2> $vd/gens/ciftbx/4insw.lst 
 $c2c -d cif_mm.dic  -i c2ctest0.cif > $vd/gens/ciftbx/c2ctest0.new 2> $vd/gens/ciftbx/c2ctest0.lst 
 $c2c -d cif_core.dic  -i c2ctest1.cif > $vd/gens/ciftbx/c2ctest1.new 2> $vd/gens/ciftbx/c2ctest1.lst 
 rm -rf 4insuw.new #cleanup
 cd $current #back to normal
 echo "diffing..."
{
if [ ! -x "$cif2cbf" ]
 then
 echo "$cif2cbf is not available, using raw diff on cifs"
 diff $vd/outs/ciftbx/xtalt2.out $vd/gens/ciftbx/xtalt2.new > $vd/errs/ciftbx/xtalt2.diff
 diff $vd/outs/ciftbx/xttne9.out $vd/gens/ciftbx/xttne9.new > $vd/errs/ciftbx/xttne9.diff
 diff $vd/outs/ciftbx/xte29.out $vd/gens/ciftbx/xte29.new > $vd/errs/ciftbx/xte29.diff
 diff $vd/outs/ciftbx/4ins.out $vd/gens/ciftbx/4ins.new > $vd/errs/ciftbx/4ins.diff
 diff $vd/outs/ciftbx/4ins.prt $vd/gens/ciftbx/4ins.lst > $vd/errs/ciftbx/4insprt.diff
 diff $vd/outs/ciftbx/4insuw.out $vd/gens/ciftbx/4insuw.new > $vd/errs/ciftbx/4insuw.diff
 diff $vd/outs/ciftbx/4insw.out $vd/gens/ciftbx/4insw.new > $vd/errs/ciftbx/4insw.diff
 diff $vd/outs/ciftbx/4insuw.prt $vd/gens/ciftbx/4insuw.lst > $vd/errs/ciftbx/4insuwprt.diff
 diff $vd/outs/ciftbx/4insw.prt $vd/gens/ciftbx/4insw.lst > $vd/errs/ciftbx/4inswprt.diff
 diff $vd/gens/ciftbx/qtest.new $vd/outs/ciftbx/qtest.out > $vd/errs/ciftbx/qtest.diff
 diff $vd/outs/ciftbx/qtest.prt $vd/gens/ciftbx/qtest.lst > $vd/errs/ciftbx/qtestprt.diff
 diff $vd/outs/ciftbx/c2ctest0.prt $vd/gens/ciftbx/c2ctest0.lst > $vd/errs/ciftbx/c2ctest0pl.diff
 diff $vd/outs/ciftbx/c2ctest1.prt $vd/gens/ciftbx/c2ctest1.lst > $vd/errs/ciftbx/c2ctest1pl.diff
 diff $vd/outs/ciftbx/c2ctest0.out $vd/gens/ciftbx/c2ctest0.new > $vd/errs/ciftbx/c2ctest0.diff
 diff $vd/outs/ciftbx/c2ctest1.out $vd/gens/ciftbx/c2ctest1.new > $vd/errs/ciftbx/c2ctest1.diff
 cat $vd/errs/ciftbx/*.diff
 echo "done."
 else
 echo "using $cif2cbf to stardardize cifs before diffs"
 $cif2cbf -w -i $vd/outs/ciftbx/xtalt2.out -o /tmp/xtalt2.out$$
 $cif2cbf -w -i $vd/gens/ciftbx/xtalt2.new -o /tmp/xtalt2.new$$
 $cif2cbf -w -i $vd/outs/ciftbx/xttne9.out -o /tmp/xttne9.out$$
 $cif2cbf -w -i $vd/gens/ciftbx/xttne9.new -o /tmp/xttne9.new$$
 $cif2cbf -w -i $vd/outs/ciftbx/xte29.out -o /tmp/xte29.out$$
 $cif2cbf -w -i $vd/gens/ciftbx/xte29.new -o /tmp/xte29.new$$
 $cif2cbf -w -i $vd/outs/ciftbx/4insuw.out -o /tmp/4insuw.out$$
 $cif2cbf -w -i $vd/gens/ciftbx/4insuw.new -o /tmp/4insuw.new$$
 $cif2cbf -w -i $vd/outs/ciftbx/4insw.out -o /tmp/4insw.out$$
 $cif2cbf -w -i $vd/gens/ciftbx/4insw.new -o /tmp/4insw.new$$
 $cif2cbf -w -i $vd/outs/ciftbx/qtest.out -o /tmp/qtest.out$$
 $cif2cbf -w -i $vd/gens/ciftbx/qtest.new -o /tmp/qtest.new$$
 $cif2cbf -w -i $vd/outs/ciftbx/c2ctest0.out -o /tmp/c2ctest0.out$$
 $cif2cbf -w -i $vd/gens/ciftbx/c2ctest0.new -o /tmp/c2ctest0.new$$
 $cif2cbf -w -i $vd/outs/ciftbx/c2ctest1.out -o /tmp/c2ctest1.out$$
 $cif2cbf -w -i $vd/gens/ciftbx/c2ctest1.new -o /tmp/c2ctest1.new$$

 diff /tmp/xtalt2.out$$ /tmp/xtalt2.new$$ > $vd/errs/ciftbx/xtalt2.diff
 diff /tmp/xttne9.out$$ /tmp/xttne9.new$$ > $vd/errs/ciftbx/xttne9.diff
 diff /tmp/xte29.out$$ /tmp/xte29.new$$ > $vd/errs/ciftbx/xte29.diff
 diff $vd/outs/ciftbx/4ins.out $vd/gens/ciftbx/4ins.new > $vd/errs/ciftbx/4ins.diff 
 diff $vd/outs/ciftbx/4ins.prt $vd/gens/ciftbx/4ins.lst > $vd/errs/ciftbx/4insprt.diff
 diff /tmp/4insuw.out$$ /tmp/4insuw.new$$ > $vd/errs/ciftbx/4insuw.diff
 diff /tmp/4insw.out$$ /tmp/4insw.new$$ > $vd/errs/ciftbx/4insw.diff
 diff $vd/outs/ciftbx/4insuw.prt $vd/gens/ciftbx/4insuw.lst > $vd/errs/ciftbx/4insuwprt.diff
 diff $vd/outs/ciftbx/4insw.prt $vd/gens/ciftbx/4insw.lst > $vd/errs/ciftbx/4inswprt.diff
 diff /tmp/qtest.new$$ /tmp/qtest.out$$ > $vd/errs/ciftbx/qtest.diff
 diff $vd/outs/ciftbx/qtest.prt $vd/gens/ciftbx/qtest.lst > $vd/errs/ciftbx/qtestprt.diff
 diff /tmp/c2ctest0.out$$ /tmp/c2ctest0.new$$ > $vd/errs/ciftbx/c2ctest0.diff
 diff /tmp/c2ctest1.out$$ /tmp/c2ctest1.new$$ > $vd/errs/ciftbx/c2ctest1.diff
 cat $vd/errs/ciftbx/*.diff
 echo "done."
fi
}
fi
}

testcif2pdb ()
{
if [ ! -x "$c2p" ]
 then
  echo "$c2p is not an executable file or does not exist. Please fix \$c2p in $0"
 else
 echo "Performing cif2pdb tests. Please wait..."
 for name in 1ace 1crn 1cro 1cwp 1hyh 1zrt 2ace 4hir 4ins 5hvp ADH041 BDL001 BDLB13 DDF040
 do
  echo "working with $name.cif"
  $c2p -d $vd/origs/ciftbx/cif_mm.dic -p $name < $vd/origs/ciftbx/$name.cif > $vd/gens/ciftbx/p_$name.pdb \
    2> $vd/gens/ciftbx/p_$name.lst
  $sed -e  '1,\$$s/ 0\./  \./g' < $vd/gens/ciftbx/p_$name.pdb | $sed -e '1,\$$s/ -0\./  -\./g' >$vd/gens/ciftbx/$name.spdb
  $c2p -w yes -d $vd/origs/ciftbx/cif_mm.dic -p $name < $vd/origs/ciftbx/$name.cif > $vd/gens/ciftbx/w_$name.wid \
    2> $vd/gens/ciftbx/w_$name.lst
  $sed -e  '1,\$$s/ 0\./  \./g' < $vd/gens/ciftbx/w_$name.wid | $sed -e '1,\$$s/ -0\./  -\./g' >$vd/gens/ciftbx/$name.swid
 done
 echo "diffing..."
 for name in 1ace 1crn 1cro 1cwp 1hyh 1zrt 2ace 4hir 4ins 5hvp ADH041 BDL001 BDLB13 DDF040
 do
 cat $vd/outs/ciftbx/$name.tpdb | $sed -e '1,\$$s/ 0\./  \./g'  | $sed -e '1,\$$s/ -0\./  -\./g' | \
 diff -  $vd/gens/ciftbx/$name.spdb > $vd/errs/ciftbx/$name.p.diff
 cat $vd/outs/ciftbx/$name.twid | $sed -e '1,\$$s/ 0\./  \./g'  | $sed -e '1,\$$s/ -0\./  -\./g' | \
 diff -  $vd/gens/ciftbx/$name.swid > $vd/errs/ciftbx/$name.w.diff
 done
 cat $vd/errs/ciftbx/*.p.diff
 cat $vd/errs/ciftbx/*.w.diff
 echo "done."
fi
}

testcif2xml ()
{
if [ ! -x "$c2x" ]
 then
  echo "$c2x is not an executable file or does not exist. Please fix \$c2x in $0"
 else
 echo "Performing cif2xml tests. Please wait..."
 cd $vd/origs/ciftbx/ #needed for some stderr consistency
 $c2x -t y -s referto cml.dtd -d cif_mm.dic -d cif_cml.dic< $vd/origs/ciftbx/xtalt2.cif > $vd/gens/ciftbx/xtalt2.new.x \
  2> $vd/gens/ciftbx/xtalt2.lst.x
 $c2x -e 9 -s referto cml.dtd < $vd/origs/ciftbx/xtalt2.cif > $vd/gens/ciftbx/xttne9.new.x \
  2> $vd/gens/ciftbx/xttne9.lst.x
 $c2x -t y -e 29 < $vd/origs/ciftbx/xtalt2.cif > $vd/gens/ciftbx/xte29.new.x \
  2> $vd/gens/ciftbx/xte29.lst.x
 $c2x -d cif_mm.dic -d cif_cml.dic -s referto cml.dtd -p .._ < $vd/origs/ciftbx/4ins.cif > $vd/gens/ciftbx/4ins.new.x \
  2> $vd/gens/ciftbx/4ins.lst.x \
 cd $current
 echo "diffing..."
 diff $vd/outs/ciftbx/xtalt2.out.x $vd/gens/ciftbx/xtalt2.new.x > $vd/errs/ciftbx/xtalt2.x.diff
 diff $vd/outs/ciftbx/xttne9.out.x $vd/gens/ciftbx/xttne9.new.x > $vd/errs/ciftbx/xttne9.x.diff
 diff $vd/outs/ciftbx/xte29.out.x $vd/gens/ciftbx/xte29.new.x > $vd/errs/ciftbx/xte29.x.diff
 diff $vd/outs/ciftbx/4ins.out.x $vd/gens/ciftbx/4ins.new.x > $vd/errs/ciftbx/4ins.x.diff
 diff $vd/outs/ciftbx/4ins.prt.x $vd/gens/ciftbx/4ins.lst.x > $vd/errs/ciftbx/4insprt.x.diff
 cat $vd/errs/ciftbx/*.x.diff	
 echo "done."
fi
}

testcyclops ()
{
if [ ! -x "$cyc" ]
then
  echo "$cyc is not an executable file or does not exist. Please fix \$cyc in $0"
else
 echo "Performing cyclops tests. Please wait..."
 cd $vd/origs/ciftbx
 $cyc $vd/origs/ciftbx/mtest.prt $vd/gens/ciftbx/STARCHEK 2> $vd/gens/ciftbx/cyclops_test.lst
 echo "-o $vd/gens/ciftbx/STARCHK2 -d cif_core.dic -d cif_mm.dic" > $vd/gens/ciftbx/STARCMD
 $cyc $vd/origs/ciftbx/mtest.prt -f $vd/gens/ciftbx/STARCMD 2> $vd/gens/ciftbx/cyclops_test2.lst
 cd $current
 echo "diffing..."
 diff $vd/outs/ciftbx/cyclops_test.prt $vd/gens/ciftbx/cyclops_test.lst > $vd/errs/ciftbx/cyclops_prt_lst.diff
 diff $vd/outs/ciftbx/mtest.cyc $vd/gens/ciftbx/STARCHEK > $vd/errs/ciftbx/cyclops_mtest.diff
 diff $vd/gens/ciftbx/cyclops_test.lst $vd/gens/ciftbx/cyclops_test2.lst > $vd/errs/ciftbx/cyclops_lst_lst.diff
 diff $vd/gens/ciftbx/STARCHEK $vd/gens/ciftbx/STARCHK2 > $vd/errs/ciftbx/cyclops_cyc.diff
 cat $vd/errs/ciftbx/cyclops*.diff
 echo "done."
fi
}

testciftbx ()
{
if [ ! -x "$tbxex" ]
then
  echo "$tbxex is not an executable file or does not exist. Please fix \$tbxex in $0"
elif [ ! -x "$tbxexm" ]
then
  echo "$tbxexm is not an executable file or does not exist. Please fix \$tbxexm in $0"
elif [ ! -x "$testrle" ]
then
  echo "$testrle is not an executable file or does not exist. Please fix \$testrle in $0"
else
 echo "Performing ciftbx tests. Please wait..."
 cd $vd/gens/ciftbx/
 ln -s $vd/origs/ciftbx/test.cif $vd/gens/ciftbx/test.cif
 ln -s $vd/origs/ciftbx/test.req $vd/gens/ciftbx/test.req
 ln -s $vd/origs/ciftbx/cif_mm.dic $vd/gens/ciftbx/cif_mm.dic
 ln -s $vd/origs/ciftbx/cif_core.dic $vd/gens/ciftbx/cif_core.dic
 $tbxex > $vd/gens/ciftbx/test.lst
 $tbxexm > $vd/gens/ciftbx/mtest.lst
 $testrle < $vd/origs/ciftbx/cif_core.dic > $vd/gens/ciftbx/testrle.lst
 rm -f $vd/gens/ciftbx/test.cif
 rm -f $vd/gens/ciftbx/test.req
 rm -f $vd/gens/ciftbx/cif_mm.dic
 rm -f $vd/gens/ciftbx/cif_core.dic
 cd $current
 echo "diffing..."
 diff -b -c $vd/outs/ciftbx/test.prt $vd/gens/ciftbx/test.lst > $vd/errs/ciftbx/tbx_test_prt_lst.diff
 diff -b -c $vd/outs/ciftbx/mtest.prt $vd/gens/ciftbx/mtest.lst > $vd/errs/ciftbx/tbx_mtest_prt_lst.diff
 diff -b -c $vd/outs/ciftbx/test.out $vd/gens/ciftbx/test.new > $vd/errs/ciftbx/tbx_test_out_new.diff
 diff -b -c $vd/outs/ciftbx/mtest.out $vd/gens/ciftbx/mtest.new > $vd/errs/ciftbx/tbx_mtest_out_new.diff
 diff -b -c $vd/outs/ciftbx/mtest.xml $vd/gens/ciftbx/mtest.xew > $vd/errs/ciftbx/tbx_mtest_xml_xew.diff
 diff -b -c $vd/outs/ciftbx/testrle.prt $vd/gens/ciftbx/testrle.lst > $vd/errs/ciftbx/tbx_testrle_prt_lst.diff
 cat $vd/errs/ciftbx/tbx_*.diff
 echo "done."
fi
}

if [ $# -eq 0 ]
then
usage
 exit 1
elif [ $1 = "vcif" ]
then
  testvcif  # execute vcif tests
elif [ $1 = "cif2cbf" ]
then
  testcif2cbf  # execute cif2cbf tests
elif [ $1 = "ciffold" ]
then
 testciffold # execute ciffold tests
elif [ $1 = "cif2cif" ]
then
 testcif2cif # execute cif2cif tests
elif [ $1 = "cif2pdb" ]
then
 testcif2pdb # execute cif2pdb tests
elif [ $1 = "cif2xml" ]
then
 testcif2xml # execute cif2xml tests
elif [ $1 = "ciftbx" ]
then
 testciftbx  # execute ciftbx tests
elif [ $1 = "cyclops" ]
then
 testcyclops # execute cyclops tests
elif [ $1 = "ciftbx3" ]
then
 echo "Performing all CIFtbx3 tests. Please wait..."
 echo ""
 testcif2cif # execute cif2cif tests
 testcif2pdb # execute cif2pdb tests
 testcif2xml # execute cif2xml tests
 testcyclops # execute cyclops tests
 testciftbx  # execute ciftbx tests
 echo ""
 echo "Done all."
elif [ $1 = "all" ]
then
 echo "Performing all tests. Please wait..."
 echo ""
 testvcif    # execute vcif tests
 testcif2cbf # execute cif2cbf tests
 testciffold # execute ciffold tests
 testcif2cif # execute cif2cif tests
 testcif2pdb # execute cif2pdb tests
 testcif2xml # execute cif2xml tests
 testcyclops # execute cyclops tests
 testciftbx  # execute ciftbx tests
 echo ""
 echo "Done all."
elif [ $1 = "clean" ]
then
 echo "Cleaning gens directory"
 rm -rf $vd/gens/*
 echo "Cleaning errs directory"
 rm -rf $vd/errs/*
 echo "Generating new directories"
 mkdir $vd/gens/cif2cbf
 mkdir $vd/gens/ciffold
 mkdir $vd/gens/ciftbx
 mkdir $vd/gens/vcif
 mkdir $vd/errs/cif2cbf
 mkdir $vd/errs/ciffold
 mkdir $vd/errs/ciftbx
 mkdir $vd/errs/vcif
 echo "done."
else
 echo ""
 echo " Unable to parse input."
 echo ""
 usage # print the usage
 exit 1   
fi
