# @(#)jm 1.5 90/06/19
#!/bin/sh
#include <xview/xview.h>
#include <xview/panel.h>
#include <xview/notice.h>
#include <xview/font.h>


################ DEFINE FUNCTIONS ##############

add_to_env() {					# Set environment variables.
	while read line; do
		if [ "${line}" = "End:" ]; then
			return 0
		else
			eval ${line}		# Set env. variable, assume "var=value"
			export `echo ${line} | awk -F"=" '{print $1}'` 
		fi
	done
}

execute() {					# Run executable commands and programs.
	starting_place=`pwd`
	while read line; do
		if [ "${line}" = "End:" ]; then
			cd ${starting_place}	# Script may have changed our location.
			return 0
		else
			eval ${line}
		fi
	done
}

play() {					# Play journaling scripts.
	while read line; do
		if [ "${line}" = "End:" ]; then
			return 0
		else
			eval sh ${BIN}/play ${line}
		fi
	done
}

exit_with_message() {
	error_message=$1
	if [ "${error_message}" ]; then
		echo
		echo ${error_message}
	fi
	echo
	echo Usage: '	jm script'
	echo
	echo '	Where "script" is of the form recognized by the journal manager.'
	echo
	exit 1
}


#################### START MAIN BODY ##########################

TUTORIAL_HOME=${TUTORIAL_HOME:-$OPENWINHOME/demo/tutorial}
HOME=${TUTORIAL_HOME}
BIN=${TUTORIAL_HOME}
path=${BIN}:$PATH
export path

Secure_Status=`psh << '--PSH_EOF--'
  /check {
      createevent /Synthetic get
  } def
  check { (enabled\n)  print }
        { (disabled\n) print } ifelse
--PSH_EOF--`

if [ "$Secure_Status" = "enabled" ]
then
        ${TUTORIAL_HOME}/notice security_on
        exit 0
fi

high_resolution_monitor=`psh << '--PSH_EOF--'
	/high_res? {
		framebuffer setcanvas
		clippath pathbbox
		4 1 roll
		pop pop pop
		900 gt
	} def
	high_res? { (true\n)  print }
		  { (false\n) print } ifelse
--PSH_EOF--`

if [ "$high_resolution_monitor" = "true" ]
then
        ${TUTORIAL_HOME}/notice high_res
        exit 0
fi


		### HANDLE STARTUP ERRORS ###

[ $# != 1 ]		&& exit_with_message

script=$1
[ ! -f ${script} ]	&& exit_with_message "Script, ${script}, is not a file."
[ ! -r ${script} ]	&& exit_with_message "Can't read script: ${script}."
[ ! -s ${script} ]	&& exit_with_message "Script file, ${script}, is empty."

if [ -z "$OPENWINHOME" ]; then
	echo
	echo You must be running OpenWindows to run this program.
	echo
	exit 1
fi

ps ax | fgrep olwm | fgrep -v fgrep 1> /dev/null 2>&1	# Running olwm?
running_olwm=$?				# Exit status of last fgrep
if [ ${running_olwm} != 0 ]; then
	echo You must be running the OPENLOOK window
	echo manager, olwm, to run this program.
	exit 1
fi

		### MAIN CODE ###

${BIN}/jmroot &		# Cover the whole screen with the root canvas.

exec < ${script}		# Get input from this file

while read line; do
	case ${line} in
		"Environment:")	add_to_env		;;
		"Executables:")	execute			;;
		"Journals:")	play			;;
		*) exit_with_message "Found ERROR in script\'s format."
							;;
	esac
done

exit 0
