#!/bin/csh -f

chmod +x mksasdata.dvo mkgpc1.sh

# a bit crude : get the datadir path by interpolation of the PATH element for psconfig
foreach f ( `echo $PATH | tr : ' '` )
  echo $f | grep $PSCONFDIR > /dev/null
  if ($status) continue
  set datadir = `echo $f | sed s/bin\$/share/`
  break
end
setenv IPPTOPSPS_DATA $datadir/ipptopsps

# before running this test, you must have a mysql user:
# mkgpc1.sh user localhost eam eam

set mkgpc1       = 1
set initdb       = 1
set mkdummy      = 1
set initscratch  = 1
set initbatch    = 1
set camqueue     = 1
set cambatch     = 1
set stackqueue   = 1
set stackbatch   = 1
set objectqueue  = 1
set objectbatch  = 1

set targ = ($argv)
set args = ""

while ($#targ)
 switch ("$targ[1]")
  case -h
  case -help
    echo "USAGE: fulltest.sh [-XX] [+XX]"
    echo " -XX : turn off features"
    echo " +XX : turn on features"
    echo "  features: all, mkgpc1, initdb, initscratch, initbatch, camera, stack, object"
    exit 2
  case -all
    set mkgpc1       = 0
    set initdb       = 0
    set initscratch  = 0
    set initbatch    = 0
    set camqueue     = 0
    set cambatch     = 0
    set stackqueue   = 0
    set stackbatch   = 0
    set objectqueue  = 0
    set objectbatch  = 0
    breaksw;
  case -mkgpc1
    set mkgpc1 = 0
    breaksw;
  case -initdb
    set initdb = 0
    breaksw;
  case -initscratch
    set initscratch = 0
    breaksw;
  case -initbatch
    set initbatch = 0
    breaksw;
  case -camera
    set camqueue = 0
    set cambatch = 0
    breaksw;
  case -stack
    set stackqueue = 0
    set stackbatch = 0
    breaksw;
  case -object
    set objectqueue = 0
    set objectbatch = 0
    breaksw;
  default:
    set args=($args $targ[1]);
    breaksw;
  endsw
  shift targ
end

set targ = ($args)
set args = ""

while ($#targ)
 switch ("$targ[1]")
  case +all
    set mkgpc1       = 1
    set initdb       = 1
    set initscratch  = 1
    set initbatch    = 1
    set camqueue     = 1
    set cambatch     = 1
    set stackqueue   = 1
    set stackbatch   = 1
    set objectqueue  = 1
    set objectbatch  = 1
    breaksw;
  case +mkgpc1
    set mkgpc1 = 1
    breaksw;
  case +initdb
    set initdb = 1
    breaksw;
  case +initscratch
    set initscratch = 1
    breaksw;
  case +initbatch
    set initbatch = 1
    breaksw;
  case "+camera"
    set camqueue = 1
    set cambatch = 1
    breaksw;
  case +stack
    set stackqueue = 1
    set stackbatch = 1
    breaksw;
  case +object
    set objectqueue = 1
    set objectbatch = 1
    breaksw;
  default:
    set args=($args $targ[1]);
    breaksw;
  endsw
  shift targ
end

if ($args[1] != "") then
    echo "USAGE: sastest.sh [-XX] [+XX]"
    echo " -XX : turn off features"
    echo " +XX : turn on features"
    echo "  features: all, mkgpc1, initdb, initscratch, initbatch, camera, stack, object"
    exit 2
endif

set stackqueuename = 0
set objectqueuename = 0

set OUTDIR = "sasdata"
set queuename = "test"

if ($mkgpc1) then
  echo ""
  echo " ---- mksasdata.dvo : creating the dvo & gpc1 database entries ----"
  mksasdata.dvo
endif

# create the ipptopsps database
if ($initdb) then 
  echo ""
  echo " ---- init ipptopsps_test ----"

  # does the database already exist?
  # XXX this command will blow away an existing database!!
  mysql -h localhost -u dvo -pdvo -e "create database ipptopsps_test"
  mysql -h localhost -u dvo -pdvo ipptopsps_test < $IPPTOPSPS_DATA/ippToPspsDbSchema.sql
endif

# re-create the ipptopsps database
if ($initscratch) then 

  # create the database (ignore failure if it already exists)
  mysql -h localhost -u dvo -pdvo -e "drop database ipptopsps_scratch"
  mysql -h localhost -u dvo -pdvo -e "create database ipptopsps_scratch"
  ippjython setupScratchDb.py -test < /dev/null
  if ($status) exit
endif

# this is poor : we need to queue something in order to generate an init batch
# this is because a skychunk carries the info about the datastore used to publish
# the batch
if ($initbatch) then
  echo ""
  echo " ---- make skychunk for IN batches ----"

  echo "initbatch              " >  initbatch.dat # skychunk name
  echo "test_ds                " >> initbatch.dat # datastore_product
  echo "IPP_PSPS               " >> initbatch.dat # datastore_type
  echo "0                      " >> initbatch.dat # datastore_publish
  echo "none                   " >> initbatch.dat # dvo_label
  echo "none                   " >> initbatch.dat # dvo_location
  echo "0                      " >> initbatch.dat # min_ra
  echo "0                      " >> initbatch.dat # max_ra
  echo "0                      " >> initbatch.dat # min_dec
  echo "0                      " >> initbatch.dat # max_dec
  echo "2                      " >> initbatch.dat # box_size
  echo "none                   " >> initbatch.dat # base_path
  echo "0                      " >> initbatch.dat # data_release
  echo "0                      " >> initbatch.dat # delete_local
  echo "1                      " >> initbatch.dat # delete_datastore
  echo "1                      " >> initbatch.dat # delete_dxlayer
  echo "2012-12-10 00:00:00.0  " >> initbatch.dat # epoch
  echo "3PI                    " >> initbatch.dat # survey
  echo "3PI                    " >> initbatch.dat # psps_survey
  echo "0                      " >> initbatch.dat # queue_P2
  echo "0                      " >> initbatch.dat # queue_ST
  echo "0                      " >> initbatch.dat # queue_OB
  echo "0                      " >> initbatch.dat # queue_DF
  echo "0                      " >> initbatch.dat # queue_DO
  echo "0                      " >> initbatch.dat # queue_FW
  echo "0                      " >> initbatch.dat # queue_FO
  echo "1                      " >> initbatch.dat # active
  echo "0                      " >> initbatch.dat # parallel
  echo "use_new                " >> initbatch.dat # P2_smf_version
  echo "any                    " >> initbatch.dat # ST_cmf_version
  echo "2000-01-01 00:00:00.0  " >> initbatch.dat # trange_start
  echo "2020-01-01 00:00:00.0  " >> initbatch.dat # trange_end
  cat initbatch.dat | ippjython queue.py -test edit
  if ($status) exit

  echo ""
  echo " ---- create init batch ----"

  rm -rf $OUTDIR/IN
  ippjython loader.py -test initbatch init < /dev/null
  if ($status) exit
endif

# this code is sensitive to a previously existing database?
if ($camqueue) then
  echo ""
  echo " ---- queue P2 batches ----"

  echo "$queuename	      " >  cambatch.dat # skychunk name
  echo "test_ds		      " >> cambatch.dat # datastore_product
  echo "IPP_PSPS	      " >> cambatch.dat # datastore_type
  echo "0		      " >> cambatch.dat # datastore_publish
  echo "catdir.sas.cam	      " >> cambatch.dat # dvo_label
  echo "$OUTDIR/catdir.sas.cam" >> cambatch.dat # dvo_location
  echo "330		      " >> cambatch.dat # min_ra
  echo "340		      " >> cambatch.dat # max_ra
  echo "-10		      " >> cambatch.dat # min_dec
  echo "0		      " >> cambatch.dat # max_dec
  echo "1		      " >> cambatch.dat # box_size
  echo "$OUTDIR		      " >> cambatch.dat # base_path
  echo "0		      " >> cambatch.dat # data_release
  echo "0		      " >> cambatch.dat # delete_local
  echo "1		      " >> cambatch.dat # delete_datastore
  echo "1		      " >> cambatch.dat # delete_dxlayer
  echo "2012-12-10 00:00:00.0 " >> cambatch.dat # epoch
  echo "3PI		      " >> cambatch.dat # survey
  echo "3PI		      " >> cambatch.dat # psps_survey
  echo "1		      " >> cambatch.dat # queue_P2
  echo "0		      " >> cambatch.dat # queue_ST
  echo "0		      " >> cambatch.dat # queue_OB
  echo "0                     " >> cambatch.dat # queue_DF
  echo "0                     " >> cambatch.dat # queue_DO
  echo "0                     " >> cambatch.dat # queue_FW
  echo "0                     " >> cambatch.dat # queue_FO
  echo "1                     " >> cambatch.dat # active
  echo "0                     " >> cambatch.dat # parallel
  echo "use_new               " >> cambatch.dat # P2_smf_version
  echo "any                   " >> cambatch.dat # ST_cmf_version
  echo "2000-01-01 00:00:00.0 " >> cambatch.dat # trange_start
  echo "2020-01-01 00:00:00.0 " >> cambatch.dat # trange_end
  cat cambatch.dat | ippjython queue.py -test edit
  if ($status) exit
endif

if ($cambatch) then
  echo ""
  echo " ---- load P2 batches ----"

  rm -rf $OUTDIR/P2
  ippjython loader.py -test $queuename once < /dev/null
  if ($status) exit
endif

# this code is sensitive to a previously existing database?
if ($stackqueue) then
  echo ""
  echo " ---- queue ST batches ----"

  if ($stackqueuename == 0) then
    set stackqueuename = $queuename\_stk
  endif
  echo "$stackqueuename	      " >  stackbatch.dat # skychunk name
  echo "test_ds		      " >> stackbatch.dat # datastore_product
  echo "IPP_PSPS	      " >> stackbatch.dat # datastore_type
  echo "0		      " >> stackbatch.dat # datastore_publish
  echo "catdir.sas.cam	      " >> stackbatch.dat # dvo_label
  echo "$OUTDIR/catdir.sas.cam" >> stackbatch.dat # dvo_location
  echo "332		      " >> stackbatch.dat # min_ra
  echo "335		      " >> stackbatch.dat # max_ra
  echo "-6		      " >> stackbatch.dat # min_dec
  echo "-4		      " >> stackbatch.dat # max_dec
  echo "1		      " >> stackbatch.dat # box_size
  echo "$OUTDIR		      " >> stackbatch.dat # base_path
  echo "0		      " >> stackbatch.dat # data_release
  echo "0		      " >> stackbatch.dat # delete_local
  echo "1		      " >> stackbatch.dat # delete_datastore
  echo "1		      " >> stackbatch.dat # delete_dxlayer
  echo "2012-12-10 00:00:00.0 " >> stackbatch.dat # epoch
  echo "3PI		      " >> stackbatch.dat # survey
  echo "3PI		      " >> stackbatch.dat # psps_survey
  echo "0		      " >> stackbatch.dat # queue_P2
  echo "1		      " >> stackbatch.dat # queue_ST
  echo "0		      " >> stackbatch.dat # queue_OB
  echo "0                     " >> stackbatch.dat # queue_DF
  echo "0                     " >> stackbatch.dat # queue_DO
  echo "0                     " >> stackbatch.dat # queue_FW
  echo "0                     " >> stackbatch.dat # queue_FO
  echo "1                     " >> stackbatch.dat # active
  echo "0                     " >> stackbatch.dat # parallel
  echo "use_new               " >> stackbatch.dat # P2_smf_version
  echo "any                   " >> stackbatch.dat # ST_cmf_version
  echo "2000-01-01 00:00:00.0 " >> stackbatch.dat # trange_start
  echo "2020-01-01 00:00:00.0 " >> stackbatch.dat # trange_end
  cat stackbatch.dat | ippjython queue.py -test edit
  if ($status) exit
endif

if ($stackbatch) then
  echo ""
  echo " ---- load ST batches ----"

  if ($stackqueuename == 0) then
    set stackqueuename = $queuename\_stk
  endif
  rm -rf $OUTDIR/ST
  ippjython loader.py -test $stackqueuename once < /dev/null
  if ($status) exit
endif

# this code is sensitive to a previously existing database?
if ($objectqueue) then
  echo ""
  echo " ---- queue OB batches ----"

  if ($objectqueuename == 0) then
    set objectqueuename = $queuename\_obj
  endif
  echo "$objectqueuename       " >  objectbatch.dat # skychunk name
  echo "test_ds		       " >> objectbatch.dat # datastore_product
  echo "IPP_PSPS	       " >> objectbatch.dat # datastore_type
  echo "0		       " >> objectbatch.dat # datastore_publish
  echo "catdir.sas.cam	       " >> objectbatch.dat # dvo_label
  echo "$OUTDIR/catdir.sas.cam " >> objectbatch.dat # dvo_location
  echo "330		       " >> objectbatch.dat # min_ra
  echo "340		       " >> objectbatch.dat # max_ra
  echo "-10		       " >> objectbatch.dat # min_dec
  echo "0		       " >> objectbatch.dat # max_dec
  echo "2		       " >> objectbatch.dat # box_size
  echo "$OUTDIR		       " >> objectbatch.dat # base_path
  echo "0		       " >> objectbatch.dat # data_release
  echo "0		       " >> objectbatch.dat # delete_local
  echo "1		       " >> objectbatch.dat # delete_datastore
  echo "1		       " >> objectbatch.dat # delete_dxlayer
  echo "2012-12-10 00:00:00.0  " >> objectbatch.dat # epoch
  echo "3PI		       " >> objectbatch.dat # survey
  echo "3PI		       " >> objectbatch.dat # psps_survey
  echo "0		       " >> objectbatch.dat # queue_P2
  echo "0		       " >> objectbatch.dat # queue_ST
  echo "1		       " >> objectbatch.dat # queue_OB
  echo "0                      " >> objectbatch.dat # queue_DF
  echo "0                      " >> objectbatch.dat # queue_DO
  echo "0                      " >> objectbatch.dat # queue_FW
  echo "0                      " >> objectbatch.dat # queue_FO
  echo "1                      " >> objectbatch.dat # active
  echo "0                      " >> objectbatch.dat # parallel
  echo "use_new                " >> objectbatch.dat # P2_smf_version
  echo "any                    " >> objectbatch.dat # ST_cmf_version
  echo "2000-01-01 00:00:00.0  " >> objectbatch.dat # trange_start
  echo "2020-01-01 00:00:00.0  " >> objectbatch.dat # trange_end
  cat objectbatch.dat | ippjython queue.py -test edit once
  if ($status) exit
endif

if ($objectbatch) then
  echo ""
  echo " ---- load OB batches ----"

  if ($objectqueuename == 0) then
    set objectqueuename = $queuename\_obj
  endif
  rm -rf $OUTDIR/OB

  ippjython loader.py -test $objectqueuename once < /dev/null
  if ($status) exit
endif

