#!/bin/bash

INSTALL_TYPE=$1
INSTALL_SOURCE=$2

DSDIR="${DSDIR:-/opt/ds}"

function install_7z {
	if ! which 7z >/dev/null 2>&1; then
		if which apt-get >/dev/null 2>&1; then
			sudo apt-get update
			sudo apt-get install p7zip
		fi
	fi
	if ! which 7z >/dev/null 2>&1; then
		echo "Please install '7z' utility in PATH."
		exit 1
	fi
}

function install_rpm2archive {
	if ! which rpm2archive >/dev/null 2>&1; then
		if which apt-get >/dev/null 2>&1; then
			sudo apt-get update
			sudo apt-get install rpm2cpio
		fi
	fi
	if ! which rpm2archive >/dev/null 2>&1; then
		echo "Please install 'rpm2archive' utility in PATH."
		exit 1
	fi
}

function install_sed {
	if ! which sed >/dev/null 2>&1; then
		if which apt-get >/dev/null 2>&1; then
			sudo apt-get update
			sudo apt-get install sed
		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 which apt-get >/dev/null 2>&1; then
			sudo apt-get update
			sudo apt-get install tar
		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 which apt-get >/dev/null 2>&1; then
			sudo apt-get update
			sudo apt-get install unzip
		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 which apt-get >/dev/null 2>&1; then
			sudo apt-get update
			sudo apt-get install wget
		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 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}" && 7z x -y "${FROMARCH}"
		else
			cd "${DESTPATH}" && 7z x -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 rpm2archive_file {
	FROMARCH=$1

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

	install_rpm2archive
	rpm2archive "${FROMARCH}"

	if [[ $? != 0 ]]; then
		echo "Unable to extract archive '${FROMARCH}'."
		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 -O "${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

# Old GLib Version

GLIB_VERSION="1.2.10-58.fc32"
#GLIB_VERSION="1.2.10-55.fc30"

if [[ ! -e "${DSDIR}/usr/lib/libglib-1.2.so.0" ]]; then
	wget_file "${DSDIR}/tmp" "glib-${GLIB_VERSION}.i686.rpm" "https://nephatrine.net/files/c3ds/linux/glib-${GLIB_VERSION}.i686.rpm"
	rpm2archive_file "${DSDIR}/tmp/glib-${GLIB_VERSION}.i686.rpm"
	tar_x_file "${DSDIR}" "${DSDIR}/tmp/glib-${GLIB_VERSION}.i686.rpm.tgz"
fi

# Old GTK+ Version

#GTK_VERSION="1.2.10-93.fc32"
#GTK_VERSION="1.2.10-90.fc30"
GTK_VERSION="1.2.10-70.el6"

if [[ ! -e "${DSDIR}/usr/lib/libgtk-1.2.so.0" ]]; then
	wget_file "${DSDIR}/tmp" "gtk+-${GTK_VERSION}.i686.rpm" "https://nephatrine.net/files/c3ds/linux/gtk+-${GTK_VERSION}.i686.rpm"
	rpm2archive_file "${DSDIR}/tmp/gtk+-${GTK_VERSION}.i686.rpm"
	tar_x_file "${DSDIR}" "${DSDIR}/tmp/gtk+-${GTK_VERSION}.i686.rpm.tgz"
fi

# Old SDL Version

SDL_VERSION="1.2.9-1"

if [[ ! -e "${DSDIR}/usr/lib/libSDL-1.2.so.0" ]]; then
	wget_file "${DSDIR}/tmp" "SDL-${SDL_VERSION}.i386.rpm" "https://nephatrine.net/files/c3ds/linux/SDL-${SDL_VERSION}.i386.rpm"
	rpm2archive_file "${DSDIR}/tmp/SDL-${SDL_VERSION}.i386.rpm"
	tar_x_file "${DSDIR}" "${DSDIR}/tmp/SDL-${SDL_VERSION}.i386.rpm.tgz"
fi

# Game Paths

export C3_MAIN="${C3_MAIN:-${DSDIR}/usr/games/creatures3}"
export DS_MAIN="${DS_MAIN:-${DSDIR}/usr/games/dockingstation}"

if [[ ! -z "${XDG_DATA_HOME}" ]]; then
	export C3_HOME="${XDG_DATA_HOME}/creatures3"
	export DS_HOME="${XDG_DATA_HOME}/dockingstation"
else
	export C3_HOME="${HOME}/.local/share/creatures3"
	export DS_HOME="${HOME}/.local/share/dockingstation"
fi

# Installation Routines

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

	# Unpack Setup

	if [[ ! -e "${DSDIR}/tmp/linuxcie/creatures3/setup.sh" ]]; then
		if [[ ! -e "$INSTALL_SOURCE" ]]; then
			wget_file "${DSDIR}/tmp" "linuxcie.iso" https://archive.org/download/linuxcie/linuxcie.iso
			INSTALL_SOURCE="${DSDIR}/tmp/linuxcie.iso"
		fi
		7z_x_file "${DSDIR}/tmp" "${INSTALL_SOURCE}" "*"
		chmod +x "${DSDIR}/tmp/linuxcie/creatures3/setup.sh"
	fi

	# Run Setup
	# We can probably do this manually, but the installer works...
	
	if [[ ! -e "${C3_MAIN}/creatures3" ]]; then
		if [[ -e "${DSDIR}/tmp/linuxcie/creatures3/setup.sh" ]]; then
			if [[ ! -e "${DSDIR}/tmp/linuxcie/creatures3/setup.bak" ]]; then
				cp "${DSDIR}/tmp/linuxcie/creatures3/setup.sh" "${DSDIR}/tmp/linuxcie/creatures3/setup.bak"
			else
				cp "${DSDIR}/tmp/linuxcie/creatures3/setup.bak" "${DSDIR}/tmp/linuxcie/creatures3/setup.sh"
			fi

			install_sed
			sed -i 's/arch=`DetectARCH`/arch="x86"/g' "${DSDIR}/tmp/linuxcie/creatures3/setup.sh"
			sed -i 's/libc=`DetectLIBC`/libc="glibc-2.1"/g' "${DSDIR}/tmp/linuxcie/creatures3/setup.sh"
		fi

		if [[ ! -e "${C3_MAIN}" ]]; then mkdir -p "${C3_MAIN}"; fi
		if [[ ! -e "${C3_HOME}" ]]; then mkdir -p "${C3_HOME}"; fi
		if [[ ! -e "${DSDIR}/usr/bin" ]]; then mkdir -p "${DSDIR}/usr/bin"; fi
		PATH="${DSDIR}/usr/bin:$PATH" LD_LIBRARY_PATH="${DSDIR}/usr/lib" "${DSDIR}/tmp/linuxcie/creatures3/setup.sh" -b "${DSDIR}/usr/bin" -i "${C3_MAIN}"
	fi

	# Create Shortcuts & Patch Launchers
	
	if [[ -z "${XDG_BIN_HOME}" ]]; then
		XDG_BIN_HOME="${HOME}/.local/bin"
	fi
	mkdir -p "${XDG_BIN_HOME}"

	if [[ -e "${C3_MAIN}/creatures3" ]]; then
		if [[ ! -e "${C3_MAIN}/creatures3.bak" ]]; then
			cp "${C3_MAIN}/creatures3" "${C3_MAIN}/creatures3.bak"
		else
			cp "${C3_MAIN}/creatures3.bak" "${C3_MAIN}/creatures3"
		fi

		install_sed
		sed -i "s~C3_MAIN=[\`]FindPath [\$]0[\`]~C3_MAIN=\"${C3_MAIN}\"~g" "${C3_MAIN}/creatures3"
		sed -i "s~C3_HOME=[\`]echo .[\`]/[\.]creatures3~C3_HOME=\"${C3_HOME}\"~g" "${C3_MAIN}/creatures3"
		sed -i "s~LD_LIBRARY_PATH=[\"][\$]C3_MAIN:[\$]LD_LIBRARY_PATH[\"]~LD_LIBRARY_PATH=\"${DSDIR}/usr/lib:\$C3_MAIN:\$LD_LIBRARY_PATH\"~g" "${C3_MAIN}/creatures3"
		sed -i "s~[\$]C3_MAIN/langpick~LD_LIBRARY_PATH=\"${DSDIR}/usr/lib\" \$C3_MAIN/langpick~g" "${C3_MAIN}/creatures3"

		ln -sf "${C3_MAIN}/creatures3" "${DSDIR}/usr/bin/creatures3"
		ln -sf "${C3_MAIN}/creatures3" "${XDG_BIN_HOME}/c3native"
	fi

	if [[ -e "${C3_MAIN}/creatures3intro" ]]; then
		if [[ ! -e "${C3_MAIN}/creatures3intro.bak" ]]; then
			cp "${C3_MAIN}/creatures3intro" "${C3_MAIN}/creatures3intro.bak"
		else
			cp "${C3_MAIN}/creatures3intro.bak" "${C3_MAIN}/creatures3intro"
		fi

		install_sed
		sed -i "s~cd [\`]FindPath [\$]0[\`]~cd \"${C3_MAIN}\"~g" "${C3_MAIN}/creatures3intro"
		sed -i "s~cd /usr/local/games/creatures3~cd \"${C3_MAIN}\"~g" "${C3_MAIN}/creatures3intro"
		sed -i "s~LD_LIBRARY_PATH=[\"][\`]pwd[\`][\"]~LD_LIBRARY_PATH=\"${DSDIR}/usr/lib:${C3_MAIN}:\$LD_LIBRARY_PATH\"~g" "${C3_MAIN}/creatures3intro"
		sed -i "s~[\.]/langpick~LD_LIBRARY_PATH=\"${DSDIR}/usr/lib\" ./langpick~g" "${C3_MAIN}/creatures3intro"

		ln -sf "${C3_MAIN}/creatures3intro" "${DSDIR}/usr/bin/creatures3intro"
		ln -sf "${C3_MAIN}/creatures3intro" "${XDG_BIN_HOME}/c3intro"
	fi
	
	# Remove Bad SDL Version

	if [[ -e ${C3_MAIN}/libSDL-1.2.so.0 ]]; then
		rm -rf ${C3_MAIN}/libSDL-1.2.so.0
	fi

	# Run Game

	if [[ -e "${C3_MAIN}/creatures3" ]]; then
		cd "${C3_HOME}" && "${C3_MAIN}/creatures3"
	fi
fi

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

	# Unpack Setup

	if [[ ! -e "${DSDIR}/tmp/dockingstation_195_64/dstation-install" ]]; then
		wget_file "${DSDIR}/tmp" "dockingstation_195_64.tar.bz2" https://nephatrine.net/files/c3ds/linux/dockingstation_195_64.tar.bz2
		tar_x_file "${DSDIR}/tmp" "${DSDIR}/tmp/dockingstation_195_64.tar.bz2"
		chmod +x "${DSDIR}/tmp/dockingstation_195_64/dstation-install"
	fi

	# Run Setup
	# We can probably do this manually, but the installer works...
	
	if [[ ! -e "${DS_MAIN}/dstation-install" ]]; then
		if [[ -e "${DSDIR}/tmp/dockingstation_195_64/dstation-install" ]]; then
			if [[ ! -e "${DSDIR}/tmp/dockingstation_195_64/dstation-install.bak" ]]; then
				cp "${DSDIR}/tmp/dockingstation_195_64/dstation-install" "${DSDIR}/tmp/dockingstation_195_64/dstation-install.bak"
			else
				cp "${DSDIR}/tmp/dockingstation_195_64/dstation-install.bak" "${DSDIR}/tmp/dockingstation_195_64/dstation-install"
			fi

			install_sed
			sed -i 's/check_writeable `dirname "$INSTALL_DEST"`//g' "${DSDIR}/tmp/dockingstation_195_64/dstation-install"
			sed -i 's/\[ "$NEW_SUM" != "$OUR_SUM" \]/false/g' "${DSDIR}/tmp/dockingstation_195_64/dstation-install"
			sed -i 's/\[ ! -z "$CD_PATH" -a -z "$RECURSIVE" \]/false/g' "${DSDIR}/tmp/dockingstation_195_64/dstation-install"
			sed -i 's~env RECURSIVE= CD_PATH= sh $BASH_SWITCH dstation-install $SELF_PARAMETERS~echo "BAD"; exit 0~g' "${DSDIR}/tmp/dockingstation_195_64/dstation-install"
			sed -i 's~su --shell=/bin/bash -c~/bin/bash -c~g' "${DSDIR}/tmp/dockingstation_195_64/dstation-install"
			sed -i 's~su -c~/bin/bash -c~g' "${DSDIR}/tmp/dockingstation_195_64/dstation-install"
			sed -i 's~sh $BASH_SWITCH~bash~g' "${DSDIR}/tmp/dockingstation_195_64/dstation-install"
			sed -i "s~DEFAULT_BIN_DEST=/usr/local/bin~DEFAULT_BIN_DEST=\"${DSDIR}/usr/bin\"~g" "${DSDIR}/tmp/dockingstation_195_64/dstation-install"
			sed -i "s~DEFAULT_INSTALL_DEST=/usr/local/games/dockingstation~DEFAULT_INSTALL_DEST=\"${DS_MAIN}\"~g" "${DSDIR}/tmp/dockingstation_195_64/dstation-install"
			sed -i "s~C3_MAIN=/usr/local/games/creatures3~C3_MAIN=\"${C3_MAIN}\"~g" "${DSDIR}/tmp/dockingstation_195_64/dstation-install"
			sed -i "s~DS_HOME=[\$]HOME/[\.]dockingstation~DS_HOME=\"${DS_HOME}\"~g" "${DSDIR}/tmp/dockingstation_195_64/dstation-install"
			sed -i "s~LD_LIBRARY_PATH=[\"][\$]DS_MAIN:[\$]LD_LIBRARY_PATH[\"]~LD_LIBRARY_PATH=${DSDIR}/usr/lib:\$DS_MAIN:\$LD_LIBRARY_PATH~g" "${DSDIR}/tmp/dockingstation_195_64/dstation-install"
			sed -i "s~[\$]DS_MAIN/langpick~LD_LIBRARY_PATH=\"${DSDIR}/usr/lib\" \$DS_MAIN/langpick~g" "${DSDIR}/tmp/dockingstation_195_64/dstation-install"
			sed -i "s~\./imageconvert~LD_LIBRARY_PATH=\"${DSDIR}/usr/lib\" ./imageconvert~g" "${DSDIR}/tmp/dockingstation_195_64/dstation-install"
		fi

		if [[ ! -e "${DS_MAIN}" ]]; then mkdir -p "${DS_MAIN}"; fi
		if [[ ! -e "${DS_HOME}" ]]; then mkdir -p "${DS_HOME}"; fi
		if [[ ! -e "${DSDIR}/usr/bin" ]]; then mkdir -p "${DSDIR}/usr/bin"; fi
		TERM=xterm-256color NO_LAUNCH=true "${DSDIR}/tmp/dockingstation_195_64/dstation-install"

		sed -i 's/\[ -z "$NO_CHECK" \] && update//g' "${DSDIR}/tmp/dockingstation_195_64/dstation-install"
		cp "${DSDIR}/tmp/dockingstation_195_64/dstation-install" "${DS_MAIN}/dstation-install"
		TERM=xterm-256color NO_LAUNCH=true "${DS_MAIN}/dstation-install"
	fi

	# Create Shortcuts
	
	if [[ -z "${XDG_BIN_HOME}" ]]; then
		XDG_BIN_HOME="${HOME}/.local/bin"
	fi
	mkdir -p "${XDG_BIN_HOME}"

	if [[ -e "${DS_MAIN}/dstation-install" ]]; then
		ln -sf "${DS_MAIN}/dstation-install" "${DSDIR}/usr/bin/dockingstation"
		ln -sf "${DS_MAIN}/dstation-install" "${XDG_BIN_HOME}/dsnative"
	fi

	# Download Latest Warp Server File

	wget_file "${DS_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
		cp "${DS_MAIN}/Bootstrap/010 Docking Station/zzz_gamestart_login.cos" "${DS_MAIN}/Bootstrap/010 Docking Station/zzz_gamestart_login.bak"
	fi
	wget_file "${DSDIR}/tmp" "DSOfflineOption.zip" https://nephatrine.net/files/c3ds/mods/DSOfflineOption.zip
	unzip_file "${DS_MAIN}/Images" "${DSDIR}/tmp/DSOfflineOption.zip" "offlinelogin.c16"
	unzip_file "${DS_MAIN}/Bootstrap/010 Docking Station" "${DSDIR}/tmp/DSOfflineOption.zip" "zzz_gamestart_login.cos" true

	# Remove Bad SDL Version

	if [[ -e ${DS_MAIN}/libSDL-1.2.so.0 ]]; then
		rm -rf ${DS_MAIN}/libSDL-1.2.so.0
	fi

	# Run Game

	if [[ -e "${DS_MAIN}/dstation-install" ]]; then
		cd "${DS_HOME}" && "${DS_MAIN}/dstation-install"
	fi
fi
