# SCCS_ID - "@(#)ftpmgmt	1.4	86/07/01"
#
#   Copyright (c) 1986  The Wollongong Group, Inc.  All Rights Reserved
#
# Script to manipulate $LOGDIR/.netrc database used by FTP
#
LIB=/usr/ethernet/ua
HOSTFILE=/etc/hosts
NETRC=$LOGDIR/.netrc
HlpMsg="If autologin is enabled for any host, you will be logged in automatically when you use ftp to connect to that host."
Form=/tmp/Ftp$$.form
Menu=/tmp/Ftp$$.menu
Tmp=/tmp/win$$
cp $LIB/Ftp.tmplt $Form
#awk '/^[0-9]/ { print "Name=" $2 }' $HOSTFILE >> $Form
cat $LIB/HostTable >> $Form
trap "rm -f $Form $Menu $Tmp" 0
while true;do
	RET=`shform -u $Form`
	if [ $? -ne 0 ]
	then
		message "$Form is corrupted or missing!"
		exit 1
	fi
	set $RET
	case $1 in
	\'Enter\')
		eval Command=$2
		eval hname=$3
		case $Command in
		Add)
			eval lname=$4
			if [ "$hname" = "" -o "$lname" = "" ]
			then
				message "Hostname and login name must be specified"
				continue
			fi
			if grep -s "machine[ 	]*$hname[ 	]" $NETRC > /dev/null
			then
			     message "Auto login already enabled for \"$hname\""
			     continue
			fi
			eval passwd=$5
			if [ "$passwd" = "" ]
			then
			    echo "machine $hname login $lname" >> $NETRC
			else
			    echo "machine $hname login $lname password $passwd" >> $NETRC
			fi
			chmod 600 $NETRC
			message -c "Autologin enabled for \"$hname\""
			if [ $? -ne 0 ]
			then
				exit 0
			fi
			;;
		Delete)
			if [ "$hname" = "" ]
			then
				message "Hostname must be specified"
				continue
			fi
			if grep -s "machine[ 	]*$hname[ 	]" $NETRC > /dev/null
			then
				grep -v -s "machine[ 	]*$hname[ 	]" $NETRC > $Tmp
				cp $Tmp $NETRC
				message -c "Autologin disabled for \"$hname\""
				if [ $? -ne 0 ]
				then
					exit 0
				fi
			else
				message "Autologin not enabled for \"$hname\""
			fi
			;;
		Show)
			if [ ! -s $NETRC ]
			then
				message "User \"$LOGNAME's\" FTP database is empty"
				continue
			fi
echo "Menu=FTP Autologin Database" > $Menu
echo "Label=FTP autologins enabled by \"$LOGNAME\"" >> $Menu
echo "Prompt=An '*' indicates that the user will be prompted for the password"  >>$Menu
			awk '{ if(NF==6)pass=" ";else pass="*" ;\
				printf("Name=%-14.14s %s%s  \n",$2,pass,$4) }'\
							$NETRC >> $Menu
			clear
			shform -u $Menu > /dev/null
			clear
			;;
		esac
		;;
	\'Help\')
		message -i "$HlpMsg"
		;;
	\'Cancel\')
		exit 0
		;;
	*) message "Invalid Key Pressed."
		exit 1
		;;
	esac
done
