#!/bin/bash

INSTALL_TYPE=$1
INSTALL_SOURCE=$2
INSTALL_NEW=0

ORIG_PATH="${PATH}"

if [[ -z "${TARGET}" ]]; then
	if [[ ! -z "${WSL_DISTRO_NAME}" || ! -z "${WSL_INTEROP}" ]]; then
		TARGET="WSL"
	elif [[ "$(uname -s)" == "Linux" ]]; then
		TARGET="Wine"
	elif [[ "$(uname -o)" == "Msys" ]]; then
		TARGET="MSYS"
	else
		echo "This script expects to be ran from a Linux-like environment."
		exit 1
	fi
fi

if [[ "${TARGET}" == "WINE" || "${TARGET}" == "Wine" || "${TARGET}" == "wine" ]]; then
	WINEPREFIX="${WINEPREFIX:-${HOME}/.wine}"
	TARGET="Wine"
	DSDIR="${DSDIR:-${WINEPREFIX}/drive_c/DS}"
elif [[ "${TARGET}" == "WSL" || "${TARGET}" == "wsl" ]]; then
	TARGET="WSL"
	DSDIR="${DSDIR:-/mnt/c/DS}"
elif [[ "${TARGET}" == "MSYS" || "${TARGET}" == "Msys" || "${TARGET}" == "msys" ]]; then
	TARGET="MSYS"
	DSDIR="${DSDIR:-/c/DS}"
else
	echo 'Invalid $TARGET provided...'
	exit 1
fi
DSDIR_WIN32="${DSDIR_WIN32:-C:\\DS}"

P7Z_NAME="7z"
function install_7z {
	if ! which ${P7Z_NAME} >/dev/null 2>&1; then
		if [[ "${TARGET}" == "WSL" || "${TARGET}" == "Wine" ]]; then
			if which apt-get >/dev/null 2>&1; then
				sudo apt-get update
				sudo apt-get install p7zip
			fi
		elif [[ "${TARGET}" == "MSYS" ]]; then
			if which pacman >/dev/null 2>&1; then
				pacman -Sy msys/p7zip
			fi
		fi
	fi
	if ! which ${P7Z_NAME} >/dev/null 2>&1; then
		if [[ "${TARGET}" == "MSYS" ]]; then
			if [[ ! -e "${DSDIR}/Tools/7za.exe" ]]; then
				wget_file "${DSDIR}/Tools" "7zr.exe" "https://nephatrine.net/files/c3ds/windows/7zr.exe" #"http://www.7-zip.org/a/7zr.exe"
				wget_file "${DSDIR}/Downloads" "7z2200-extra.7z" "https://nephatrine.net/files/c3ds/windows/7z2200-extra.7z" #"https://www.7-zip.org/a/7z2200-extra.7z"
				cd "${DSDIR}/Tools" && "${DSDIR}/Tools/7zr.exe" e -y "${DSDIR}/Downloads/7z2200-extra.7z" "x64/7za.dll" "x64/7za.exe" "x64/7zxa.dll"
			fi
			if [[ -e "${DSDIR}/Tools/7za.exe" ]]; then
				P7Z_NAME="7za"
				export PATH="${DSDIR}/Tools:$ORIG_PATH"
			fi
		fi
	fi
	if ! which ${P7Z_NAME} >/dev/null 2>&1; then
		echo "Please install '${P7Z_NAME}' utility in PATH."
		exit 1
	fi
}

function install_bspatch {
	if ! which bspatch >/dev/null 2>&1; then
		if [[ "${TARGET}" == "WSL" || "${TARGET}" == "Wine" ]]; then
			if which apt-get; then
				sudo apt-get update
				sudo apt-get install bsdiff
			fi
		fi
	fi
	if ! which bspatch >/dev/null 2>&1; then
		if [[ "${TARGET}" == "MSYS" ]]; then
			wget_file "${DSDIR}/Downloads" "bsdiff_win_exe.zip" "https://nephatrine.net/files/c3ds/windows/bsdiff_win_exe.zip" #"http://download.pokorra.de/coding/bsdiff_win_exe.zip"
			unzip_file "${DSDIR}/Tools" "${DSDIR}/Downloads/bsdiff_win_exe.zip" "bspatch.exe"
			export PATH="${DSDIR}/Tools:$ORIG_PATH"
		fi
	fi
	if ! which bspatch >/dev/null 2>&1; then
		echo "Please install 'bspatch' utility in PATH."
		exit 1
	fi
}

function install_bunzip2 {
	if ! which bunzip2 >/dev/null 2>&1; then
		if [[ "${TARGET}" == "WSL" || "${TARGET}" == "Wine" ]]; then
			if which apt-get >/dev/null 2>&1; then
				sudo apt-get update
				sudo apt-get install bzip2
			fi
		elif [[ "${TARGET}" == "MSYS" ]]; then
			if which pacman >/dev/null 2>&1; then
				pacman -Sy msys/bzip2
			fi
		fi
	fi
	if ! which bunzip2 >/dev/null 2>&1; then
		echo "Please install 'bunzip2' utility in PATH."
		exit 1
	fi
}

function install_sed {
	if ! which sed >/dev/null 2>&1; then
		if [[ "${TARGET}" == "WSL" || "${TARGET}" == "Wine" ]]; then
			if which apt-get >/dev/null 2>&1; then
				sudo apt-get update
				sudo apt-get install sed
			fi
		elif [[ "${TARGET}" == "MSYS" ]]; then
			if which pacman >/dev/null 2>&1; then
				pacman -Sy msys/sed
			fi
		fi
	fi
	if ! which sed >/dev/null 2>&1; then
		echo "Please install 'sed' utility in PATH."
		exit 1
	fi
}

function install_tar {
	if ! which tar >/dev/null 2>&1; then
		if [[ "${TARGET}" == "WSL" || "${TARGET}" == "Wine" ]]; then
			if which apt-get >/dev/null 2>&1; then
				sudo apt-get update
				sudo apt-get install tar
			fi
		elif [[ "${TARGET}" == "MSYS" ]]; then
			if which pacman >/dev/null 2>&1; then
				pacman -Sy msys/tar
			fi
		fi
	fi
	if ! which tar >/dev/null 2>&1; then
		echo "Please install 'tar' utility in PATH."
		exit 1
	fi
}

function install_unzip {
	if ! which unzip >/dev/null 2>&1; then
		if [[ "${TARGET}" == "WSL" || "${TARGET}" == "Wine" ]]; then
			if which apt-get >/dev/null 2>&1; then
				sudo apt-get update
				sudo apt-get install unzip
			fi
		elif [[ "${TARGET}" == "MSYS" ]]; then
			if which pacman >/dev/null 2>&1; then
				pacman -Sy msys/unzip
			fi
		fi
	fi
	if ! which unzip >/dev/null 2>&1; then
		echo "Please install 'unzip' utility in PATH."
		exit 1
	fi
}

