#!/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/Pi, 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.
###############################################################################

# Version info
SHORTVERSION=182U64
LONGVERSION="18.2 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)"
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
if [ ! -d "${MYCONFIGDIR}" ] ; then
	mkdir -p "${MYCONFIGDIR}"
	FIRSTRUN=true
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/__DYALOG__/`sedarg "${DYALOG}"`/g" \
	 	-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
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

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

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