#!/bin/bash
#This is the single top-level launch script for all VMs in the OS museum. It
#handles locking the VM to prevent multiple running instances, spawning a
#terminal if required, downloading VMs on demand when they haven't yet been
#installed, and looking up the correct emulator/machine-specific boot script to
#run.
#
#Every VM in the OS museum is in the form of a "machine config" directory with
#a name of the form <short name>_config. For a VM with a single installation,
#the short name of the machine config will be the same as that of the
#single installation. Alongside the machine config directory is a symlink to
#this script with a name of the form <short name>_boot (which is used to run
#that particular installation). For each machine config that contains multiple
#installations, there are pairs of <short name>_boot(2|) and <short name>_config
#symlinks, with the boot symlink pointing to this script and the config one to
#the machine config.

#It is possible to mark an installation as a secondary one
#by using a boot symlink of the form <short name_boot2, which means that the
#installation isn't booted directly by running the boot script and is somehow
#dependent on a primary installation (most often this means that the secondary
#OS runs hosted under the primary one); in this case instead of being a direct
#symlink to this script, it should instead point to the _boot symlink of the
#corresponding primary installation.

#It is also possible to have multiple primary installations in one machine
#config directory; this should only be done for something like a multiboot
#setup where the OS that is booted con be changed by passing options to the
#emulator, rather than hosted setups where one OS runs on top of another. In
#this case, the short name of the machine config doesn't have to correspond to
#the name of an installation.

#The top level of the collection is in the images/
#directory. Subdirectories of the top level represent platforms. It is
#optionally possible to have a second level of subdirectories within a platform
#subdirectory for sub-platforms. Subdirectories of platform subdirectories
#represent families, which may have any number of nested levels. Installations
#may be symlinked into any number of platform and family subdirectories as
#appropriate. <short name>_boot symlinks should always point directly to this
#script regardless of whether the corresponding <short name>_config is a symlink
#or a machine config.

#Most installations use a standard boot script in scripts/lib/std_boot.d; the
#script to run is determined by the Boot-Script: field in the installation info
#file. If this isn't present, the script is instead taken from the
#Default-Boot-Script: field in the platform info file. Installations may also
#use a BOOT.<short_name> script in the machine config directory; this takes
#precedence over the Default-Boot-Script: field (having both a BOOT.<short name>
#script and a Boot-Script: field is an error).

real_script_path="`realpath $0`"
real_script_dir="`dirname $real_script_path`"
ORIG_PWD="$PWD"
. "$real_script_dir/bootscriptfunctions.sh"

if [ ! -L "$0" ]
then
	panic "This script cannot be run directly. It must be run through a symlink to <installation name>_boot in the same directory as the corresponding <installation name>_config directory."
fi

script_basename="`basename $0`"

cd "`dirname $0`"

OSM_BOOT_SCRIPT_LINK="`realpath -s $script_basename`"
if echo "$OSM_BOOT_SCRIPT_LINK" | grep -q '_boot2$'
then
	cd "`dirname "$OSM_BOOT_SCRIPT_LINK"`"
	OSM_BOOT_SCRIPT_LINK="`readlink $script_basename`"
	if [ ! -L "$OSM_BOOT_SCRIPT_LINK" ]
	then
		panic "secondary boot script link target is not another symlink (must be a symlink to the primary boot script link)"
	fi
fi

basename="`basename $OSM_BOOT_SCRIPT_LINK`"
OSM_INSTALL_NAME=`basename $OSM_BOOT_SCRIPT_LINK | sed -E s/_boot2\?\$//`

configparent="`dirname "$OSM_BOOT_SCRIPT_LINK"`"
configparent="`realpath $configparent`"
OSM_CONFIG_DIR="$configparent/${OSM_INSTALL_NAME}_config"

machine_config_dir="`realpath $OSM_CONFIG_DIR`"
if ! echo $machine_config_dir | grep -q '_config$'
then
	panic "Machine configuration directory path $machine_config_dir does not end in _config"
fi

config_fields="`print_info -f boot_script,platforms,families,emulator_version \"$OSM_BOOT_SCRIPT_LINK\"`"

if ! cd "$OSM_CONFIG_DIR"
then
	panic "cannot cd to emulator configuration $OSM_CONFIG_DIR"
fi

if [ ! "$config_fields" ]
then
	panic "cannot get info for boot script $OSM_BOOT_SCRIPT_LINK"
fi