WGET_NAME="wget"
WGET_OPTS="-O"
function install_wget {
	if ! which ${WGET_NAME} >/dev/null 2>&1; then
		if [[ "${TARGET}" == "WSL" || "${TARGET}" == "Wine" ]]; then
			if which apt-get >/dev/null 2>&1; then
				sudo apt-get update
				sudo apt-get install wget
			fi
		elif [[ "${TARGET}" == "MSYS" ]]; then
			if which pacman >/dev/null 2>&1; then
				pacman -Sy msys/wget
			fi
		fi
	fi
	if ! which ${WGET_NAME} >/dev/null 2>&1; then
		if which curl >/dev/null 2>&1; then
			WGET_NAME="curl"
			WGET_OPTS="-L -o"
		fi
	fi
	if ! which ${WGET_NAME} >/dev/null 2>&1; then
		echo "Please install '${WGET_NAME}' utility in PATH."
		exit 1
	fi
}

function install_wine {
	if ! which wine >/dev/null 2>&1; then
		if [[ "${TARGET}" == "WSL" || "${TARGET}" == "Wine" ]]; then
			if which apt-get >/dev/null 2>&1; then
				sudo apt-get update
				sudo apt-get install wine
			fi
		fi
	fi
	if ! which wine >/dev/null 2>&1; then
		echo "Please install 'wine' utility in PATH."
		exit 1
	fi
}

function 7z_x_file {
	DESTPATH=$1
	FROMARCH=$2
	SRCFILES=$3
	OVERWRITE=$4

	if [[ ! -e "${DESTPATH}/${SRCFILES}" || "${SRCFILES}" == "*" || ! -z "${OVERWRITE}" ]]; then
		if [[ ! -e "${FROMARCH}" ]]; then
			echo "File '${FROMARCH}' does not exist."
			exit 1
		fi

		if [[ ! -e "${DESTPATH}" ]]; then mkdir -p "${DESTPATH}"; fi
		if [[ ! -e "${DESTPATH}" ]]; then
			echo "Unable to create '${DESTPATH}'. Please correct permissions."
			exit 1
		fi
	
		install_7z
		if [[ "${SRCFILES}" == "*" ]]; then
			cd "${DESTPATH}" && ${P7Z_NAME} x -o"${DESTPATH}" -y "${FROMARCH}"
		else
			cd "${DESTPATH}" && ${P7Z_NAME} x -o"${DESTPATH}" -y "${FROMARCH}" "${SRCFILES}"
		fi

		if [[ $? != 0 ]]; then
			if [[ "$(basename ${FROMARCH})" != "linuxcie.iso" ]]; then
				echo "Unable to extract archive '${FROMARCH}'."
				exit 1
			fi
		fi
	fi
}

function 7z_e_file {
	DESTPATH=$1
	FROMARCH=$2
	SRCFILES=$3
	OVERWRITE=$4

	if [[ ! -e "${DESTPATH}/$(basename ${SRCFILES})" || "$(basename ${SRCFILES})" == "*" || ! -z "${OVERWRITE}" ]]; then
		if [[ ! -e "${FROMARCH}" ]]; then
			echo "File '${FROMARCH}' does not exist."
			exit 1
		fi

		if [[ ! -e "${DESTPATH}" ]]; then mkdir -p "${DESTPATH}"; fi
		if [[ ! -e "${DESTPATH}" ]]; then
			echo "Unable to create '${DESTPATH}'. Please correct permissions."
			exit 1
		fi
	
		install_7z
		if [[ "${SRCFILES}" == "*" ]]; then
			cd "${DESTPATH}" && ${P7Z_NAME} e -o"${DESTPATH}" -y "${FROMARCH}"
		else
			cd "${DESTPATH}" && ${P7Z_NAME} e -o"${DESTPATH}" -y "${FROMARCH}" "${SRCFILES}"
		fi

		if [[ $? != 0 ]]; then
			if [[ "$(basename ${FROMARCH})" != "linuxcie.iso" ]]; then
				echo "Unable to extract archive '${FROMARCH}'."
				exit 1
			fi
		fi
	fi
}

function bspatch_file {
	OLDFILE=$1
	NEWFILE=$2
	PATCHFL=$3

	if [[ ! -e "${OLDFILE}" ]]; then
		echo "File '${OLDFILE}' does not exist."
		exit 1
	fi
	if [[ ! -e "${PATCHFL}" ]]; then
		echo "File '${PATCHFL}' does not exist."
		exit 1
	fi

	install_bspatch
	bspatch "${OLDFILE}" "${NEWFILE}" "${PATCHFL}"
	
	if [[ $? != 0 ]]; then
		echo "Unable to patch file '${OLDFILE}'."
		exit 1
	fi
}

function tar_x_file {
	DESTPATH=$1
	FROMARCH=$2

	if [[ ! -e "${FROMARCH}" ]]; then
		echo "File '${FROMARCH}' does not exist."
		exit 1
	fi

	if [[ ! -e "${DESTPATH}" ]]; then mkdir -p "${DESTPATH}"; fi
	if [[ ! -e "${DESTPATH}" ]]; then
		echo "Unable to create '${DESTPATH}'. Please correct permissions."
		exit 1
	fi
	
	install_tar
	tar -xf "${FROMARCH}" -C "${DESTPATH}"

	if [[ $? != 0 ]]; then
		echo "Unable to extract archive '${FROMARCH}'."
		exit 1
	fi
}

function unzip_file {
	DESTPATH=$1
	FROMARCH=$2
	SRCFILES=$3
	OVERWRITE=$4

	if [[ ! -e "${DESTPATH}/${SRCFILES}" || "${SRCFILES}" == "*" || ! -z "${OVERWRITE}" ]]; then
		if [[ ! -e "${FROMARCH}" ]]; then
			echo "File '${FROMARCH}' does not exist."
			exit 1
		fi

		if [[ ! -e "${DESTPATH}" ]]; then mkdir -p "${DESTPATH}"; fi
		if [[ ! -e "${DESTPATH}" ]]; then
			echo "Unable to create '${DESTPATH}'. Please correct permissions."
			exit 1
		fi
	
		install_unzip
		if [[ "${SRCFILES}" == "*" ]]; then
			unzip -o "${FROMARCH}" -d "${DESTPATH}"
		else
			unzip -o "${FROMARCH}" "${SRCFILES}" -d "${DESTPATH}"
		fi

		if [[ $? != 0 ]]; then
			echo "Unable to extract archive '${FROMARCH}'."
			exit 1
		fi
	fi
}

