#!/bin/sh
#############################################################################
#
#  restore_hydrographs <directory>
#
#  This script restores all archived hydrograph information
#  (inserts database records and moves files)
#
#  History:
#  Anders Nilsson, UCAR, 01/25/06, Created
#  Anders Nilsson, UCAR, 02/27/06, Gets information from nohrsc.cfg
#  Anders Nilsson, UCAR, 07/20/06, Checks hydrograph file presence
#
##########################################################################

### Constants ###

    PROGRAM=NOHRSC_systems
    SUB_DIR=archived_hydrographs
    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 DATAPATH and TABLENAME from config file ###

    LINE=`grep -e "^DATAPATH" "$CONFIG_FILE" | cut -d \= -f 2`

    if [ $? -ne 0 ]
    then
       echo Unable to determine hydrograph DATAPATH in $CONFIG_FILE
       echo Exiting.
       exit 1
    fi
    DESTINATION_DIRECTORY=`echo $LINE`
    
    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`


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

    HYDRO_DIRECTORY=$WORK/$SUB_DIR

    if [ ! -d $HYDRO_DIRECTORY ]
    then
       echo Archived hydrographs directory does not exist.
       echo Nothing to process. Exiting.
       exit 1
    fi

### Test for existence of hydrograph table ###

    $PROGRAM -table_exists "$TABLE"

    if [ $? -ne 0 ]
    then

###    Create hydrograph table ###

       echo Creating new hydrograph table $TABLE

       SUFFIX=_schema

       $PROGRAM -table_create "$TABLE" "$HYDRO_DIRECTORY/$TABLE$SUFFIX"

       if [ $? -ne 0 ]
       then
          echo Unable to create table $TABLE. Exiting.
          exit 1
       fi

   fi

### Create new table contents with revised paths ###

    SUFFIX=.new
    DATA_FILE=$HYDRO_DIRECTORY/$TABLE
    OUTPUT_FILE=$DATA_FILE$SUFFIX
    IFS="
"
    if [ -f $DATA_FILE ]
    then

       if [ -f $OUTPUT_FILE ]
       then
          rm -f "$OUTPUT_FILE"
          if [ $? -ne 0 ]
          then
             echo Unable to clear scratch output file $OUTPUT_FILE. Exiting.
             exit 1
          fi
       fi

       for LINE in `cat "$DATA_FILE"`
       do
          FILE=`echo $LINE | cut -d \| -f 10`
          BASEFILE=`basename $FILE`
          PART1=`echo $LINE | cut -d \| -f 0-9`
          PART3=`echo $LINE | cut -d \| -f 11-`
          OUTPUT_HYDRO_FILE=$DESTINATION_DIRECTORY/$BASEFILE
          INPUT_HYDRO_FILE=$HYDRO_DIRECTORY/$BASEFILE

###       Test to see if file exists ###

          if [ -f $INPUT_HYDRO_FILE ]
          then

###          Add to list to entries to be inserted ###
          
             echo $PART1\|$OUTPUT_HYDRO_FILE\|$PART3 >> "$OUTPUT_FILE"

###          Copy hydrograph to output location ###

             echo Copying $INPUT_HYDRO_FILE to $OUTPUT_HYDRO_FILE

             cp -fp "$INPUT_HYDRO_FILE" "$OUTPUT_HYDRO_FILE" 
             if [ $? -ne 0 ]
             then
                echo Unable to copy $INPUT_HYDRO_FILE to $OUTPUT_HYDRO_FILE
                echo Exiting.
                exit 1
             fi
          fi

       done

###    Load data into database ###

       $PROGRAM -table_load "$TABLE" "$OUTPUT_FILE"

       if [ $? -ne 0 ]
       then
          echo Unable to load $OUTPUT_FILE into table $TABLE. Exiting.
          exit 1
       fi

    else
       echo No data that needs restoring.
    fi

### Done ###


    exit 0

