#!/bin/csh -f
#----------------------------------------------------------------------
# Hi-Fi pizza orderer
# Written for the MIT AI Lab by Michael Frank and Mark Torrance.
# Copyleft 1991.
#----------------------------------------------------------------------

#
# Set constants
#

set libdir = /usr/local/lib/pizza
set tmpfile = pizza-order-$$.tmp
set menufile = $libdir/hifi.menu
set loclib = $libdir/Locations
set dest = 9W8648073
set faxopts =

#
# Change working directory to ~.
#
cd

#
# Parse arguments
#

while ($#argv >= 1)
	switch ($1)
	case -d:
	case -debug:
		set echo
		breaksw
	case -m:
	case -mail:
		set faxopts = ($faxopts -m)
		breaksw
	case -n:
	case -nosleep:
		unset sleep
		breaksw
	case -s:
	case -sleep:
		set sleep
		breaksw
	case -t:
	case -test:
		set dest=35060
		breaksw
	default:
		cat << endcat
Usage: $0 [-help] [-mail] [-nosleep] [-sleep] [-test]
Summary:
   Orders food by fax from 'Hi-Fi Pizza and Subs' for delivery to
   the MIT AI Lab.

Options:
   -d(ebug)	Debug by tracing execution.
   -h(elp)	Displays this message.
   -m(ail)	Mail user a message when fax is sent or rejected.
   -n(osleep)	Exit when done, even when running under xpizza.
   -s(leep)	When done, just sleep until user hits ^C (this
		option is turned on automatically by xpizza).
   -t(est)	Sends the fax to the AI lab fax machine on the 8th
		floor of NE43, instead of to Hi-Fi.

Description: The program is interactive and the prompts are fairly
   self-explanatory.  A menu (of food) is provided. If the lab is
   currently locked, the fax will contain instructions for the driver
   to call you up from the guard station.  Your floor, room, and phone
   number are obtained from the file ~/.Locations (or \$LOCATIONS),
   and are keyed off of the current hostname or some other keyword.
   One line per keyword, format "keyword floor room ext", e.g, "luke
   4th 417 3-3546".  The delimiter must be whitespace.  The fax is
   signed with a name that defaults to \$NAME.

endcat
		goto end
	endsw
	shift
end

#
# Startup message
#

echo "Welcome to the Hi-Fi pizza orderer for the MIT AI Lab."
echo "Created 1991 by Michael Frank and Mark Torrance."
echo "To exit, press ^C <RET> at any prompt."

#
# Determine whether Hi-Fi pizza is delivering at this hour.
#

set daywk = `date +%w`
set hour = `date +%H`

if ($daywk == 0) then
	# Sunday hours midnight-2am, noon-midnight
	if ($hour < 2 || $hour >= 12) set deliver
else if ($daywk >= 1 && $daywk <= 5) then
	# Monday-Friday hours 11am-midnight
	if ($hour >= 11) set deliver
else if ($daywk == 6) then
	# Saturday hours midnight-2am, 11am-midnight
	if ($hour < 2 || $hour >= 11) set deliver
endif

if (! $?deliver) then
	echo "I'm sorry, but Hi-Fi is not currently open for delivery."
	echo -n "Continue anyway (default no)? "
	if ("$<" !~ y*) goto end
endif

#
# Get the location to deliver to (default is current hostname).
#

set keyloc = `hostname`
echo ""
echo "Enter the hostname of some machine at the location you want the"
echo "food delivered to, or some other keyword for the location."
echo -n "(default $keyloc): "
set input = "$<"
if ($input != "") set keyloc = $input

#
# Create the locations file if it does not exist.
#

if ($?LOCATIONS) then
	set locfile = $LOCATIONS
else
	set locfile = ~/.Locations
endif
if (! -f "$locfile") then
	echo ""
	echo "Your personal locations file $locfile does not exist."
	echo -n "Create it (default yes)? "
	if ("$<" !~ n*) then
		if (-r $loclib) then
			echo "Copying $loclib to $locfile."
			cp $loclib $locfile
		else
			echo "Creating empty $locfile."
			touch $locfile
		endif
	else if (-r $loclib) then
		set locfile = $loclib
	endif
endif

#
# Look for the given location in the locations file.
#

if (-r "$locfile") then
	set locinfo = (`grep $keyloc $locfile`)
	if ($#locinfo >= 2) then
		set floor = $locinfo[2]
	endif
	if ($#locinfo >= 3) then
		set room = $locinfo[3]
	endif
	if ($#locinfo >= 4) then
		set ext = $locinfo[4]
	endif
endif

#
# If location unknown, ask for it, and add it to the locations file.
#

if (! ( $?floor && $?room && $?ext ) ) then
	echo ""
	echo "I don't know where $keyloc is."
	echo -n "Floor (e.g., 4th)? "
	set floor = "$<"
	echo -n "Room (e.g., 421)? "
	set room = "$<"
	echo -n "Extension (e.g., 3-2351)? "
	set ext = "$<"
	if (-w "$locfile") then
		echo "Appending this info to $locfile."
		echo >>$locfile "$keyloc $floor $room $ext"
	endif
endif

#
# Get the user's name.
#
if ($?NAME) then
	set name = "$NAME"
else
	set name = "<unset>"
endif
echo ""
echo -n "Your name (default $name)? "
set input = "$<"
if ("$input" != "") set name = "$input"

#
# Emergency defaults.
#
if ("$floor" == "") set floor = "<floor number here>"
if ("$room" == "") set room = "<room number here>"
if ("$ext" == "") set ext = "<phone extension here>"

#
# Show user the menu of food.
#
echo ""
echo -n "Do you want to see a menu (default yes)? "
if ("$<" =~ n*) goto skipmenu
if ($?DISPLAY) then
	xedit $menufile &
else
	if ($?PAGER) then
		$PAGER $menufile
	else
		more $menufile
	endif
endif
skipmenu:

#
# Determine whether the AI Lab is unlocked at this hour.
#
if ($daywk >= 1 && $daywk <= 5) then
	# Weekday hours 9-5
	if ($hour >= 9 && $hour < 17) then
		set doorsopen
	endif
endif

#
# On interrupts, goto "bye" label which makes sure $tmpfile has
# been removed.
#
onintr bye

#
# Compose the message.
#

if ($?doorsopen) then
#----------------------- doors unlocked message -----------------------
	cat >$tmpfile <<endcat
Dear Hi-Fi Pizza,

	I would like to place the following order for delivery.
Please deliver it to the MIT AI Lab, 545 Tech Square, on Main Street,
Cambridge, near Kendall Square. The cross-street is Vassar.  The doors
should all be unlocked, so the driver can come on up.  I'm on the
$floor floor, room: $room.
endcat
else
#----------------------- doors locked message -------------------------
cat >$tmpfile <<endcat
Dear Hi-Fi Pizza,

	I would like to place the following order for delivery.
Please deliver it to the MIT AI Lab, 545 Tech Square, on Main Street,
Cambridge, near Kendall Square. The cross-street is Vassar.  The
driver should come by the lobby and use the guard's phone to ring up
to my room ($room), at extension $ext. Then the driver should come up
to the $floor floor and meet me in the elevator lobby.
endcat
endif
#------------------------ rest of message -----------------------------
echo >>$tmpfile ""
echo >>$tmpfile "Here is my order:"
echo >>$tmpfile "----------------"
echo ""
echo "Enter your order. There is a" \$6.00 "minimum order for delivery."
echo "Press ^D when done."
cat >>$tmpfile
echo >>$tmpfile "----------------"
echo >>$tmpfile ""
echo >>$tmpfile "If there are any problems, please call me at 25$ext."
echo >>$tmpfile ""
echo >>$tmpfile "Thank you,"
echo >>$tmpfile "       $name"

#
# Verification phase.
#

loop:
echo "============================================================"
echo "Ok, I'm going to send the following to fax $dest."
echo "============================================================"
cat $tmpfile
echo "============================================================"
echo ""
echo -n "Edit (default no)? "
if ("$<" =~ y*) then
	if ($?EDITOR) then
		$EDITOR $tmpfile
	else
		if ($?DISPLAY) then
			xedit $tmpfile
		else
			emacs $tmpfile
		endif
	endif
	goto loop
endif

#
# Send the fax, and clean up.
#

echo "Sending fax. Please be patient; this may take a while."
if (-e /usr/local/bin/fax) then
	set echo
	/usr/local/bin/fax -p $dest $faxopts $tmpfile
	faxq
	unset echo
	echo "(Use 'faxq' to check send status.)"
else
	echo "Fax program not found -- rsh'ing to rice-chex."
	set echo
	rcp $tmpfile rice-chex.ai.mit.edu:
	rsh rice-chex.ai.mit.edu "/usr/local/bin/fax -p $dest $faxopts $tmpfile; /usr/local/bin/faxq; rm -f $tmpfile"
	unset echo
	echo "(Use 'rsh rice-chex.ai.mit.edu faxq' to check send status.)"
endif
rm -f $tmpfile
echo ""
echo "Fax sent! Your food should be delivered shortly."
echo "(If you want make sure your order arrived you may call Hi-Fi"
echo "by old-fashioned voice phone at 492-4600 or 354-9673."
end:
if ($?sleep) then
	echo "Press ^C to exit."
	sleep 1000000000 # Sleep a billion seconds.
endif
bye:
if ($?tmpfile) then
	if (-f $tmpfile) then
		echo ""
		echo "(Cleaning up)"
		rm -f $tmpfile
	endif
endif
