#!/bin/bash
#
# LWDAQ Launch Program for MacOS, Windows, Linux, and Raspbian.
#
# Copyright (C) 2004-2021 Kevan Hashemi, Brandeis University
# Copyright (C) 2021-2023 Kevan Hashemi, Open Source Instruments Inc.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.

# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.

# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA  02111-1307, USA

# This program launches LWDAQ from a terminal on Linux, Raspbian, or MacOS. On
# Windows, you can run the shell from GitBash or the Windows Linux shell. The
# shell program accepts options --no-gui, --gui, --no-console, --pipe, or
# --spawn, as described in the LWDAQ manual. The default is --gui. After the
# option you can pass a TCL script that will be executed by LWDAQ after it
# starts up. This script is the configuration script. The full path name of the
# configuration script will be available in the global variable LWDAQ global
# variable LWDAQ_Info(configuration_file). If you specify a configuration script
# you can pass parameters into it by adding them to your command line. These
# parameters will be placed in the global variable LWDAQ_Info(argv). The first
# argument after the configuration script will be element zero in the list. The
# following line calls tclsh in the background and runs the tcl script
# config.tcl, passing two parameters into LWDAQ_Info(argv), data.txt and 34.58.
#
# ./lwdaq --no-console config.tcl data.txt 34.58
#

# Determine the location of the LWDAQ directory. We assume that this script is
# in the LWDAQ directory.
LWDAQ_DIR=`dirname $0`

# Determine operating system and set default options accordingly.
OS=Windows
if [ "`uname -a | grep -i Darwin`" != "" ];
then 
	OS=MacOS
	OPTION="--gui"
	VERBOSE=0
	CONFIG_FILE=""
	PROMPT=""
fi
if [ "`uname -a | grep -i Windows`" != "" ];
then 
	OS=Windows
	OPTION="--spawn"
	VERBOSE=0
	CONFIG_FILE=""
	PROMPT=""
fi
if [ "`uname -a | grep -i Linux`" != "" ];
then 
	OS=Linux
	if [ "`cat /etc/os-release | grep -i Raspbian`" != "" ];
	then
		OS=Raspbian
	fi 
	OPTION="--gui"
	VERBOSE=0
	CONFIG_FILE=""
	PROMPT=""
fi

# Go through the options, all of which begin with two dashes, then read the
# configuration file name, if it exits. After that, further arguments will
# be retained to pass into LWDAQ. The default launch option is --gui.
while [[ $1 == --* ]];
do
	case "$1" in
		"--quiet") VERBOSE=0;;
		"--verbose") VERBOSE=1;;
		"--prompt") PROMPT="--prompt";;
		"--no-prompt") PROMPT="--no-prompt";;
		*) OPTION=$1;;
	esac
	shift 1
done
CONFIG_FILE=$1
shift 1

# Check to see if we should run in no-gui mode with tclsh or with gui in wish,
# and also whether we should run in the background. The run mode options also
# dictate whether or not the console will be enabled, but this decision is made
# inside LWDAQ, so we are going to pass the OPTION into LWDAQ when we run LWDAQ.
OPTION_FOUND=0
if [ "$OPTION" == "--pipe" ]; 
then 
	# Run without graphics, without console, in foreground.
	GUI_ENABLED=0
	RUN_IN_BACKGROUND=0
	PROMPT="--no-prompt"
	OPTION_FOUND=1
fi
if [ "$OPTION" == "--no-console" ]; 
then 
	# Run without graphics, without console, in background.
	GUI_ENABLED=0
	RUN_IN_BACKGROUND=1
	PROMPT="--no-prompt"
	OPTION_FOUND=1
fi
if [ "$OPTION" == "--no-gui" ]; 
then 
	# Run without graphics, with console, in foreground. If the prompt has not
	# yet been turned on or off, turn it on.
	GUI_ENABLED=0
	RUN_IN_BACKGROUND=0
	if [ "$PROMPT" == "" ]
	then
		PROMPT="--prompt"
	fi
	OPTION_FOUND=1
fi
if [ "$OPTION" == "--gui" ]; 
then 
	# Run with graphics, with console, in foreground. If the prompt has not yet
	# been turned on or off, turn it on.
	GUI_ENABLED=1
	RUN_IN_BACKGROUND=0
	if [ "$PROMPT" == "" ]
	then
		PROMPT="--prompt"
	fi
	OPTION_FOUND=1
fi
if [ "$OPTION" == "--spawn" ]; 
then 
	# Run with graphics, without console, background.
	GUI_ENABLED=1
	RUN_IN_BACKGROUND=1
	PROMPT="--no-prompt"
	OPTION_FOUND=1
