#! /bin/bash

# This copies logbook entries from the test cell to the central database

SPEC=/usr/local/share/cyflex/tclogbook_specs
SQLITE=$(grep com.cybermetrix.cyflex.tclogbook_sqlite $SPEC | \
   tr -s \  |
   cut -f 2 -d\ )
ORACLE=$(grep com.cybermetrix.cyflex.tclogbook_oracle $SPEC | \
   tr -s \  |
   cut -f 2 -d\ )

if [ -f /cell/site_special ]; then
   sed '/^#/d' /cell/site_special 1>/tmp/site_special.$$
   SITE=$(cat /tmp/site_special.$$ | grep  test_site  | sed s/\'//g | awk '{print $3 }')
   rm -f /tmp/site_special.$$
else
   SITE=""
fi

CELL=$(cat /cell/cell_name)

BASE=/opt/cyflex_java

export CLASSPATH=$BASE/specfilegui/applications.jar\
:$BASE/specfilegui/specfilegui.jar\
:$BASE/specfilegui.3rdparty/ojdbc.jar\
:$BASE/specfilegui.3rdparty/sqlitejdbc.jar

if [[ "${SITE}" == ""  ]]; then
   echo "No site has been specified. No data will be downloaded. The local database should not be flooded with unknown site data."
   exit -1
fi

if [[ "${CELL}" == ""  ]]; then
   echo "No cell has been specified. No data will be downloaded. The local database should not be flooded with unknown cell data."
   exit -2
fi

/opt/cyflex_java/java_jdk/bin/java com.cybermetrix.cyflex.applications.tclogbook.TCLogbookToTestCell \
   $ORACLE $SQLITE $SITE $CELL
