#!/bin/csh -f

echo ""
echo "--------------------------------------"
echo "| The Sequoia 2000 Storage Benchmark |"
echo "--------------------------------------"
echo ""


# read in config information
source config

#
# Creating database.
#
echo -n "Benchmark> Do you want to create database (y/n) "
set ans = $<
echo ""
if ("$ans" == "y") then
  echo "$CREATEDB $DBNAME"
  /bin/time $CREATEDB $DBNAME
  cp $RASTER_O $PGDATA/base/$DBNAME/
  cp $POLY_FUNCS_O $PGDATA/base/$DBNAME/
endif

#
# Setup classes, types and functions.
#
echo -n "Benchmark> Do you want to run setups? (y/n) "
set ans = $<
echo ""
if ("$ans" == "y") then
  echo "/bin/time $MONITOR -q $DBNAME < setup.pq"
  /bin/time $MONITOR -q $DBNAME < setup.pq
endif

#
# Copy polygons into the database.
#
echo -n "Benchmark> Do you want to load polygons? (y/n) "
set ans = $<
echo ""
if ("$ans" == "y") then
  echo "/bin/time $MONITOR -c "'"'"copy polygons from "'"'$POLYGON_FLAT'"' \
$DBNAME
  /bin/time $MONITOR -c "copy polygons from "'"'$POLYGON_FLAT'"' $DBNAME
endif

#
# Copy points into the database.
#
echo -n "Benchmark> Do you want to load points? (y/n) "
set ans = $<
echo ""
if ("$ans" == "y") then
  echo "/bin/time $MONITOR -c "'"'"copy gpoints from "'"'$POINT_FLAT'"' $DBNAME
  /bin/time $MONITOR -c "copy gpoints from "'"'$POINT_FLAT'"' $DBNAME
endif

#
# Copy islands into the database.
#
echo -n "Benchmark> Do you want to load islands? (y/n) "
set ans = $<
echo ""
if ("$ans" == "y") then
  echo "/bin/time $MONITOR -c "'"'"copy islands from "'"'$ISLAND_FLAT'"' \
$DBNAME
  /bin/time $MONITOR -c "copy islands from "'"'$ISLAND_FLAT'"' $DBNAME
endif

#
# Copy raster files into the database.
#
echo -n "Benchmark> Do you want to load raster data? (y/n) "
set ans = $<
echo ""
if ("$ans" == "y") then
  echo "Loading rasters.. This will take a while, go get a cup of coffee.."
  set rasttime=0
  set freq=0
#
#  This section depends heavily on the naming conventions by which
#  the raster files are identified. In the case of the national
#  raster data, there are raster files of kinds other than the one
#  associated with AVHRR data; ie no time and no frequency.
#

  foreach i (`find $RASTER_PATH -name '*.ca' -print`)
    set rasttime=`echo $i | sed -e "s/.*p\(..\)ch..ca.*/\1/"`
    set freq=`echo $i | sed -e "s/.*p..ch\(.\).ca.*/\1/"`
    echo "append rasters(time = $rasttime, frequency= $freq, \
      data = "'"0 0, -2029000 -16000, 2560 1600, 2 500 500, '$i'")' \
      $DBNAME
    /bin/time $MONITOR -c "append rasters(time = $rasttime, frequency= $freq, \
      data = "'"0 0, -2029000 -16000, 2560 1600, 2 500 500, '$i'")' \
      $DBNAME
  end
endif

#
# Setup index.
#
echo -n "Benchmark> Do you want to creat index now? (y/n) "
set ans = $<
echo ""
if ("$ans" == "y") then
  echo "/bin/time $MONITOR -q $DBNAME < index.pq"
  /bin/time $MONITOR -q $DBNAME < index.pq
endif

#
# Run all queries...
#
echo -n "Benchmark> Do you want to run the queries? (y/n) "
set ans = $<
echo ""
if ("$ans" == "y") then
  echo "Making..."
  make
  echo "Query program started ..."
  fe2
endif

#
# Done.
#
echo "..... all done, bye ....."
echo "========================="
echo ""



