#!/bin/sh
# Copyright 1995-2025 Richard M. Troth, all rights reserved. <plaintext>
#
#         Name: rcv (shell script)
#               "receive" a file sent via UFT
#         Date: 2024-04-19 (Fri) Gallatin, and prior
#
#         Note: this is a *really* simplistic script
#               The layout of the UFT spooling directories along with
#               the format of each ".cf" control file is intended to
#               simplify scripting, should you choose to do your own.
#

#
# get basename of this script for reporting
Z=`basename "$0"`

#
# get username to know which UFT spool space sub-directory to use
U=`id | awk -F\( '{print $2}' | awk -F\) '{print $1}'`

#
# here's hoping that per-user sub-dir does in fact exist
D=/var/spool/uft/$U
if [ ! -d $D ] ; then
echo "$Z: UFT configuration problem: no spool directory" 1>&2
exec ls -ld $D ; fi             # 'ls' here throws an informative error

#
# clean-up the spool ID .. four digits for historical reasons
S="$1"
if [ -z "$S" ] ; then S=1 ; fi
S=`echo "0000$S" | awk '{print substr($0,length($0)-3,4)}'`

#
# extract the metadata and load it into this shell
grep -v '^#' $D/$S.cf | awk '{print "UFT_" $0}' | grep -v '=$' \
    | grep -v '`' | grep -v ';' | grep -v '(' | grep -v ')' \
    | grep -v '^$' > /tmp/$$.set
RC=$? ; if [ $RC -ne 0 ] ; then rm -f /tmp/$$.set ; exit $RC ; fi
. /tmp/$$.set
rm /tmp/$$.set

#
# file needs a name - command line can override or specify a name
T="$2"
#if [ -z "$T" ] ; then T="$UFT_NAME" ; fi
if [ -z "$T" ] ; then T=`basename "$UFT_NAME"` ; fi
if [ -z "$T" ] ; then
    echo "$Z: UFT file $S needs a name" 1>&2
    exit 1 ; fi
# files might be sent unnamed, such as output from a command

#
# report and receive
echo "$Z: receiving UFT file $S as '$T'"

#
# be selective about canonicalization
case "$UFT_TYPE" in
    a*|A*|t*|T*)                # "ASCII" or plain text
        cat $D/$S.df | sed 's#\r$##' > $T
        RC=$?
        ;;
    n*|N*)                      # IBM NETDATA (convert to plain text)
        uftcndd $D/$S.df > $T
        RC=$?
        ;;
    *)                          # all others treat as binary
        cat $D/$S.df > $T
        RC=$?
        ;;
esac
if [ $RC -ne 0 ] ; then exit $RC ; fi

#
# conditionally stamp the original time on the file
if [ ! -z "$UFT_DATE" ] ; then touch -d "$UFT_DATE" $T 2> /dev/null ; fi

#
# conditionally set original permissions on the file
if [ ! -z "$UFT_PROT" ] ; then
    echo "$UFT_PROT" | xargs -d , -n 1 -r \
        | grep '^U:' | tr A-Z a-z | sed 's#u:#u+#' \
        | awk '{print "chmod" , $0 , "#"}' | sed "s/#/$T/" | sh
    echo "$UFT_PROT" | xargs -d , -n 1 -r \
        | grep '^G:' | tr A-Z a-z | sed 's#g:#g+#' \
        | awk '{print "chmod" , $0 , "#"}' | sed "s/#/$T/" | sh
    echo "$UFT_PROT" | xargs -d , -n 1 -r \
        | grep '^W:' | tr A-Z a-z | sed 's#w:#o+#' \
        | awk '{print "chmod" , $0 , "#"}' | sed "s/#/$T/" | sh
fi

#
# if that worked then discard the spool file (all components)
yes | rm $D/$S.*

exit

# some example UFT file attributes:

UFT_REMOTE=''                           # the sending user@host if available
UFT_SIZE='36K'                          # estimated size reported by sender
UFT_FROM='ltroth3'                      # sending user per the FILE command
UFT_USER='rmt'                          # intended recipient
UFT_TYPE='I'                            # text? binary? or something else?
UFT_NAME='uftcreqs.tar'                 # the name of the file, if available
UFT_DATE='2024-04-19 20:22:31'          # date stamp on the file being sent
UFT_RECFM='F'                           # record format, if applicable
UFT_LRECL='512'                         # record length, if applicable


