:
#
# watson - for the finger distribution.
# Written by Keith Gabryelski
# Released into public domain September 1, 1988.
# Please keep this header.
#
# This is an interactive data base handler for the whois database.
#

novice_directory=/usr/lib/finger/watson

#
# getyorn() - SOP, get `y' or `n' and return some status relating to them.
#

getyorn()
{
    while :
    do
	echo "$1 [y/n]? \c"
	IFS="$NORMAL_IFS"
	read yorn dummy

	case $yorn in
	y*|Y*)
	    return 0
	    ;;

	n*|N*)
	    return 1
	    ;;
	esac

	echo "Please answer Y or N."
    done
}

#
# getyornorq()
#

getyornorq()
{
    while :
    do
	echo "$1 [y/n/q]? \c"
	IFS="$NORMAL_IFS"
	read yorn dummy

	case $yorn in
	y*|Y*)
	    return 0
	    ;;

	n*|N*)
	    return 1
	    ;;

	q*|Q*)
	    exit 1
	    ;;
	esac

	echo "Please answer Y, N, or Q."
    done
}

quiter()
{
   getyornorq "Do you really want to quit" && {
	rm -f "$watfile" "$remark" "$remarkfile"
	exit 1
	}
}

#
# get_line
#

get_line()
{
    name="$1"
    lastanswer="$2"

    stty quit \^\[
    IFS=

    while :
    do
	echo "Your current $name is\c"

	if [ "x$lastanswer" = "x" ]
	then
	    echo " blank."
	else
	    echo ":\n\t\"$lastanswer\""
	fi

	if [ $novice = TRUE ]
	then
	    echo "Enter a new $name, press the [Return] key to accept this one, or"
	    echo "press the [ESC] key to leave this field blank."
	fi

	echo "    New $name: \c"

	blankp=FALSE
	trap "blankp=TRUE" "3"

	read answer

	if [ $blankp = FALSE ]
	then
	    if [ "x$answer" = "x" ]
	    then
		trap "" 3
		stty quit $quit
		answer="$lastanswer"
		return 0
	    fi
	fi
	lastanswer="$answer"
    done
}

get_nickname()
{
    if [ "$novice" = TRUE ] && [ -r "$nickname_novice_file" ]
    then
	cat "$nickname_novice_file"
    fi

    get_line nickname "$nickname"

    if [ "x$nickname" != "x$answer" ]
    then
	nickname="$answer"
	changes_made=TRUE
    fi
}

get_workaddress()
{
    if [ "$novice" = TRUE ] && [ -r "$workaddress_novice_file" ]
    then
	cat "$workaddress_novice_file"
    fi

    get_line workaddress "$work_address"

    if [ "x$work_address" != "x$answer" ]
    then
	work_address="$answer"
	changes_made=TRUE
    fi
}

get_workphone()
{
    if [ "$novice" = TRUE ] && [ -r "$workphone_novice_file" ]
    then
	cat "$workphone_novice_file"
    fi

    get_line workphone "$work_phone"

    if [ "x$work_phone" != "x$answer" ]
    then
	work_phone="$answer"
	changes_made=TRUE
    fi
}

get_homeaddress()
{
    if [ "$novice" = TRUE ] && [ -r "$homeaddress_novice_file" ]
    then
	cat "$homeaddress_novice_file"
    fi

    get_line homeaddress "$home_address"

    if [ "x$home_address" != "x$answer" ]
    then
	home_address="$answer"
	changes_made=TRUE
    fi
}

get_homephone()
{
    if [ "$novice" = TRUE ] && [ -r "$homephone_novice_file" ]
    then
	cat "$homephone_novice_file"
    fi

    get_line homephone "$home_phone"

    if [ "x$home_phone" != "x$answer" ]
    then
	home_phone="$answer"
	changes_made=TRUE
    fi
}

get_project()
{
    if [ "$novice" = TRUE ] && [ -r "$project_novice_file" ]
    then
	cat "$project_novice_file"
    fi

    get_line project "$project"

    if [ "x$project" != "x$answer" ]
    then
	project="$answer"
	changes_made=TRUE
    fi
}

get_supervisor()
{
    if [ "$novice" = TRUE ] && [ -r "$supervisor_novice_file" ]
    then
	cat "$supervisor_novice_file"
    fi

    get_line supervisor "$supervisor"

    if [ "x$supervisor" != "x$answer" ]
    then
	supervisor="$answer"
	changes_made=TRUE
    fi
}

get_birthday()
{
    if [ "$novice" = TRUE ] && [ -r "$birthday_novice_file" ]
    then
	cat "$birthday_novice_file"
    fi

    get_line birthday "$birthday"

    if [ "x$birthday" != "x$answer" ]
    then
	birthday="$answer"
	changes_made=TRUE
    fi
}

get_remark()
{
    if [ "$novice" = TRUE ] && [ -r "$remark_novice_file" ]
    then
	cat "$remark_novice_file"
    fi

    answer="$remark"

    echo "Your current remark is:"
    grep -n "^" $answer | sed -n -e 's/^\([0-9]*:\)\(.*\)/\1 \2/' -e 's/^/Line /p'

    while :
    do
	echo "edit, quit, ?, or [Return]: \c"

	IFS="$NORMAL_IFS"
	read ans garbage

	case $ans in
	    \?)
		echo "?\t\tHelp list."
		echo "edit\t\tEdit remark using favorite editor."
		echo "quit\t\tUse current remark."
		echo "[Return]\tEnter remark in line by line."
		;;

	    e*)
		mv $answer $remarkfile
		$EDITOR $remarkfile
		break
		;;

	    q*)
		return 0
		;;

	    "")
		echo "Enter your remark.  A blank line ends input."

		> $remarkfile

