#!/usr/bin/ksh
# program arkive - jdjohnsn - 8/4/97
# 
# makes copy of datalogger raw datafiles to
#       /usr/opt/nwis/data/to_tape/datalogger directory for archival
#
# install locally on NWIS server, as in /usr/opt/nwis/local/util

# set archive directory

ARKIVE_PATH=/usr/opt/nwis/data/to_tape/datalogger

# test for supplied argument.

if [ -z "$1" ]
  then
    echo
    read ifn?"Enter name of datafile to archive: " 
    echo
  else
    ifn=$1
fi
if [ ! -f "$ifn" ] 
  then
   echo
   echo "$ifn  - does not exist in this directory.  Please check and try again."
   ifn=""
   echo
   echo "Files in your current directory are:"
   echo
   ls -F
   echo
fi

if [ ! -z "$ifn" ] ; then

# test for previous archive of that dataset
if [ -f $ifn.A ]
  then
    echo
    echo "The file name $ifn.A exists in your directory, indicating that the"
    echo "datafile $ifn has already been archived."
    read cont?"Do you wish to continue arkive? (y/n, <CR>=n): "
    if [ -z "$cont" -o "$cont" = "n" -o "$cont" = "N" ] ; then exit ; fi
fi

# check for multiple stations in one file (as in the modem retrieval files)

read m?"Data for more than one station id in this file? (y/n, <CR>=n): "

if [ -z "$m" ] ; then m="n" ; fi
echo

# get station number or special identifier.  Don't leave until you get one.
# allowing any string allows for the archival of multi-station dumps (as with
# phone modem pulls) keyed to a specific naming convention known to the user

until [ "$sta" ]
do
  echo "Enter the station number by which you wish to identify this dataset."
  echo "You may enter any string of numbers or letters, but if it is not the"
  echo "station number, the data may be unrecoverable."
  echo
  read sta?"Enter station number: "
  if [ $sta = "ex" -o $sta = "EX" ] ; then exit ; fi
  echo
done

# Get beginning date of dataset.
# 
   read dt?"Enter the begin date of this dataset, in the format YYMMDD: "
   if [ $dt = "ex" -o $dt = "EX" ] ; then exit ; fi

# Create archive file name (insert M if there are mulitple sites)

if [ $m = "y" -o $m = "Y" ] 
  then
    fn=edlraw.M.$sta.$dt
  else 
    fn=edlraw.$sta.$dt
fi

#

echo
echo "File name for archived raw dataset: $fn"
echo
read a?"Is this file name ok? (y/n, <CR>=y)" 
echo
if [ -z "$a" ] ; then a="y" ; fi
if [ $a = "y" -o $a = "Y" ] 
  then

    # check for existing file in archive dir, and prompt for overwrite

    if [ -f "$ARKIVE_PATH/$fn" ] 
      then
        echo
        echo "The file $fn already exists in the archive directory,"
        read cont?"Do you wish to overwrite? (y/n, <CR>=n): "
        if [ -z "$cont" -o "$cont" = "n" -o "$cont" = "N" ] 
          then
          echo
          echo "Aborting arkive of dataset $ifn to $fn"
          echo
          exit
        fi
    fi

# ACTUALLY COPY TO ARCHIVE DIRECTORY

    mv $ifn $ifn.A
    cp $ifn.A $ARKIVE_PATH/$fn
    if [ $? -eq 0 ]
      then
        echo
        echo "ARCHIVE ACHIEVED."
        echo
        echo "$ifn copied to $ARKIVE_PATH/$fn"
        echo "   and"
        echo "$ifn  changed to  $ifn.A"
        echo
      else
        mv $ifn.A $ifn
        echo
        echo "File copy failed.  No archive made.  Call ADAPS DBA for help."
        exit
    fi

fi
fi  # END BIG LOOP
        echo
        read again?"Do you want to arkive another file? (y/n, <CR=n): "
done