function wget_file {
	DESTPATH=$1
	FILENAME=$2
	FROMPATH=$3

	if [[ ! -e "${DESTPATH}" ]]; then mkdir -p "${DESTPATH}"; fi
	if [[ ! -e "${DESTPATH}" ]]; then
		echo "Unable to create '${DESTPATH}'. Please correct permissions."
		exit 1
	fi

	if [[ ! -e "${DESTPATH}/${FILENAME}" ]]; then
		install_wget
		${WGET_NAME} ${WGET_OPTS} "${DESTPATH}/${FILENAME}" "${FROMPATH}"

		if [[ $? != 0 ]]; then
			echo "Unable to download '${FROMPATH}'."
			exit 1
		fi
	fi
}

# Need Base Directory

if [[ ! -e "${DSDIR}" ]]; then mkdir "${DSDIR}"; fi
if [[ ! -e "${DSDIR}" ]]; then
	echo "Please create '${DSDIR}' and try again."
	exit 1
fi

# Game Paths

export C3_MAIN="${C3_MAIN:-${DSDIR}/Creatures 3}"
export DS_MAIN="${DS_MAIN:-${DSDIR}/Docking Station}"

export C3_WIN32="${C3_WIN32:-${DSDIR_WIN32}\\Creatures 3}"
export DS_WIN32="${DS_WIN32:-${DSDIR_WIN32}\\Docking Station}"

SDL_MAIN="${DS_MAIN}/SDL"
SDL_WIN32="${DS_WIN32}\\SDL"

# Installation Routines

if [[ "$INSTALL_TYPE" == "c3del" ]]; then
	rm -rf "${C3_MAIN}"