fi

# If the option was not found, we issue a warning and set the flags that need to
# be set.
if [ $OPTION_FOUND == 0 ];
then
	echo "WARNING: Unrecognised option '$OPTION', defaulting to --no-gui --no-prompt."
	GUI_ENABLED=0
	RUN_IN_BACKGROUND=0
	PROMPT="--no-prompt"
fi

# Pick the TclTk shell based upon the options, operating system
# and architecture.
if [ $OS == Unknown ];
then
	if [ $GUI_ENABLED == 1 ];
	then
		SHELL=wish
	else
		SHELL=tclsh
	fi
fi
if [ $OS == Raspbian ];
then
	if [ $GUI_ENABLED == 1 ];
	then
		SHELL=wish
	else
		SHELL=tclsh
	fi
fi
if [ $OS == Linux ];
then
	LD_LIBRARY_PATH="$LWDAQ_DIR/LWDAQ.app/Contents/Linux/lib"
	export LD_LIBRARY_PATH
	if [ $GUI_ENABLED == 1 ]; 
	then
		SHELL="$LWDAQ_DIR/LWDAQ.app/Contents/Linux/bin/wish8.6"
	else
		SHELL="$LWDAQ_DIR/LWDAQ.app/Contents/Linux/bin/tclsh8.6"
	fi
fi
if [ $OS == MacOS ];
then
	if [ $GUI_ENABLED == 1 ];
	then
		SHELL="$LWDAQ_DIR/LWDAQ.app/Contents/MacOS/Wish"
	else
		SHELL="$LWDAQ_DIR/LWDAQ.app/Contents/MacOS/tclsh"
	fi
fi
if [ $OS == Windows ];
then
	if [ $GUI_ENABLED == 1 ];
	then
		SHELL="$LWDAQ_DIR/LWDAQ.app/Contents/Windows/bin/wish86.exe"
	else
		PATH="$PATH:$LWDAQ_DIR/LWDAQ.app/Contents/Windows/bin/"
		export PATH
		SHELL="$LWDAQ_DIR/LWDAQ.app/Contents/Windows/bin/tclsh86.exe"
	fi
fi

# Set the initialization script name.
INIT_FILE="$LWDAQ_DIR/LWDAQ.app/Contents/LWDAQ/Init.tcl"

# If verbose, print details of LWDAQ launch.
if [ $VERBOSE = 1 ];
then
	echo "OS: $OS"
	echo "OPTION: $OPTION"
	echo "PROMPT: $PROMPT"
	echo "GUI_ENABLED: $GUI_ENABLED"
	echo "RUN_IN_BACKGROUND: $RUN_IN_BACKGROUND"
	echo "LOCAL_DIR: `pwd`"
	if [ "$CONFIG_FILE" != "" ];
	then 
		echo "CONFIG_FILE: $CONFIG_FILE"
	else
		echo "CONFIG_FILE: None"
	fi
	echo "SHELL: $SHELL"
fi

# Check the configuration file.
if [ "$CONFIG_FILE" == "" ];
then 
	if [ "$OPTION" == "--no-console" ];
	then
		echo "ERROR: Option --no-console requires a configuration file."
		exit 1
	fi
fi

# If we are going to set up a terminal interface with a prompt, save terminal
# state now.
if [ $PROMPT == "--prompt" ] 
then
	TTY_STATE=`stty -g`
fi

# Launch the shell and pass to it the start-up script and any additional
# parameters we may have passed to this bash script. On Linux, to make sure that
# our child process are independent of their parents, we have to launch with the
# "no hang-up" command "nohup". This is the behavior we want when spawning new
# lwdaq processes with --spawn. When we launch with --no-console, we also run
# in the background, but we want our output to go to stdout, so we don't use
# the nohup.
if [ $RUN_IN_BACKGROUND == 1 ];
then
	if [ "$OS" == "Linux" ] || [ "$OS" == "Raspbian" ];
	then
		if [ "$OPTION" == "--spawn" ];
		then
			nohup $SHELL $INIT_FILE $OPTION $PROMPT $CONFIG_FILE "$@" & 
		else
			$SHELL $INIT_FILE $OPTION $PROMPT $CONFIG_FILE "$@" & 
		fi
	else
		$SHELL $INIT_FILE $OPTION $PROMPT $CONFIG_FILE "$@" &
	fi
else
	$SHELL $INIT_FILE $OPTION $PROMPT $CONFIG_FILE "$@"
fi

# If we were using a terminal interface with a prompt, restore the terminal
# interface now. 
if [ $PROMPT == "--prompt" ] 
then
	stty $TTY_STATE
fi