config_boot_script="`echo \"$config_fields\" | cut -f1`"
platform="`echo \"$config_fields\" | cut -f2`"
family="`echo \"$config_fields\" | cut -f3`"
OSM_EMULATOR_VERSION="`echo \"$config_fields\" | cut -f4`"
if [ x"$OSM_EMULATOR_VERSION" = x- ]
then
	unset OSM_EMULATOR_VERSION
else
	export OSM_EMULATOR_VERSION
fi


if [ "$config_boot_script" = "-" ]
then
	platform_info="$OSM_IMAGE_DIR/$platform/INFO"
	if [ ! -f "$platform_info" ]
	then
		platform_info_dir="$OSM_CONFIG_DIR"
		while [ "$platform_info_dir" ]
		do
			platform_info_dir="`dirname $platform_info_dir`"
			platform_info="$platform_info_dir/INFO"
			if [ -f "$platform_info" ]
			then
				break
			fi
		done
	fi
	if [ ! -f "$platform_info" ]
	then
		panic "no info file found for platform $platform"
	fi

	boot_script="`print_info -f default_boot_script \"$platform_info\" | cut -f1`"
else
	boot_script="$config_boot_script"
fi

if [ "$config_boot_script" == "_custom_" ]
then
	config_boot_script="-"
fi

if [ -x "BOOT.$OSM_INSTALL_NAME" ]
then
	if [ "$config_boot_script" != '-' ]
	then
		panic "both a Boot-Script info field and a BOOT.$OSM_INSTALL_NAME file found in the same directory"
	fi
	config_boot_found=1
fi

if [ "$config_boot_found" ]
then
	boot_script_path="$PWD/BOOT.$OSM_INSTALL_NAME"
elif [ ! "$boot_script" ]
then
	panic "boot script path is null"
elif [ "$boot_script" = "_custom_" ]
then
	panic "installation/scenario $OSM_INSTALL_NAME specifies _custom_ as a boot script but doesn't include a BOOT.$OSM_INSTALL_NAME script"
elif [ "$boot_script" != "-" ]
then
	boot_script_path="$OSM_SCRIPT_DIR"/lib/std_boot.d/$boot_script
else
	panic "no boot script configured in platform or installation info files and no BOOT.$OSM_INSTALL_NAME file found"
fi

if grep -q '^###[[:space:]]*DYNAMIC INSTALLATION DEFAULTS' $boot_script_path
then
	is_dynamic="true"
fi

if [ ! -x "$boot_script_path" ]
then
	panic "boot script $boot_script_path nonexistent or non-executable"
fi

if [ "$OSM_BOOTSCRIPT_RUN_MODE" = "dynamic_info" -a "$is_dynamic" ]
then
	echo "Static-Info-Path: $boot_script_path"
	exit
fi

if [ "$is_dynamic" ]
then
	get_info_command="$boot_script_path"
else
	get_info_command="cat"
	script_info="$OSM_ROOT/info/emulator_defaults/`basename $boot_script_path`"
	if [ ! -f "$script_info" ]
	then
		script_info="$boot_script_path"
	fi
fi

launch_mode_field="`OSM_BOOTSCRIPT_RUN_MODE='dynamic_info' \"$get_info_command\" $script_info | grep 'Launch-Mode:' | head -n 1`"

if [ ! "$launch_mode_field" ]
then
	launch_mode_field="Launch-Mode: gui-and-terminal"
fi

if [ ! -t 0 ]
then
	if echo "$launch_mode_field" | grep -q terminal
	then
		cd $OLDPWD
		run_terminal_if_needed "$@"
	fi
fi

use_faketree=""
if echo "$launch_mode_field" | grep -q ',faketree'
then
	use_faketree="true"
fi

machine_config_name="`basename $machine_config_dir | sed 's/_config$//g'`"

acquire_guest_lock -n -u "$machine_config_name"

export OSM_ROOT OSM_BOOT_SCRIPT_LINK OSM_INSTALL_NAME OSM_CONFIG_DIR

download_required()
{
	download_installation -c "$OSM_CONFIG_DIR"
	download_status="$?"
	case "$download_status" in
		0) return 1 ;;
		2) return 0 ;;
		*) panic "checking installation status with download_installation failed with status $?" ;;
	esac
}

if is_os_museum_vm && download_required
then
	download_installation "$OSM_CONFIG_DIR"
fi

if [ -t 1 ]
then
	pause_on_fail=
else
	pause_on_fail=pause_on_fail
fi

if [ "$use_faketree" ]
then
	faketree --propagate --wait-timeout 5s $pause_on_fail "$boot_script_path" "$@"
else
	$pause_on_fail "$boot_script_path" "$@"
fi