fi
if [[ "$INSTALL_TYPE" == "c3" || "$INSTALL_TYPE" == "c3reg" ]]; then

	# Unpack Data Files From CIE
	
	if [[ ! -e "${C3_MAIN}/build_number" ]]; then
		if [[ ! -e "$INSTALL_SOURCE" ]]; then
			wget_file "${DSDIR}/Downloads" "linuxcie.iso" "https://archive.org/download/linuxcie/linuxcie.iso"
			INSTALL_SOURCE="${DSDIR}/Downloads/linuxcie.iso"
		fi
		7z_e_file "${DSDIR}/Downloads" "${INSTALL_SOURCE}" "creatures3/creatures3.tar.gz"
		tar_x_file "${C3_MAIN}" "${DSDIR}/Downloads/creatures3.tar.gz"
		INSTALL_NEW=1
	fi

	# Unpack Engine From Update 2

	if [[ ! -e "${C3_MAIN}/engine.exe" ]]; then
		wget_file "${DSDIR}/Downloads" "creatures3_update2.exe" "https://nephatrine.net/files/c3ds/windows/creatures3_update2.exe"
		7z_e_file "${C3_MAIN}/Catalogue" "${DSDIR}/Downloads/creatures3_update2.exe" "Catalogue Directory/*"
		7z_e_file "${C3_MAIN}" "${DSDIR}/Downloads/creatures3_update2.exe" "Main Directory/*"
		INSTALL_NEW=1
	fi

	# Fix Conflicts Between CIE & Update 2

	if [[ -e "${C3_MAIN}/Backgrounds/blank.blk" && ! -e "${C3_MAIN}/Backgrounds/GreatOutdoors.blk" ]]; then
		cp "${C3_MAIN}/Backgrounds/blank.blk" "${C3_MAIN}/Backgrounds/GreatOutdoors.blk"
	fi
	if [[ -e "${C3_MAIN}/Catalogue/pray_extensions.catalogue" ]]; then
		mv "${C3_MAIN}/Catalogue/pray_extensions.catalogue" "${C3_MAIN}/Catalogue/pray_extensions.bak"
	fi

	# Fix Permissions

	if [[ "$INSTALL_TYPE" == "c3reg" || $INSTALL_NEW == 1 ]]; then
		echo "Fixing permissions. This may take a bit."
		find "${C3_MAIN}" -type d -exec chmod 755 {} \;
		find "${C3_MAIN}" -type f -exec chmod 644 {} \;
		find "${C3_MAIN}" -type f -name '*.exe' -exec chmod 755 {} \;
	fi

	# Force Fullscreen To Avoid 16-Bit Fuckery
	# Enable virtual desktop in winecfg to keep it "windowed".

	if [[ "${TARGET}" == "Wine" ]]; then
		if [[ -e "${C3_MAIN}/user.cfg" ]]; then
			install_sed
			sed -i 's/FullScreen 0/FullScreen 1/g' "${C3_MAIN}/user.cfg"
		fi
	fi

	# Create Shortcut
		
	if [[ "${TARGET}" == "Wine" ]]; then
		if [[ -z "${XDG_BIN_HOME}" ]]; then
			XDG_BIN_HOME="${HOME}/.local/bin"
		fi
		mkdir -p "${XDG_BIN_HOME}"
	
		if [[ -e "${XDG_BIN_HOME}" ]]; then
			echo '#!/bin/bash' >${XDG_BIN_HOME}/c3wine
			echo "cd \"${C3_MAIN}\"" >>${XDG_BIN_HOME}/c3wine
			echo 'wine explorer /desktop=Creatures3,800x600 ./engine.exe --autokill Creatures 3' >>${XDG_BIN_HOME}/c3wine
			chmod +x ${XDG_BIN_HOME}/c3wine
		fi
	else
		if [[ -e "${DSDIR}" ]]; then
			echo "cd \"${C3_WIN32}\"" >${DSDIR}/c3win32.cmd
			echo 'start engine.exe --autokill Creatures 3' >>${DSDIR}/c3win32.cmd
		fi
	fi

	# Create Registry Entries

	if [[ "$INSTALL_TYPE" == "c3reg" || $INSTALL_NEW == 1 ]]; then
		if [[ "${TARGET}" == "Wine" ]]; then
			install_wine
			wine reg add "HKCU\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers" /v "${C3_WIN32}\\engine.exe" /t REG_SZ /d "~ DWM8And16BitMitigation 16BITCOLOR" /f

			for comp in "Gameware Development" "CyberLife Technology"; do
				wine reg add "HKCU\\Software\\${comp}\\Creatures Engine" /v "Default Background" /t REG_SZ /d "c3_splash" /f
				wine reg add "HKCU\\Software\\${comp}\\Creatures Engine" /v "Language" /t REG_SZ /d "en-GB" /f
			done
		elif [[ "${TARGET}" == "WSL" ]]; then
			echo "REG ADD \"HKCU\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\" /v \"${C3_WIN32}\\engine.exe\" /t REG_SZ /d \"~ DWM8And16BitMitigation 16BITCOLOR\" /f /reg:32" >${DSDIR}/register-c3.cmd

			for comp in "Gameware Development" "CyberLife Technology"; do
				echo "REG ADD \"HKCU\\Software\\${comp}\\Creatures Engine\" /v \"Default Background\" /t REG_SZ /d \"c3_splash\" /f /reg:32" >>${DSDIR}/register-c3.cmd
				echo "REG ADD \"HKCU\\Software\\${comp}\\Creatures Engine\" /v \"Language\" /t REG_SZ /d \"en-GB\" /f /reg:32" >>${DSDIR}/register-c3.cmd
			done
			echo "DEL \"${DSDIR_WIN32}\\register-c3.cmd\"" >>${DSDIR}/register-c3.cmd
			echo "You will need to run '${DSDIR_WIN32}\\register-c3.cmd' from Windows."
		else
			REG ADD "HKCU\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers" -v "${C3_WIN32}\\engine.exe" -t REG_SZ -d "~ DWM8And16BitMitigation 16BITCOLOR" -f -reg:32

			for comp in "Gameware Development" "CyberLife Technology"; do
				REG ADD "HKCU\\Software\\${comp}\\Creatures Engine" -v "Default Background" -t REG_SZ -d "c3_splash" -f -reg:32
				REG ADD "HKCU\\Software\\${comp}\\Creatures Engine" -v "Language" -t REG_SZ -d "en-GB" -f -reg:32
			done
		fi
		
		if [[ "${TARGET}" == "Wine" ]]; then
			install_wine
			for comp in "Gameware Development" "CyberLife Technology" "WOW6432Node\Gameware Development" "WOW6432Node\CyberLife Technology"; do
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Creatures 3" /v "Backgrounds Directory" /t REG_SZ /d "${C3_WIN32}\\Backgrounds\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Creatures 3" /v "Body Data Directory" /t REG_SZ /d "${C3_WIN32}\\Body Data\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Creatures 3" /v "Bootstrap Directory" /t REG_SZ /d "${C3_WIN32}\\Bootstrap\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Creatures 3" /v "Catalogue Directory" /t REG_SZ /d "${C3_WIN32}\\Catalogue\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Creatures 3" /v "Creature Database Directory" /t REG_SZ /d "${C3_WIN32}\\Creature Galleries\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Creatures 3" /v "Display Type" /t REG_DWORD /d "00000002" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Creatures 3" /v "Exported Creatures Directory" /t REG_SZ /d "${C3_WIN32}\\My Creatures\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Creatures 3" /v "Genetics Directory" /t REG_SZ /d "${C3_WIN32}\\Genetics\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Creatures 3" /v "Images Directory" /t REG_SZ /d "${C3_WIN32}\\Images\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Creatures 3" /v "Journal Directory" /t REG_SZ /d "${C3_WIN32}\\Journal\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Creatures 3" /v "Main Directory" /t REG_SZ /d "${C3_WIN32}\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Creatures 3" /v "Overlay Data Directory" /t REG_SZ /d "${C3_WIN32}\\Overlay Data\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Creatures 3" /v "Resource Files Directory" /t REG_SZ /d "${C3_WIN32}\\My Agents\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Creatures 3" /v "Sounds Directory" /t REG_SZ /d "${C3_WIN32}\\Sounds\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Creatures 3" /v "Worlds Directory" /t REG_SZ /d "${C3_WIN32}\\My Worlds\\" /f
			done
		else
			echo "" >${DSDIR}/register-c3-runas.cmd
			for comp in "Gameware Development" "CyberLife Technology"; do
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Creatures 3\" /v \"Backgrounds Directory\" /t REG_SZ /d \"${C3_WIN32}\\Backgrounds\\\\\" /f /reg:32" >>${DSDIR}/register-c3-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Creatures 3\" /v \"Body Data Directory\" /t REG_SZ /d \"${C3_WIN32}\\Body Data\\\\\" /f /reg:32" >>${DSDIR}/register-c3-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Creatures 3\" /v \"Bootstrap Directory\" /t REG_SZ /d \"${C3_WIN32}\\Bootstrap\\\\\" /f /reg:32" >>${DSDIR}/register-c3-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Creatures 3\" /v \"Catalogue Directory\" /t REG_SZ /d \"${C3_WIN32}\\Catalogue\\\\\" /f /reg:32" >>${DSDIR}/register-c3-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Creatures 3\" /v \"Creature Database Directory\" /t REG_SZ /d \"${C3_WIN32}\\Creature Galleries\\\\\" /f /reg:32" >>${DSDIR}/register-c3-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Creatures 3\" /v \"Display Type\" /t REG_DWORD /d \"00000002\" /f /reg:32" >>${DSDIR}/register-c3-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Creatures 3\" /v \"Exported Creatures Directory\" /t REG_SZ /d \"${C3_WIN32}\\My Creatures\\\\\" /f /reg:32" >>${DSDIR}/register-c3-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Creatures 3\" /v \"Genetics Directory\" /t REG_SZ /d \"${C3_WIN32}\\Genetics\\\\\" /f /reg:32" >>${DSDIR}/register-c3-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Creatures 3\" /v \"Images Directory\" /t REG_SZ /d \"${C3_WIN32}\\Images\\\\\" /f /reg:32" >>${DSDIR}/register-c3-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Creatures 3\" /v \"Journal Directory\" /t REG_SZ /d \"${C3_WIN32}\\Journal\\\\\" /f /reg:32" >>${DSDIR}/register-c3-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Creatures 3\" /v \"Main Directory\" /t REG_SZ /d \"${C3_WIN32}\\\\\" /f /reg:32" >>${DSDIR}/register-c3-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Creatures 3\" /v \"Overlay Data Directory\" /t REG_SZ /d \"${C3_WIN32}\\Overlay Data\\\\\" /f /reg:32" >>${DSDIR}/register-c3-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Creatures 3\" /v \"Resource Files Directory\" /t REG_SZ /d \"${C3_WIN32}\\My Agents\\\\\" /f /reg:32" >>${DSDIR}/register-c3-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Creatures 3\" /v \"Sounds Directory\" /t REG_SZ /d \"${C3_WIN32}\\Sounds\\\\\" /f /reg:32" >>${DSDIR}/register-c3-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Creatures 3\" /v \"Worlds Directory\" /t REG_SZ /d \"${C3_WIN32}\\My Worlds\\\\\" /f /reg:32" >>${DSDIR}/register-c3-runas.cmd
			done
			echo "DEL \"${DSDIR_WIN32}\\register-c3-runas.cmd\"" >>${DSDIR}/register-c3-runas.cmd
			echo "You will need to run '${DSDIR_WIN32}\\register-c3-runas.cmd' as Administrator."
		fi
	fi

	# Apply NoCD Patch

	if [[ -e "${C3_MAIN}/engine.exe" && ! -e "${C3_MAIN}/engine-nocd.exe" ]]; then
		wget_file "${DSDIR}/Downloads" "creatures3_nocd.patch" https://nephatrine.net/files/c3ds/windows/creatures3_nocd.patch

		cp "${C3_MAIN}/engine.exe" "${C3_MAIN}/engine-original.exe"
		bspatch_file "${C3_MAIN}/engine-original.exe" "${C3_MAIN}/engine-nocd.exe" "${DSDIR}/Downloads/creatures3_nocd.patch"
	fi
	if [[ -e "${C3_MAIN}/engine-nocd.exe" ]]; then
		cp "${C3_MAIN}/engine-nocd.exe" "${C3_MAIN}/engine.exe"
	fi

	# Run Game

	if [[ -e "${C3_MAIN}/engine.exe" ]]; then
		if [[ "${TARGET}" == "Wine" ]]; then
			cd "${C3_MAIN}" && wine explorer /desktop=Creatures3,800x600 ./engine.exe --autokill Creatures 3
		elif [[ "${TARGET}" == "WSL" ]]; then
			echo "Skipping launch."
		elif [[ "$INSTALL_TYPE" == "c3reg" || $INSTALL_NEW == 1 ]]; then
			echo "Skipping launch."
		else
			cd "${C3_MAIN}" && ./engine.exe --autokill Creatures 3
		fi
	fi