#		linenumber=1

		stty quit \^\[

		IFS=
		while :
		do
#
# No longer handles linenumber because of "Stack Overflow" messages.
#		    echo "Line $linenumber: \c"
		    echo "Line: \c"

		    blankp=FALSE
		    trap "blankp=TRUE" "3"

		    read answer

		    if [ $blankp = FALSE ]
		    then
			if [ "x$answer" = "x" ]
			then
			    trap "" 3
			    stty quit $quit
			    break
			fi
		    fi

		    echo "$answer" >> $remarkfile

#		    linenumber=`expr "$linenumber" + 1`
		done
		break
		;;

	    *)
		echo "Invalid answer, try again."
		;;
	esac
    done

    mv $remarkfile $remark

    changes_made=TRUE
}

progname="$0"

if [ "$1" = "" ]
then
    username="$LOGNAME"
else
    username="$1"
fi

if [ "x$EDITOR" = "x" ]
then
    EDITOR=emacs
fi

nickname=
work_address=
work_phone=
home_address=
home_phone=
birthday=
project=
supervisor=
remark="/tmp/rem$$"

changes_made=FALSE

watfile="/tmp/watson$$"
remarkfile="/tmp/remark$$"

firstimer="$novice_directory/firstimer"

nickname_novice_file="$novice_directory/nickname"
homephone_novice_file="$novice_directory/homephone"
homeaddress_novice_file="$novice_directory/homeaddress"
workphone_novice_file="$novice_directory/workphone"
workaddress_novice_file="$novice_directory/workaddress"
project_novice_file="$novice_directory/project"
supervisor_novice_file="$novice_directory/supervisor"
birthday_novice_file="$novice_directory/birthday"
remark_novice_file="$novice_directory/remark"

quit=`stty -a | sed -n -e "s/.*quit = \([^; ]*\).*/\1/p"`

trap quiter "2"

if [ "$PG" = "" ]
then
    PG=cat
fi

#
# Do they already exist in the database?
#

sed -n -e "/^${username}/{
p
q
}" /etc/passwd > $watfile

pass=`cat $watfile`

if [ "$pass" = "" ]
then
    echo "There is no user \`$username' in /etc/passwd."
    exit 1
fi

passwd_uid=`sed -n -e "s/^${username}:[^:]*:\([^:]*\).*/\1/p" $watfile`
passwd_gid=`sed -n -e "s/^${username}:[^:]*:[^:]*:\([^:]*\).*/\1/p" $watfile`
fullname=`sed -n -e "s/^${username}:[^:]*:[^:]*:[^:]*:\([^:]*\).*/\1/p" $watfile`
home_dir=`sed -n -e "s/^${username}:[^:]*:[^:]*:[^:]*:[^:]*:\([^:]*\).*/\1/p" $watfile`
login_shell=`sed -n -e "s/^${username}:[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:\([^:]*\).*/\1/p" $watfile`

if inquire -exist $username
then
    echo "Entry \`$username' is for \"$fullname\"."
    novice=FALSE
else
    getyorn "Would you like help" && $PG $firstimer
    inquire -write $username
    novice=TRUE
fi

nickname="`inquire $username -nickname`"
work_address="`inquire $username -workaddress`"
work_phone="`inquire $username -workphone`"
home_address="`inquire $username -homeaddress`"
home_phone="`inquire $username -homephone`"
birthday="`inquire $username -birthday`"
project="`inquire $username -project`"
supervisor="`inquire $username -supervisor`"

inquire $username -remark > $remark

