 #!/bin/sh
#---------#---------#---------#---------#---------#---------#---------#---------
#
# title
#   scp directory contents from a remote host
# author
#   larg 28 nov 2002
# description
#   copy all files found in one directory on the remote host
#   to another directory. This is like an scp command with a couple of checks
#   and some reporting.
#   Run like this ./xxx > xxx.log to capture the reports into a log.
# modifications
#
#---------#---------#---------#---------#---------#---------#---------#---------
# setup
        usage_message="
        usage:
          $0
        example:
          the command
              $0 10.198.160.93 /opt/isp01/site/pub /tmp/IN/data
        description
          copy all files found in one directory on the remote host
          (10.198.160.93:/opt/isp01/site/pub) to another directory
          (/tmp/IN/data). This is like an scp command with added
          checks and some logging.
"
# checks
        # command line options
        # no options provided? tell the punter how to use this utility
        if [ "$#" -ne 2 ]; then
                echo "This script does not take options."
                echo "$usage_message" 1>&2
                exit 1
        fi

        # Check to see if we are being called by 'nsuser'
        if [ `/usr/ucb/whoami` != "nsuser" ]; then
                echo "$0: You must be nsuser to run this script" >&2
                exit 128
        fi

# main
        SOURCE_HOST=$1
        SOURCE_DIR=$2
        DEST_DIR=$3

# start logging
        echo ""
        echo "------------------------------------------"
        echo "Run date: "`date`
        echo ""
        echo "remote before copy"
        /opt/ssh/bin/ssh $SOURCE_HOST ls -l $SOURCE_DIR
        echo "local before copy"
        ls -l $DEST_DIR

# copy all files
        /opt/ssh/bin/scp -q $SOURCE_HOST:$SOURCE_DIR/* $DEST_DIR
        RET=$?

# Check return code
        if [ "$RET" -ne 0 ]; then
                echo "$0: scp from $SOURCE_HOST failed with return code $RET" 1>&2
                exit $RET
        fi
# remove remote files
        /opt/ssh/bin/ssh $SOURCE_HOST rm $SOURCE_DIR/*
# more logging
        echo ""
        echo "local after copy"
        ls -l $DEST_DIR


