#!/bin/sh

###############################################################################
# Do not edit this script !
#
# If you want to set interpreter configuration parameters it is best to edit
# the configuration files in ~/.dyalog
#
# If you want to call other commands it is best to write your own script and
# set that to call this one.
#
# On Linux, if you want the "dyalog" command to point to a different
# version, run
#	sudo update-alternatives --config dyalog
# and select which version should run by default.
###############################################################################

# If we're on macOS 26 (or we guess, above) then by the time we get here when called 
# from the GUI, our PATH has been reset to something unwanted.  So set it back to the default
# If we've been called from the GUI, then RIDE_SPAWNED will have been set
# Note: you may also need to create $HOME/.dyalog _before_ running Dyalog APL; startup 
# appears to hang waiting for an invisible msgbox to be clicked on
#case $(uname) in
#Darwin)	if [ "x1" = "x$RIDE_SPAWNED" ]
#	then
#		if [ $(sw_vers -productVersion | sed 's/\..&$//') -ge 26 ]
#		then
#			NEWPATH=$(/usr/libexec/path_helper)
#			eval $NEWPATH
#			export PATH
#			unset $newpath
#		fi
#	fi
#	;;
#sac

# Version info
SHORTVERSION=200U64
LONGVERSION="20.0 64-bit Unicode"
REL="`echo "${LONGVERSION}" | sed 's/ .*$//'`"

# Find the Dyalog installation directory
if [ "$(uname)" = Linux ]; then
	# this script location, canonical.
	THIS="$(readlink -f $0)"