fi

if [[ "$INSTALL_TYPE" == "dsdel" ]]; then
	rm -rf "${DS_MAIN}"
fi
if [[ "$INSTALL_TYPE" == "sdldel" ]]; then
	rm -rf "${SDL_MAIN}"
fi
if [[ -z "$INSTALL_TYPE" || "$INSTALL_TYPE" == "ds" || "$INSTALL_TYPE" == "sdl" || "$INSTALL_TYPE" == "dsreg" ]]; then

	# Unpack Game
	
	if [[ ! -e "${DS_MAIN}/engine.exe" || ! -e "${SDL_MAIN}/engine.exe" ]]; then
		wget_file "${DSDIR}/Downloads" "dockingstation_195.exe" "https://nephatrine.net/files/c3ds/windows/dockingstation_195.exe"
		7z_x_file "${DSDIR}/Downloads" "${DSDIR}/Downloads/dockingstation_195.exe" "dsbuild 195"

		if [[ -e "${DSDIR}/Downloads/dsbuild 195" ]]; then
			install_bunzip2
			find "${DSDIR}/Downloads/dsbuild 195" -type f -name '*.bz2' -exec bunzip2 {} \;
		fi
		if [[ -e "${DSDIR}/Downloads/dsbuild 195/global/machine.cfg" ]]; then
			if [[ ! -e "${DS_MAIN}" ]]; then mkdir -p "${DS_MAIN}"; fi
			cp -r "${DSDIR}/Downloads/dsbuild 195/global/." "${DS_MAIN}/"
			if [[ ! -e "${SDL_MAIN}" ]]; then mkdir -p "${SDL_MAIN}"; fi
			cp "${DSDIR}/Downloads/dsbuild 195"/global/* "${SDL_MAIN}/"
		fi
		if [[ -e "${DSDIR}/Downloads/dsbuild 195/win32_directx/engine.exe" ]]; then
			if [[ ! -e "${DS_MAIN}" ]]; then mkdir -p "${DS_MAIN}"; fi
			cp -r "${DSDIR}/Downloads/dsbuild 195/win32_directx/." "${DS_MAIN}/"
		fi
		if [[ -e "${DSDIR}/Downloads/dsbuild 195/win32_sdl/engine.exe" ]]; then
			if [[ ! -e "${SDL_MAIN}" ]]; then mkdir -p "${SDL_MAIN}"; fi
			cp -r "${DSDIR}/Downloads/dsbuild 195/win32_sdl/." "${SDL_MAIN}/"
		fi
		INSTALL_NEW=1
	fi
	
	# Fix Permissions

	if [[ "$INSTALL_TYPE" == "dsreg" || $INSTALL_NEW == 1 ]]; then
		echo "Fixing permissions. This may take a bit."
		find "${DS_MAIN}" -type d -exec chmod 755 {} \;
		find "${DS_MAIN}" -type f -exec chmod 644 {} \;
		find "${DS_MAIN}" -type f -name '*.exe' -exec chmod 755 {} \;
	fi

	# Force Fullscreen To Avoid 16-Bit Fuckery
	# Enable virtual desktop in winecfg to keep it "windowed".

	if [[ "${TARGET}" == "Wine" ]]; then
		if [[ -e "${DS_MAIN}/user.cfg" ]]; then
			install_sed
			sed -i 's/FullScreen 0/FullScreen 1/g' "${DS_MAIN}/user.cfg"
		fi
	fi

	# Change SDL Game Name

	if [[ -e "${SDL_MAIN}/machine.cfg" ]]; then
		install_sed
		sed -i 's/Docking Station/SDL Station/g' "${SDL_MAIN}/machine.cfg"
		sed -i 's~"Backgrounds Directory" Backgrounds~"Backgrounds Directory" "..\\\\Backgrounds"~g' "${SDL_MAIN}/machine.cfg"
		sed -i 's~"Backgrounds Directory" "Backgrounds"~"Backgrounds Directory" "..\\\\Backgrounds"~g' "${SDL_MAIN}/machine.cfg"
		sed -i 's~"Body Data Directory" "Body Data"~"Body Data Directory" "..\\\\Body Data"~g' "${SDL_MAIN}/machine.cfg"
		sed -i 's~"Bootstrap Directory" Bootstrap~"Bootstrap Directory" "..\\\\Bootstrap"~g' "${SDL_MAIN}/machine.cfg"
		sed -i 's~"Bootstrap Directory" "Bootstrap"~"Bootstrap Directory" "..\\\\Bootstrap"~g' "${SDL_MAIN}/machine.cfg"
		sed -i 's~"Catalogue Directory" Catalogue~"Catalogue Directory" "..\\\\Catalogue"~g' "${SDL_MAIN}/machine.cfg"
		sed -i 's~"Catalogue Directory" "Catalogue"~"Catalogue Directory" "..\\\\Catalogue"~g' "${SDL_MAIN}/machine.cfg"
		sed -i 's~"Genetics Directory" Genetics~"Genetics Directory" "..\\\\Genetics"~g' "${SDL_MAIN}/machine.cfg"
		sed -i 's~"Genetics Directory" "Genetics"~"Genetics Directory" "..\\\\Genetics"~g' "${SDL_MAIN}/machine.cfg"
		sed -i 's~"Exported Creatures Directory" "My Creatures"~"Exported Creatures Directory" "..\\\\My Creatures"~g' "${SDL_MAIN}/machine.cfg"
		sed -i 's~"Images Directory" Images~"Images Directory" "..\\\\Images"~g' "${SDL_MAIN}/machine.cfg"
		sed -i 's~"Images Directory" "Images"~"Images Directory" "..\\\\Images"~g' "${SDL_MAIN}/machine.cfg"
		sed -i 's~"Overlay Data Directory" "Overlay Data"~"Overlay Data Directory" "..\\\\Overlay Data"~g' "${SDL_MAIN}/machine.cfg"
		sed -i 's~"Resource Files Directory" "My Agents"~"Resource Files Directory" "..\\\\My Agents"~g' "${SDL_MAIN}/machine.cfg"
		sed -i 's~"Sounds Directory" Sounds~"Sounds Directory" "..\\\\Sounds"~g' "${SDL_MAIN}/machine.cfg"
		sed -i 's~"Sounds Directory" "Sounds"~"Sounds Directory" "..\\\\Sounds"~g' "${SDL_MAIN}/machine.cfg"
		sed -i 's~"Worlds Directory" "My Worlds"~"Worlds Directory" "..\\\\My Worlds"~g' "${SDL_MAIN}/machine.cfg"
		mkdir -p "${SDL_MAIN}/Creature Galleries" "${SDL_MAIN}/Journal" "${SDL_MAIN}/Users"
	fi

	# Use Fixed SDL DLL

	wget_file "${DSDIR}/Downloads" "SDL-1.2.15-win32.zip" "https://nephatrine.net/files/c3ds/windows/SDL-1.2.15-win32.zip"
	unzip_file "${SDL_MAIN}" "${DSDIR}/Downloads/SDL-1.2.15-win32.zip" "SDL.dll" true

	# Create Shortcut

	if [[ "${TARGET}" == "Wine" ]]; then
		if [[ -z "${XDG_BIN_HOME}" ]]; then
			XDG_BIN_HOME="${HOME}/.local/bin"
		fi
		mkdir -p "${XDG_BIN_HOME}"
	
		if [[ -e "${XDG_BIN_HOME}" ]]; then
			echo '#!/bin/bash' >${XDG_BIN_HOME}/dswine_dx
			echo "cd \"${DS_MAIN}\"" >>${XDG_BIN_HOME}/dswine_dx
			echo 'wine explorer /desktop=DockingStation,800x600 ./engine.exe --autokill "Docking Station"' >>${XDG_BIN_HOME}/dswine_dx
			chmod +x ${XDG_BIN_HOME}/dswine_dx
			echo '#!/bin/bash' >${XDG_BIN_HOME}/dswine_sdl
			echo "cd \"${SDL_MAIN}\"" >>${XDG_BIN_HOME}/dswine_sdl
			echo 'wine ./engine.exe --autokill "SDL Station"' >>${XDG_BIN_HOME}/dswine_sdl
			chmod +x ${XDG_BIN_HOME}/dswine_sdl
		fi
	else
		if [[ -e "${DSDIR}" ]]; then
			echo "cd \"${DS_WIN32}\"" >${DSDIR}/dswin32_dx.cmd
			echo 'start engine.exe --autokill "Docking Station"' >>${DSDIR}/dswin32_dx.cmd
			echo "cd \"${SDL_WIN32}\"" >${DSDIR}/dswin32_sdl.cmd
			echo 'start engine.exe --autokill "SDL Station"' >>${DSDIR}/dswin32_sdl.cmd
		fi
	fi

	# Download Latest Warp Server File

	wget_file "${DS_MAIN}" "server.cfg" "https://nephatrine.net/files/c3ds/server.cfg"
	wget_file "${SDL_MAIN}" "server.cfg" "https://nephatrine.net/files/c3ds/server.cfg"

	# Install Offline Login Option

	if [[ -e "${DS_MAIN}/Bootstrap/010 Docking Station/zzz_gamestart_login.cos" && ! -e "${DS_MAIN}/Bootstrap/010 Docking Station/zzz_gamestart_login.bak" ]]; then
		mv "${DS_MAIN}/Bootstrap/010 Docking Station/zzz_gamestart_login.cos" "${DS_MAIN}/Bootstrap/010 Docking Station/zzz_gamestart_login.bak"
	fi
	wget_file "${DSDIR}/Downloads" "DSOfflineOption.zip" https://nephatrine.net/files/c3ds/mods/DSOfflineOption.zip
	unzip_file "${DS_MAIN}/Images" "${DSDIR}/Downloads/DSOfflineOption.zip" "offlinelogin.c16"
	unzip_file "${DS_MAIN}/Bootstrap/010 Docking Station" "${DSDIR}/Downloads/DSOfflineOption.zip" "zzz_gamestart_login.cos" true
			
	# Create Registry Entries

	if [[ "$INSTALL_TYPE" == "dsreg" || $INSTALL_NEW == 1 ]]; then
		if [[ "${TARGET}" == "Wine" ]]; then
			install_wine
			wine reg add "HKCU\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers" /v "${DS_WIN32}\\engine.exe" /t REG_SZ /d "~ DWM8And16BitMitigation 16BITCOLOR" /f

			for comp in "Gameware Development" "CyberLife Technology"; do
				wine reg add "HKCU\\Software\\${comp}\\Creatures Engine" /v "Default Game" /t REG_SZ /d "Docking Station" /f
				wine reg add "HKCU\\Software\\${comp}\\Docking Station\\InstallBlast" /v "Language" /t REG_DWORD /d "00000064" /f
				wine reg add "HKCU\\Software\\${comp}\\SDL Station\\InstallBlast" /v "Language" /t REG_DWORD /d "00000064" /f
			done
		elif [[ "${TARGET}" == "WSL" ]]; then
			echo "REG ADD \"HKCU\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers\" /v \"${DS_WIN32}\\engine.exe\" /t REG_SZ /d \"~ DWM8And16BitMitigation 16BITCOLOR\" /f /reg:32" >${DSDIR}/register-ds.cmd

			for comp in "Gameware Development" "CyberLife Technology"; do
				echo "REG ADD \"HKCU\\Software\\${comp}\\Creatures Engine\" /v \"Default Game\" /t REG_SZ /d \"Docking Station\" /f /reg:32" >>${DSDIR}/register-ds.cmd
				echo "REG ADD \"HKCU\\Software\\${comp}\\Docking Station\\InstallBlast\" /v \"Language\" /t REG_DWORD /d \"00000064\" /f /reg:32" >>${DSDIR}/register-ds.cmd
				echo "REG ADD \"HKCU\\Software\\${comp}\\SDL Station\\InstallBlast\" /v \"Language\" /t REG_DWORD /d \"00000064\" /f /reg:32" >>${DSDIR}/register-ds.cmd
			done
			echo "DEL \"${DSDIR_WIN32}\\register-ds.cmd\"" >>${DSDIR}/register-ds.cmd
			echo "You will need to run '${DSDIR_WIN32}\\register-ds.cmd' from Windows."
		else
			REG ADD "HKCU\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers" -v "${DS_WIN32}\\engine.exe" -t REG_SZ -d "~ DWM8And16BitMitigation 16BITCOLOR" -f -reg:32

			for comp in "Gameware Development" "CyberLife Technology"; do
				REG ADD "HKCU\\Software\\${comp}\\Creatures Engine" -v "Default Game" -t REG_SZ -d "Docking Station" -f -reg:32
				REG ADD "HKCU\\Software\\${comp}\\Docking Station\\InstallBlast" -v "Language" -t REG_DWORD -d "00000064" -f -reg:32
				REG ADD "HKCU\\Software\\${comp}\\SDL Station\\InstallBlast" -v "Language" -t REG_DWORD -d "00000064" -f -reg:32
			done
		fi
		
		if [[ "${TARGET}" == "Wine" ]]; then
			for comp in "Gameware Development" "CyberLife Technology" "WOW6432Node\Gameware Development" "WOW6432Node\CyberLife Technology"; do
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station" /v "Backgrounds Directory" /t REG_SZ /d "${DS_WIN32}\\Backgrounds\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station" /v "Body Data Directory" /t REG_SZ /d "${DS_WIN32}\\Body Data\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station" /v "Bootstrap Directory" /t REG_SZ /d "${DS_WIN32}\\Bootstrap\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station" /v "Catalogue Directory" /t REG_SZ /d "${DS_WIN32}\\Catalogue\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station" /v "Creature Database Directory" /t REG_SZ /d "${DS_WIN32}\\Creature Galleries\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station" /v "Exported Creatures Directory" /t REG_SZ /d "${DS_WIN32}\\My Creatures\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station" /v "Genetics Directory" /t REG_SZ /d "${DS_WIN32}\\Genetics\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station" /v "Images Directory" /t REG_SZ /d "${DS_WIN32}\\Images\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station" /v "Journal Directory" /t REG_SZ /d "${DS_WIN32}\\Journal\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station" /v "Main Directory" /t REG_SZ /d "${DS_WIN32}\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station" /v "Overlay Data Directory" /t REG_SZ /d "${DS_WIN32}\\Overlay Data\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station" /v "Resource Files Directory" /t REG_SZ /d "${DS_WIN32}\\My Agents\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station" /v "Sounds Directory" /t REG_SZ /d "${DS_WIN32}\\Sounds\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station" /v "Users Directory" /t REG_SZ /d "${DS_WIN32}\\Users\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station" /v "Worlds Directory" /t REG_SZ /d "${DS_WIN32}\\My Worlds\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station\\InstallBlast" /v "CD Path" /t REG_SZ /d "\"\"" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station\\InstallBlast" /v "Creatures3InstalledLastTime" /t REG_DWORD /d "00000001" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station\\InstallBlast" /v "Current Build" /t REG_SZ /d "dsbuild 195" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station\\InstallBlast" /v "Directory" /t REG_SZ /d "${DS_WIN32}\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station\\InstallBlast" /v "Language" /t REG_DWORD /d "00000064" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\Docking Station\\InstallBlast" /v "Warn No Updates If Offline" /t REG_DWORD /d "00000001" /f

				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station" /v "Backgrounds Directory" /t REG_SZ /d "${DS_WIN32}\\Backgrounds\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station" /v "Body Data Directory" /t REG_SZ /d "${DS_WIN32}\\Body Data\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station" /v "Bootstrap Directory" /t REG_SZ /d "${DS_WIN32}\\Bootstrap\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station" /v "Catalogue Directory" /t REG_SZ /d "${DS_WIN32}\\Catalogue\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station" /v "Creature Database Directory" /t REG_SZ /d "${SDL_WIN32}\\Creature Galleries\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station" /v "Exported Creatures Directory" /t REG_SZ /d "${DS_WIN32}\\My Creatures\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station" /v "Genetics Directory" /t REG_SZ /d "${DS_WIN32}\\Genetics\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station" /v "Images Directory" /t REG_SZ /d "${DS_WIN32}\\Images\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station" /v "Journal Directory" /t REG_SZ /d "${SDL_WIN32}\\Journal\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station" /v "Main Directory" /t REG_SZ /d "${SDL_WIN32}\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station" /v "Overlay Data Directory" /t REG_SZ /d "${DS_WIN32}\\Overlay Data\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station" /v "Resource Files Directory" /t REG_SZ /d "${DS_WIN32}\\My Agents\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station" /v "Sounds Directory" /t REG_SZ /d "${DS_WIN32}\\Sounds\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station" /v "Users Directory" /t REG_SZ /d "${SDL_WIN32}\\Users\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station" /v "Worlds Directory" /t REG_SZ /d "${DS_WIN32}\\My Worlds\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station\\InstallBlast" /v "CD Path" /t REG_SZ /d "\"\"" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station\\InstallBlast" /v "Creatures3InstalledLastTime" /t REG_DWORD /d "00000001" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station\\InstallBlast" /v "Current Build" /t REG_SZ /d "dsbuild 195" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station\\InstallBlast" /v "Directory" /t REG_SZ /d "${SDL_WIN32}\\" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station\\InstallBlast" /v "Language" /t REG_DWORD /d "00000064" /f
				wine reg add "HKLM\\SOFTWARE\\${comp}\\SDL Station\\InstallBlast" /v "Warn No Updates If Offline" /t REG_DWORD /d "00000001" /f
			done
		else
			echo "" >${DSDIR}/register-ds-runas.cmd
			for comp in "Gameware Development" "CyberLife Technology"; do
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\" /v \"Backgrounds Directory\" /t REG_SZ /d \"${DS_WIN32}\\Backgrounds\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\" /v \"Body Data Directory\" /t REG_SZ /d \"${DS_WIN32}\\Body Data\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\" /v \"Bootstrap Directory\" /t REG_SZ /d \"${DS_WIN32}\\Bootstrap\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\" /v \"Catalogue Directory\" /t REG_SZ /d \"${DS_WIN32}\\Catalogue\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\" /v \"Creature Database Directory\" /t REG_SZ /d \"${DS_WIN32}\\Creature Galleries\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\" /v \"Exported Creatures Directory\" /t REG_SZ /d \"${DS_WIN32}\\My Creatures\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\" /v \"Genetics Directory\" /t REG_SZ /d \"${DS_WIN32}\\Genetics\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\" /v \"Images Directory\" /t REG_SZ /d \"${DS_WIN32}\\Images\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\" /v \"Journal Directory\" /t REG_SZ /d \"${DS_WIN32}\\Journal\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\" /v \"Main Directory\" /t REG_SZ /d \"${DS_WIN32}\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\" /v \"Overlay Data Directory\" /t REG_SZ /d \"${DS_WIN32}\\Overlay Data\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\" /v \"Resource Files Directory\" /t REG_SZ /d \"${DS_WIN32}\\My Agents\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\" /v \"Sounds Directory\" /t REG_SZ /d \"${DS_WIN32}\\Sounds\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\" /v \"Users Directory\" /t REG_SZ /d \"${DS_WIN32}\\Users\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\" /v \"Worlds Directory\" /t REG_SZ /d \"${DS_WIN32}\\My Worlds\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\\InstallBlast\" /v \"CD Path\" /t REG_SZ /d \"\\\"\\\"\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\\InstallBlast\" /v \"Creatures3InstalledLastTime\" /t REG_DWORD /d \"00000001\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\\InstallBlast\" /v \"Current Build\" /t REG_SZ /d \"dsbuild 195\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\\InstallBlast\" /v \"Directory\" /t REG_SZ /d \"${DS_WIN32}\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\\InstallBlast\" /v \"Language\" /t REG_DWORD /d \"00000064\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\Docking Station\\InstallBlast\" /v \"Warn No Updates If Offline\" /t REG_DWORD /d \"00000001\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd

				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\" /v \"Backgrounds Directory\" /t REG_SZ /d \"${DS_WIN32}\\Backgrounds\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\" /v \"Body Data Directory\" /t REG_SZ /d \"${DS_WIN32}\\Body Data\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\" /v \"Bootstrap Directory\" /t REG_SZ /d \"${DS_WIN32}\\Bootstrap\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\" /v \"Catalogue Directory\" /t REG_SZ /d \"${DS_WIN32}\\Catalogue\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\" /v \"Creature Database Directory\" /t REG_SZ /d \"${SDL_WIN32}\\Creature Galleries\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\" /v \"Exported Creatures Directory\" /t REG_SZ /d \"${DS_WIN32}\\My Creatures\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\" /v \"Genetics Directory\" /t REG_SZ /d \"${DS_WIN32}\\Genetics\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\" /v \"Images Directory\" /t REG_SZ /d \"${DS_WIN32}\\Images\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\" /v \"Journal Directory\" /t REG_SZ /d \"${SDL_WIN32}\\Journal\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\" /v \"Main Directory\" /t REG_SZ /d \"${SDL_WIN32}\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\" /v \"Overlay Data Directory\" /t REG_SZ /d \"${DS_WIN32}\\Overlay Data\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\" /v \"Resource Files Directory\" /t REG_SZ /d \"${DS_WIN32}\\My Agents\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\" /v \"Sounds Directory\" /t REG_SZ /d \"${DS_WIN32}\\Sounds\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\" /v \"Users Directory\" /t REG_SZ /d \"${SDL_WIN32}\\Users\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\" /v \"Worlds Directory\" /t REG_SZ /d \"${DS_WIN32}\\My Worlds\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\\InstallBlast\" /v \"CD Path\" /t REG_SZ /d \"\\\"\\\"\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\\InstallBlast\" /v \"Creatures3InstalledLastTime\" /t REG_DWORD /d \"00000001\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\\InstallBlast\" /v \"Current Build\" /t REG_SZ /d \"dsbuild 195\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\\InstallBlast\" /v \"Directory\" /t REG_SZ /d \"${SDL_WIN32}\\\\\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\\InstallBlast\" /v \"Language\" /t REG_DWORD /d \"00000064\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
				echo "REG ADD \"HKLM\\SOFTWARE\\${comp}\\SDL Station\\InstallBlast\" /v \"Warn No Updates If Offline\" /t REG_DWORD /d \"00000001\" /f /reg:32" >>${DSDIR}/register-ds-runas.cmd
			done
			echo "DEL \"${DSDIR_WIN32}\\register-ds-runas.cmd\"" >>${DSDIR}/register-ds-runas.cmd
			echo "You will need to run '${DSDIR_WIN32}\\register-ds-runas.cmd' as Administrator."
		fi
	fi

	# Apply NoCD Patch

	if [[ -e "${DS_MAIN}/engine.exe" && ! -e "${DS_MAIN}/engine-nocd.exe" ]]; then
		wget_file "${DSDIR}/Downloads" "dockingstation_nocd.patch" https://nephatrine.net/files/c3ds/windows/dockingstation_nocd.patch

		cp "${DS_MAIN}/engine.exe" "${DS_MAIN}/engine-original.exe"
		bspatch_file "${DS_MAIN}/engine-original.exe" "${DS_MAIN}/engine-nocd.exe" "${DSDIR}/Downloads/dockingstation_nocd.patch"
	fi
	if [[ -e "${DS_MAIN}/engine-nocd.exe" ]]; then
		cp "${DS_MAIN}/engine-nocd.exe" "${DS_MAIN}/engine.exe"
	fi

	# Run Game

	if [[ -e "${DS_MAIN}/engine.exe" ]]; then
		if [[ "${TARGET}" == "Wine" ]]; then
			if [[ "$INSTALL_TYPE" == "sdl" ]]; then
				cd "${SDL_MAIN}" && wine ./engine.exe --autokill "SDL Station"
			else
				cd "${DS_MAIN}" && wine explorer /desktop=DockingStation,800x600 ./engine.exe --autokill "Docking Station"
			fi
		elif [[ "${TARGET}" == "WSL" ]]; then
			echo "Skipping launch."
		elif [[ "$INSTALL_TYPE" == "dsreg" || $INSTALL_NEW == 1 ]]; then
			echo "Skipping launch."
		elif [[ "$INSTALL_TYPE" == "sdl" ]]; then
			cd "${SDL_MAIN}" && ./engine.exe --autokill "SDL Station"
		else
			cd "${DS_MAIN}" && ./engine.exe --autokill "Docking Station"
		fi
	fi
fi
