#! /bin/sh -
################################################################################
#
# File:         fetchnotes
# RCS:          $Header$
# Description:  notes retrieval script
# Author:       Tw Cook, HP Corporate Engineering
# Created:      21 November 1986
# Modified:     Tue Nov 24 15:04:46 1987 (Walter Underwood) wunder@hpcerb
# Modified:     Sat Apr 22 16:52:39 1989 (Jacob Gore) gore@eecs.nwu.edu
# Language:     /bin/sh
# Package:      N/A
# Status:       updated for Notes 2.9 release
#
################################################################################
#
# Revisions:
#
# Mon Sep 28 09:03:28 1987 (Tw Cook) tw@hpcea
#    Folded in changes from John Diamant (diamant@hpfclp) to make it possible
#    to use rcp with an alternate username.
# Wed Jul 8, 1987 (Donn Terry) donn@hpfcdc
#    Added process locking.
#
################################################################################

##############################################################################
# fetchnotes
#    Script to fetch notes which have been spooled on a remote file
#    system for pickup by this system.   Can use either ftp, rfa, or rcp.
#
#    Which to use (and the appropriate passwds) is encoded in a passwd-like
#    file; see the comments below for PASSWDFILE.
#
#    RFA is probably marginally more recure and reliable, except that you
#    have to hardcode passwords somewhere.   See also the spoolnotes
#    script, which should have been run on the other end.
#
# Usage:  fetchnotes remotesysname
#
# Original:  Tw Cook <tw@hpcea.HP.COM> 21 November 1986
# Modified: Donn Terry <donn@hpfcdc> 8 July 1987 - added process locking
##############################################################################

LIBDIR=__LIBDIR__
RNEWS=rnews
hostname=`hostname`

# PASSWDFILE is a file which consists of lines specifying the type of
# transfer to use, and the necessary login / passwd information.
# Following are examples of the three allowed types:
#
#  hpsystem1:rfa:someuser:hispasswd
#  hpsystem2:ftp:anonymous:somepasswd
#  hpsystem3:rcp
#  hpsystem4:rcp:someuser#  
#
#  The special "transport" retry is used for manual retries

PASSWDFILE=$LIBDIR/fetch.cmds

if [ ! -r $PASSWDFILE ]
then
    cat << Barf
    A command file $PASSWDFILE is required by this script.

    Sample entries for rfa, ftp, and rcp respectively are:

	hpsystem1:rfa:someuser:hispasswd
	hpsystem2:ftp:anonymous:somepasswd
	hpsystem3:rcp
	hpsystem4:rcp:someuser
Barf
    exit 1
fi

PATH=/usr/ucb:/bin:/usr/bin:/usr/local/bin:/etc