NORMAL_IFS="$IFS"

while :
do
    if [ "$novice" = TRUE ]
    then
	echo "Type \`?' for a list of commands."
    fi

    echo "watson> \c"
    IFS="$NORMAL_IFS"
    read command garbage
    case $command in
    "")
	;;

    \?)
	echo "?		abort		all		birthday	exit"
	echo "help		homeaddress	homephone	list		nickname"
	echo "nonovice	novice		project		quit		remark"
	echo "supervisor	whois		workaddress	workphone"
	;;

    abort|quit|q)
	if [ "$changes_made" = TRUE ]
	then
	    getyornorq "Quit without saving changes" && {
		rm -f "$watfile" "$remark" "$remarkfile"
		exit 1
		}
	else
	    rm -f "$watfile" "$remark" "$remarkfile"
	    exit 1
	fi
	;;

    help)
	$PG $firstimer
	;;

    list)
	echo "Nickname     = $nickname"
	echo "Work Address = $work_address"
	echo "Work Phone   = $work_phone"
	echo "Home Address = $home_address"
	echo "Home Phone   = $home_phone"
	echo "Birthday     = $birthday"
	echo "Project      = $project"
	echo "Supervisor   = $supervisor"
	echo "Remarks:"
	grep -n "^" $remark | sed -n -e 's/^\([0-9]*:\)\(.*\)/\1 \2/' -e 's/^/Line /p'
	;;

    nonovice)
	novice=FALSE
	echo "Novice mode OFF."
	;;

    novice)
	novice=TRUE
	echo "Novice mode ON."
	;;

#
# Either I'm broken or this Bourne shell is broken.
#

    "done"|"exit")
	if [ "$changes_made" = TRUE ]
	then
	    echo "Saving changes...\c"
	    inquire -write $username -nickname "$nickname" -homeaddress "$home_address" -homephone "$home_phone" -workaddress "$work_address" -workphone "$work_phone" -birthday "$birthday" -project "$project" -supervisor "$supervisor" -remark < $remark
	    echo " "
	fi

	rm -f "$watfile" "$remark" "$remarkfile"
	exit 0
	;;

    whois)
	finger "$username"
	terpri=FALSE

	if [ "x$nickname" != "x" ]
	then
	    echo "($nickname) \c"
	    terpri=TRUE
	fi

	if [ "x$home_dir" != "x" ]
	then
	    echo "<$home_dir> \c"
	    terpri=TRUE
	fi

	if [ "x$project" != "x" ] || [ "x$supervisor" != "x" ]
	then
	    echo "Hacking $project\c"

	    if [ "x$supervisor" != "x" ]
	    then
		echo " for $supervisor\c"
	    fi
	terpri=TRUE
	fi

	if [ $terpri = TRUE ]
	then
	    echo
	fi

	echo "[$passwd_uid, $passwd_gid] [$login_shell]\c"

	if [ "x$birthday" = "x" ]
	then
	    echo
	else
	    echo " Birthday $birthday"
	fi

	if [ "x$home_address" != "x" ] || [ "x$home_phone" != "x" ]
	then
	    echo "Home \c"

	    if [ "x$home_address" != "x" ]
	    then
		echo "$home_address; \c"
	    fi

	    if [ "x$home_phone" != "x" ]
	    then
		echo "Phone $home_phone\c"
	    fi

	    echo
	fi

	if [ "x$work_address" != "x" ] || [ "x$work_phone" != "x" ]
	then
	    echo "Work \c"

	    if [ "x$work_address" != "x" ]
	    then
		echo "$work_address; \c"
	    fi

	    if [ "x$work_phone" != "x" ]
	    then
		echo "Phone $work_phone\c"
	    fi

	    echo
	fi

	if [ "x`cat $remark`" != "x" ]
	then
	    cat $remark
	fi
	;;

    all)
	get_nickname
	get_birthday
	get_project
	get_supervisor
	get_homeaddress
	get_homephone
	get_workaddress
	get_workphone
	get_remark
	;;

    birthday)
	get_birthday
	;;

    homeaddress)
	get_homeaddress
	;;

    homephone)
	get_homephone
	;;

    nickname)
	get_nickname
	;;

    project)
	get_project
	;;

    remark)
	get_remark
	;;

    supervisor)
	get_supervisor
	;;

    workaddress)
	get_workaddress
	;;

    workphone)
	get_workphone
	;;

    !*)
	sh -c "`expr "x$command" : "x!\(.*\)"`"
	echo "!"
	;;

    *)
	echo "Invalid command \`$command'."
	echo "Type \`?' for a list of commands or type \`help' for help"
	;;

    esac
done

