#!/bin/ksh

#ident "@(#)opensrc:cups-1.1.19/printjobGUI /main/uw713up3bl3/1"

# messages
MSG_NOBROWSER="\
A browser is required to administer the CUPS subsystem.
No browser was found on this system.  Since you don't have one
installed, you must use a remote browser for administration."

MSG_STOPADMINLATER="\
CUPS server has been started for you. To stop the server,
execute the command \"/etc/init.d/cups stop\" as root."

MSG_CANTSTARTSERV="CUPS server could not be started."

#############################################################################
#
# display_message
#
#############################################################################
display_message()
{
	# set up some VTCL values from the arguments
	if [ "$1" = "error" ]
	then
		vtcl_type="VtErrorDialog"
	else
		vtcl_type="VtWarningDialog"
	fi
	vtcl_msg=`echo "$2" | sed -e 's/\"/\\\"/g'`

	TMPFILE="/tmp/$(basename $0).$$"

	# build a small VTCL script to create the dialog
	cat > $TMPFILE << EOF
		proc QuitCB { cbs } {
			VtClose; exit 0
		}

		set app [ VtOpen info ]
		set dialog [ ${vtcl_type} \$app.main \
			-message "${vtcl_msg}" \
			-ok \
			-okCallback QuitCB \
			-wmDecoration {TITLE BORDER MENU} \
			]
		VtShowDialog \$dialog
		VtMainLoop
EOF
	# display the error dialog, then remove the temporary file
	vtcl $TMPFILE
	rm $TMPFILE
}


#############################################################################
#
# main
#
#############################################################################

if [ -z "$DISPLAY" ]
then
	BROWSER=$(whence lynx)
else
	BROWSER=$(whence mozilla)
	[ -z "$BROWSER" ] && BROWSER=$(whence netscape)
fi

if [ ! -x "$BROWSER" ]
then
	display_message error "$MSG_NOBROWSER"
	exit 0
fi

# if cupsd is not running, start it now.
#
if LANG=C /etc/init.d/cups status | grep -q 'scheduler is not running'
then
	if LANG=C /etc/init.d/cups start | grep -q 'unable to start scheduler'
	then
		display_message error "$MSG_CANTSTARTSERV"
		exit 0
	fi

	if [ -z "$CHARM" ]
	then
		display_message warning "$MSG_STOPADMINLATER" &
	else
		display_message warning "$MSG_STOPADMINLATER"
	fi
fi

# start browser
#
URL="http://localhost:631/jobs"

if [ -n "$DISPLAY" ]
then
	$BROWSER -remote 'ping()' >/dev/null 2>&1
	if [ $? -eq 0 ]
	then
		URL="-remote openURL($URL,new-window)"
	fi
fi

$BROWSER $URL
exit $?
