#!/bin/sh
############################################################################
#
#  archive_all <directory>
#
#  This script unloads and removes data from an existing GISRS installation
#  and puts it into a directory
#
#  History:
#  Anders Nilsson, UCAR, 02/22/06, Created
#
##########################################################################

### Constants ###

    PROGRAM=NOHRSC_systems
    HYDROGRAPH_TABLE=hydrograph
    OPTION=archive

### Get output directory ##

    WORK_DIRECTORY=$1

    if [ -z $WORK_DIRECTORY ]
    then
       echo Specify a directory where archived data can be stored...
       exit 1
    fi

### Make sure NOHRSC_systems is executable ###

    which $PROGRAM 1>/dev/null

    if [ $? -ne 0 ]
    then

      echo Error: the NOHRSC_systems executable cannot be found.
      echo Exiting.
      exit 1

    fi

### Check for presence of work directory ##

    if [ ! -d $WORK_DIRECTORY ]
    then
       mkdir "$WORK_DIRECTORY"

       if [ $? -ne 0 ]
       then
          echo Unable to create output directory. Exiting.
          exit 1
       else
          echo Created directory $WORK_DIRECTORY
       fi
    fi

### Check for emptiness ##

    if [ -n `ls "$WORK_DIRECTORY"` ]
    then
       echo Warning: output directory $WORK_DIRECTORY is not empty.
    fi

### Archive all layers ###

    GISRS_ARCHIVE=$WORK_DIRECTORY

    $PROGRAM -$OPTION All

    if [ $? -ne 0 ]
    then
       echo Unable to archive layers from database. Exiting.
       exit 1
    fi

### Done archiving layers ###

    exit 0