if [ $# -lt 1 ]
then
    echo Usage: fetchnotes remotesysname
    exit 1
fi

debug=/dev/null

if [ "$1" = "-v" ]
then
    debug=/tmp/fetchnote$$
    shift
    set -x
fi

REMOTENAME=$1

eval `__AWK__ -F: "/^$1:/"' { printf "CMD=%s;\nUSER=%s;\nPASSWD=%s;\n", $2, $3, $4 }' < $PASSWDFILE`

if [ "$CMD" = "" ]
then
    echo "$0: no entry for $1 in $PASSWDFILE"
    exit 1
fi

REMOTEDIR=__SPOOLOUTDIR__/$hostname

SPOOLDIR=__SPOOLINDIR__

# Make sure we can get into the appropriate spool directory on
# our system.

if [ ! -d $SPOOLDIR ]
then
    mkdir $SPOOLDIR
    if [ ! -d $SPOOLDIR ]
    then
	echo $0": couldn't make a directory $SPOOLDIR"
	exit 1
    fi
fi

# Now check to see if a copy of this script is already running,
# and if so, forget about it for now.

if [ -f $SPOOLDIR/LCK..fetchnotes ]
then
	if kill -0 `cat $SPOOLDIR/LCK..fetchnotes`
	then
		exit 0
	fi
fi

echo $$ >$SPOOLDIR/LCK..fetchnotes

if [ ! -d $SPOOLDIR/$REMOTENAME ]
then
    mkdir $SPOOLDIR/$REMOTENAME
    if [ ! -d $SPOOLDIR/$REMOTENAME ]
    then
	echo "$0: couldn't make a directory $SPOOLDIR/$REMOTENAME"
	exit 1
    else
	chmod 777 $SPOOLDIR/$REMOTENAME
    fi
fi

if [ ! -d $SPOOLDIR/bogus ]
then
    mkdir $SPOOLDIR/bogus
    if [ ! -d $SPOOLDIR/bogus ]
    then
	echo "$0: couldn't make a directory $SPOOLDIR/bogus"
	exit 1
    else
	chmod 777 $SPOOLDIR/bogus
    fi
fi

cd $SPOOLDIR/$REMOTENAME


case $CMD in

rfa | RFA)
   if [ "$PASSWD" != "" ]
   then
       netunam /net/$REMOTENAME $USER:$PASSWD
   else
       netunam /net/$REMOTENAME $USER
   fi
   cd /net/$REMOTENAME/$REMOTEDIR
   # Splitting the days out like this is to keep the arg list
   # from being too long if we're a few days behind....
   mv -f Mon* $SPOOLDIR/$REMOTENAME		2> /dev/null
   mv -f Tue* $SPOOLDIR/$REMOTENAME		2> /dev/null
   mv -f Wed* $SPOOLDIR/$REMOTENAME		2> /dev/null
   mv -f Thu* $SPOOLDIR/$REMOTENAME		2> /dev/null
   mv -f Fri* $SPOOLDIR/$REMOTENAME		2> /dev/null
   mv -f Sat* $SPOOLDIR/$REMOTENAME		2> /dev/null
   mv -f Sun* $SPOOLDIR/$REMOTENAME		2> /dev/null
   cd $SPOOLDIR/$REMOTENAME
   ;;

ftp | FTP)
    (
	echo user $USER $PASSWD
	echo cd $REMOTEDIR
	echo binary    # make sure compressed files don't get mangled
	echo mget '*'
	echo quit
    ) | ftp -i -n $REMOTENAME > $debug
    # Have to do this twice to make sure * gets expanded at right time.
    (
	echo user $USER $PASSWD
	echo cd $REMOTEDIR
	for filename in `/bin/ls`
	do
	    echo delete $filename
	done
	echo quit
    ) | ftp -i -n $REMOTENAME >> $debug
    ;;

rcp | RCP | rsh | RSH)
   if [ "$USER" != "" ]
   then
       rcp $REMOTENAME.$USER:$REMOTEDIR/'*' .
       for file in *
       do
        __RSH__ $REMOTENAME -l $USER rm -f $REMOTEDIR/$file
       done
   else
       rcp $REMOTENAME:$REMOTEDIR/'*' .
       for file in *
       do
        __RSH__ $REMOTENAME rm -f $REMOTEDIR/$file
       done
   fi
   ;;

retry)
   ;;

*)
   echo "$0: invalid or missing command field in $PASSWDFILE for $REMOTENAME"
   exit 1 ;;
esac

# At this point, all the remote files should have been
# moved into the current directory.  Now we need to process
# each one by handing it into rnews.

for file in `/bin/ls`
do

    if [ ! -s $file ]
    then
	# Zero length batch, no need to bother processing it.
	echo "Warning: zero length batch $file from $REMOTENAME received and discarded."
	rm -f $file
    else
        # First, figure out if this file is compressed.  If
        # the first line isn't either "#! rnews", or "Relay-Version:"
	# or "Path:"
        # then it probably is compressed.
    
        check=`head -1 $file | fgrep 'rnews
Path
Relay'`
    
        if [ "$check" = "" ]
        then
            uncompress < $file > /tmp/fn$$
    	    nice $RNEWS < /tmp/fn$$
        else
        	nice $RNEWS < $file
        fi
        status=$?
        if [ $status -eq 0 ]
        then
    	    rm -f $file
        else
    	    echo "Warning: nonzero exit ($status) from $RNEWS, batch left in $SPOOLDIR/bogus/$file"
    	    mv -f $file ../bogus
        fi
        rm -f /tmp/fn$$
    fi
done

rm -f $SPOOLDIR/LCK..fetchnotes

exit 0

