#!/bin/sh
CDBS_TOP=${CDBS_TOP:-/data/cdbs1/tools/}
export CDBS_TOP

. ${CDBS_TOP}/util/defines.sh
export DSQUERY CDBS_SERVER CDBS_DB

echo "starting run_delivery_sql"
echo "database " ${CDBS_DB?"CDBS_ERROR: CDBS_DB is undefined"}
echo "server " ${CDBS_SERVER?"CDBS_ERROR: CDBS_SERVER is undefined"}
date

dir=`pwd`
cd $CDBS_TOP/src/run_cdbs_sql
dn=`delivery_number.s | awk -f awk.dn`
fin=$dir/cdbs_delivery$dn.sql
fout=$fin.out
echo $fout
if test -f $fin
then
  echo "using file $fin"
else
  echo "CDBS ERROR: file $fin does not exist"
  exit 1
fi
isql -Ucalwrite -ne < $fin  > $fout
if [ `grep '^Msg' $fin.out | wc -l` -gt 0 -o `grep -i "CDBS ERROR" $fin.out | wc -l` -gt 0 ]
then
   echo "possible errors in processing sql file $fin"
   echo "see output file $fout"
   exit 1
else
   echo "no errors in processing sql file $fin"
   echo "created output file $fout"
   exit 0
fi
cd $dir

