#!/bin/sh

# @(#) obtain 1.12 Delta: 95/04/13 14:43:53 Extraction: 95/04/13 14:44:05 @(#)

name=`basename $0`
if test $# -ne 1
then
        echo "$name: usage: $name type"
        exit 0
fi
case $1 in
uid|uids) suffix=uid
        ;;
gid_id|gid_ids) suffix=gid_id
        ;;
gid|gids) suffix=gid
        ;;
lname|lnames) suffix=lname
        ;;
printer|printers) suffix=printers
        ;;
printcap) suffix=printcap
        ;;
*)      echo "$name: type $1 is not valid."
        exit 1
        ;;
esac

if [ $suffix = "printers" ]
then
        TO=flp.printers
        HOST=fnprt.fnal.gov
        FROM=flpr-defaults.latest
elif [ $suffix = "printcap" ]
then
        TO=printcap
        HOST=fnprt.fnal.gov
        FROM=printcap.latest
else
        HOST=giduid.fnal.gov
        FROM=${suffix}.lis
        if [  -f unix.${suffix}.list ]
        then
                TO=`find unix.${suffix}.list -mtime +1 -print`
        else
                TO=unix.${suffix}.list
        fi
fi

if [ "x$TO" = "x" ]
then
        echo "A copy of the ${suffix} file has been retrieved in the past day."
        echo "Do you want to retrieve another copy (y/n)? "
        read answer
        answer=`echo $answer | cut -c1`
        if [ "x$answer" = "xy" -o "x$answer" = "xY" ]
        then
                TO=unix.${suffix}.list
        else
                exit 0
        fi
fi
        
trap 'rm -fr /tmp/$$.*' 0 1 2 3 4 5 6 7 8 10 12 13 15
shift
eval $*
WORK1=/tmp/$$.work1
WORK2=/tmp/$$.work2

su - obtain -c "rcp obtain@giduid:${suffix}.lis $WORK1"

case $suffix in
uid)
sed -n -e "/^[  ]*[0-9][0-9]*[  ]/p" $WORK1 |
tr '[A-Z]:' '[a-z]_' >$WORK2
cut -c1-7   $WORK2 |
  sed -e "s/[   ][      ]*/ /g" -e "s/^ //" -e "s/ $//" >/tmp/$$.col1
cut -c9-16  $WORK2 |
  sed -e "s/[   ][      ]*/ /g" -e "s/^ //" -e "s/ $//" >/tmp/$$.col2
cut -c21-43 $WORK2 |
  sed -e "s/[   ][      ]*/ /g" -e "s/^ //" -e "s/ $//" |
  /usr/local/bin/namefix >/tmp/$$.col3
cut -c45-62 $WORK2 |
  sed -e "s/[   ][      ]*/ /g" -e "s/^ //" -e "s/ $//" |
  /usr/local/bin/namefix >/tmp/$$.col4
cut -c64-   $WORK2 |
  sed -e "s/[   ][      ]*/ /g" -e "s/^ //" -e "s/ $//" >/tmp/$$.col5
paste -d':: :' /tmp/$$.col[12] /tmp/$$.col4 /tmp/$$.col3 /tmp/$$.col5 >$TO
        ;;
gid_id)
sed -n -e "/^[  ]*[0-9][0-9]*[  ]/p" $WORK1 |
tr '[A-Z]:' '[a-z]_' |
sed -e "s/[     ][      ]*/:/" -e "s/[  ][      ]*/:/" >$WORK2
cut -d: -f1 $WORK2 >/tmp/$$.col1
cut -d: -f2 $WORK2 | sed -e 's/^-*$//' >/tmp/$$.col2
cut -d: -f3 $WORK2 |
  sed -e "s/[   ][      ]*/ /g" -e "s/^ //" -e "s/ $//" |
  /usr/local/bin/namefix |
  sed -e "s/Cdf/CDF/g" -e "s/'S /'s /g" >/tmp/$$.col3
paste -d':' /tmp/$$.col? >$TO
        ;;
*)
mv $WORK1 $TO
        ;;
esac