elif [ "$(uname)" = "Darwin" ]; then
	# Find the ultimate target of a symbolic link on MacOS
	# Only MacOS versions 12.X have the -f switch available
	# in readlink (and no `realpath`), so we need to roll our
	# own version.
	THIS="$0"
	while [ -h "$THIS" ]; do
		DIR="$(dirname "$THIS")"
		THIS="$(readlink "$THIS")"
		[[ $THIS != /* ]] && THIS="$DIR/$THIS"
	done
	THIS="$(cd "$(dirname "$THIS")"; pwd -P)/$(basename "$THIS")"
else
	# this script location.
	THIS="$0"
fi
export DYALOG=$(cd $(dirname $THIS) && pwd)

export APL_LANGUAGE_BAR_FILE=${DYALOG}/languagebar.json

if [ "$(uname)" = Linux ]; then
	if [ "x" = "x${LD_LIBRARY_PATH}" ]; then
		export LD_LIBRARY_PATH="${DYALOG}"
	else
		export LD_LIBRARY_PATH="${DYALOG}:${LD_LIBRARY_PATH}"
	fi
fi

# Setup the configuration directory
MYCONFIGDIR=${HOME}/.dyalog
FIRSTRUN=""
if [ ! -d "${MYCONFIGDIR}" ] ; then
	mkdir -p "${MYCONFIGDIR}"
	FIRSTRUN=true
fi

if [ "$(uname)" = Linux ]; then
	if [ "x" = "x${DISPLAY}" ]; then
		export ENABLE_CEF=0
	fi
fi

# Obtain and export the serial number
if [ "x" = "x${DYALOG_SERIAL}" ]; then
	SF="${DYALOG_SERIALFILE-${MYCONFIGDIR}/serial}"
	if [ -s ${SF} ] ; then
		DYALOG_SERIAL="$(cat ${SF} | tr -d "\r\n")"
	fi
fi
export DYALOG_SERIAL="${DYALOG_SERIAL-Unregistered}"

# Create interpreter config files if they don't exist
create_config () {
	if [ ! -s "$1" ]; then
		sedarg() {
			echo "$1" | sed 's/[/]/\\&/g'
		}
	
		# Create the file
	 	sed \
	 	-e "s/__SHORTVERSION__/`sedarg "${SHORTVERSION}"`/g" \
	 	-e "s/__LONGVERSION__/`sedarg "${LONGVERSION}"`/g" \
	 	-e "s/__REL__/`sedarg "${REL}"`/g" \
	 	"$2" > "$1"
	fi
}
UCF="${MYCONFIGDIR}/dyalog.${SHORTVERSION}.dcfg"
create_config "${UCF}" "${DYALOG}/dyalog.ver.dcfg.template"
create_config "${MYCONFIGDIR}/dyalog.dcfg" "${DYALOG}/dyalog.dcfg.template"

# Create SALT settings file if it doesn't exist
SSF="${MYCONFIGDIR}/SALT.${SHORTVERSION}.settings"
if [ ! -f "${SSF}" ] ; then
	cat > "${SSF}" << EOF
CommandFolder [HOME]/MyUCMDs:[SALT]/spice
SourceFolder .:[DYALOG]/Library/Core:[DYALOG]/Library/Conga:[DYALOG]/SALT
EOF
fi

# Process the command line
RUNAPLKEYS=false
RUNTIME=""
OPTS=""
WSID=""
while [ $# != 0 ] ; do
 	case $1 in
 	-tty)				# default, just ignore it
 		;;
 	-rt|-runtime)			# Runtime Only
 		RUNTIME=True
 		;;
 	-bg|--background)
 		RUNTIME=Background
 		;;
 	-kbd|--keyboard)
 		RUNAPLKEYS=true
 		;;
 	-nokbd|--no-keyboard)
 		RUNAPLKEYS=false
 		;;
 	-*)
 		OPTS="${OPTS} $1"	# Calling option.
 		;;
 	 *)
 		WSID="$1"		# Workspace id.
 		OPTS="${OPTS} $1"	# Calling argument.
 		;;
 	esac
 	shift				# Next parameter.
done

# First ever run and no explicit workspace - tell interpreter to run the intro
# workspace, if present
INTROWS=${DYALOG}/ws/intro.dws
if [ "${WSID}" = "" ] && [ "${FIRSTRUN}" = "true" ] && [ -s "${INTROWS}" ] ; then
 	OPTS="${OPTS} ${INTROWS}"
fi

# Initialise APL Keyboard if none is set
${RUNAPLKEYS} && "${DYALOG}/aplkeys.sh"

# Invoke interpreter
case ${RUNTIME} in
True)
 	"${DYALOG}/dyalog.rt" ${OPTS} 0<&- 1>&- 2>&- &
 	;;

Background)
 	if [ "`whoami`" = "root" ]; then
 		MYLOGDIR="/var/log/dyalog/session_logs"
 	else
 		MYLOGDIR="${MYCONFIGDIR}/session_logs"
 	fi
 	if ! [ -d "${MYLOGDIR}" ]; then
 		mkdir -p "${MYLOGDIR}"
 		chmod 777 "$MYLOGDIR"
 	fi
 	MYLOG="$MYLOGDIR/dyalog.out_$(date +'%y%m%d_%H%M%S')"
	export USERCONFIGFILE="${UCF}"
 	"${DYALOG}/dyalog" ${OPTS} 0<&- >"${MYLOG}" 2>&1 &
 	;;

*)
 	# download fonts
 	for APLFONTNAME in "${APLT1}" "${APLT}" "${TERM}" ; do
 		APLFONT="${DYALOG}/fonts/${APLFONTNAME}"
 		if test -f "${APLFONT}" ; then
 			cat ${APLFONT}
 			break
 		fi
 	done

	# Disable multi-line input by default in tty version unless started via RIDE
	if [ "x1" != "x$RIDE_SPAWNED" ]
	then
		export DYALOG_LINEEDITOR_MODE=${DYALOG_LINEEDITOR_MODE:-0}
	fi

 	# AUTO_PW on by default
 	export AUTO_PW=${AUTO_PW-1}

	export USERCONFIGFILE="${UCF}"
 	"${DYALOG}/dyalog" ${OPTS}
 	;;
esac
