#!/bin/csh -f

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

# 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 OUTDIR = "sas12"
set queuename = "test"

if ($mkgpc1) then
  mksasdata.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"
  mysql -h localhost -u dvo -pdvo ipptopsps < $IPPTOPSPS_DATA/ippToPspsDbSchema.sql

endif

# create a dummy config so setupScratchDb.py does not fail
if ($mkdummy) then
  ippjython queue.py 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
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_scratch"
  # 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_scratch"
  # mysql -h localhost -u dvo -pdvo -e "create database ipptopsps_scratch2"
  # mysql -h localhost -u dvo -pdvo -e "create database ipptopsps_scratch3"

  ippjython setupScratchDb.py dummy ipptopsps_scratch
  # ippjython setupScratchDb.py dummy ipptopsps_scratch2
  # ippjython setupScratchDb.py dummy ipptopsps_scratch3
endif

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

if ($initbatch) then
  ippjython loader.py $queuename init
endif

if ($cambatch) then
  ippjython loader.py $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 edit <<EOF
$stackqueuename
test_ds
IPP_PSPS
0
catdir.sas12
$OUTDIR/catdir.sas12
330
340
-8
2
1
$OUTDIR
0
0
1
1
2012-12-10 00:00:00.0
3PI
3PI
0
1
0
1
y
y
EOF
endif

if ($stackbatch) then
  if ($stackqueuename == 0) then
    set stackqueuename = $queuename\_stk
  endif
  ippjython loader.py $stackqueuename once
endif

