#!/bin/csh -f

set stackqueuename = 0
if ($#argv > 0) then
  set stackqueuename = $1
  echo "stackqueue: $stackqueuename"
endif

set objectqueuename = 0
if ($#argv > 0) then
  set stackqueuename = $1
  echo "stackqueue: $stackqueuename"
endif

# a bit crude : get the datadir path by interpollation 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       = 0
set initdb       = 1
set mkdummy      = 1
set initscratch  = 1
set camqueue     = 1
set initbatch    = 1
set cambatch     = 1
set stackqueue   = 1
set stackbatch   = 1
set objectqueue  = 0
set objectbatch  = 0

set OUTDIR = "testdata"
set queuename = "test"

if ($mkgpc1) then
  mkgpc1data.dvo
endif

# create the ipptopsps database
if ($initdb) then 

  # 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

## XXX no longer needed : setupScratchDb does not need a fake 'config' (or 'chunk')
## # create a dummy config so setupScratchDb.py does not fail
## if ($mkdummy) then
##   ippjython queue.py -test edit <<EOF
## dummy
## none
## IPP_PSPS
## 0
## none
## none
## 0
## 0
## 0
## 0
## 1
## none
## 0
## 0
## 1
## 1
## 2001-01-01 00:00:00.0
## NONE
## NONE
## 1
## 0
## 0
## 1
## 0
## EOF
## 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_test_scratch1"
  # mysql -h localhost -u dvo -pdvo -e "drop database ipptopsps_scratch2"
  # mysql -h localhost -u dvo -pdvo -e "drop database ipptopsps_scratch3"

  mysql -h localhost -u dvo -pdvo -e "create database ipptopsps_test_scratch1"
  # mysql -h localhost -u dvo -pdvo -e "create database ipptopsps_scratch2"
  # mysql -h localhost -u dvo -pdvo -e "create database ipptopsps_scratch3"

  # setupScratchDb.py now takes the scratch name version, which is appended to the
  # name in the settings.xml table
  ippjython setupScratchDb.py -test 1
  # ippjython setupScratchDb.py -test 2
  # ippjython setupScratchDb.py -test 3
endif

# this code is sensitive to a previously existing database?
if ($camqueue) then
  ippjython queue.py -test edit <<EOF
$queuename
test_ds
IPP_PSPS
0
catdir.cam
$OUTDIR/catdir.cam
9
11
19
21
2
$OUTDIR
0
0
1
1
2012-12-10 00:00:00.0
3PI
3PI
1
0
0
1
0
y
y
EOF
endif

if ($initbatch) then
  rm -rf $OUTDIR/IN
  ippjython loader.py -test $queuename init
endif

if ($cambatch) then
  rm -rf $OUTDIR/P2
  ippjython loader.py -test $queuename once
endif

# this code is sensitive to a previously existing database?
if ($stackqueue) then
  if ($stackqueuename == 0) then
    set stackqueuename = $queuename\_stk
  endif
  ippjython queue.py -test edit <<EOF
$stackqueuename
test_ds
IPP_PSPS
0
catdir.stk
$OUTDIR/catdir.stk
9
11
19
21
2
$OUTDIR
0
0
1
1
2012-12-10 00:00:00.0
3PI
3PI
0
1
0
1
0
y
y
EOF
endif

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

# this code is sensitive to a previously existing database?
if ($objectqueue) then
  if ($objectqueuename == 0) then
    set objectqueuename = $queuename\_obj
  endif
  ippjython queue.py -test edit <<EOF
$objectqueuename
test_ds
IPP_PSPS
0
catdir.stk
$OUTDIR/catdir.stk
9
11
19
21
2
$OUTDIR
0
0
1
1
2012-12-10 00:00:00.0
3PI
3PI
0
0
1
1
0
y
y
EOF
endif

if ($objectbatch) then
  if ($objectqueuename == 0) then
    set objectqueuename = $queuename\_obj
  endif
  rm -rf $OUTDIR/OB
   ippjython loader.py -test $objectqueuename once
endif

