#!/bin/sh
#############################################################################
#
#  archive_hydrographs <directory>
#
#  This script copies all hydrograph information into a created subdirectory
#
#  History:
#  Anders Nilsson, UCAR, 01/25/06, Created
#  Anders Nilsson, UCAR, 02/22/06, Added test for table existence
#  Anders Nilsson, UCAR, 02/27/06, Gets information from nohrsc.cfg
#  Anders Nilsson, UCAR, 03/14/06, Checks to see if hydrograph file
#                                  exists. If not, skips.
#
##########################################################################

### Constants ###

    PROGRAM=NOHRSC_systems
    CONFIG=nohrsc.cfg

### Get working directory ###

    WORK=$1

    if [ -z $WORK ]
    then
       WORK=`pwd`
    fi

### Make sure NOHRSC_systems is executable ###

    if [ -f ${WORK}/${PROGRAM} ]
    then
       PROGRAM=${WORK}/${PROGRAM}
    else

       which ${PROGRAM} 1>/dev/null

       if [ $? -ne 0 ]
       then

         echo Error: the NOHRSC_systems executable can't be found.
         echo \(It is not located within the current directory, and,
         echo  its parent directory isn't found in the PATH system variable.\)
         echo Exiting.
         exit 1

       fi
     
    fi

### Check for existence of config file ###

    CONFIG_FILE=${IHABBS}/${CONFIG}

    if [ ! -f ${CONFIG_FILE} ]
    then
       CONFIG_FILE=`pwd`/${CONFIG}
       if [ ! -f ${CONFIG_FILE} ]
       then
          CONFIG_FILE=${IHABBS}/${CONFIG}
          echo Unable to find config file ${CONFIG_FILE}
          echo Exiting.
          exit 1
       fi
    fi

### Get TABLENAME from config file ###
   
    LINE=`grep -e "^TABLENAME" "${CONFIG_FILE}" | cut -d \= -f 2`

    if [ $? -ne 0 ]
    then
       echo Unable to determine hydrograph TABLENAME in ${CONFIG_FILE}
       echo Exiting.
       exit 1
    fi
    TABLE=`echo ${LINE}`


### Test for table existence ###

    ${PROGRAM} -table_exists "${TABLE}"

    if [ $? -ne 0 ]
    then
       echo Hydrograph table ${TABLE} does not exist. No need to archive.
       exit 0
    fi


### Check for existence of archived hydrographs directory ###

    HYDRO_DIRECTORY=${WORK}/archived_hydrographs

    if [ ! -d ${HYDRO_DIRECTORY} ]
    then
       mkdir "${HYDRO_DIRECTORY}"
       
       if [ $? -ne 0 ]
       then
          echo Unable to copy hydrograph information. Exiting.
          exit 1
       else
          echo Created directory ${HYDRO_DIRECTORY}
       fi
    fi

### Get hydrograph table information

    SUFFIX=_schema 
    
    echo Putting the table schema of ${TABLE} into ${HYDRO_DIRECTORY}/${TABLE}${SUFFIX}
    ${PROGRAM} -table_schema "${TABLE}" "${HYDRO_DIRECTORY}/${TABLE}${SUFFIX}"

    if [ $? -ne 0 ]
    then
       echo Unable to get schema information on ${TABLE} table. Exiting.
       exit 1
    fi

### Get table contents ###

    echo Unloading table ${TABLE} contents to ${HYDRO_DIRECTORY}/${TABLE}

    ${PROGRAM} -table_unload "${TABLE}" "${HYDRO_DIRECTORY}/${TABLE}_tmp"

    if [ $? -ne 0 ]
    then
       echo Unable to get contents of ${TABLE} table. Exiting.
       exit 1
    fi

### Get hydrograph files ###

    if [ -f ${HYDRO_DIRECTORY}/${TABLE} ]
    then 
       rm -f "${HYDRO_DIRECTORY}/${TABLE}"
    fi
    touch "${HYDRO_DIRECTORY}/${TABLE}"

    IFS="
"
    for LINE in `cat "${HYDRO_DIRECTORY}/${TABLE}_tmp"`
    do
       FILE=`echo ${LINE} | cut -d \| -f 10`

       if [ -f ${FILE} ]
       then 
          echo Copying file ${FILE}
          BASEFILE=`basename ${FILE}`
          cp -fp "${FILE}" "${HYDRO_DIRECTORY}/${BASEFILE}"

          if [ $? -ne 0 ]
          then
             echo Unable to copy ${FILE}. Exiting.
             exit 1
          else
             echo ${LINE} >> "${HYDRO_DIRECTORY}/${TABLE}"
          fi
       else
          echo Hydrograph file ${FILE} missing. Skipping.
       fi
    done

    rm -f "${HYDRO_DIRECTORY}/${TABLE}_tmp"

### Done ###

    exit 0
