#!/bin/sh

case $(uname -m) in x86_64) ARCH=x64;; i[36]86) ARCH=x86;; arm*) ARCH=arm;; esac
case $(uname) in Darwin) OS=osx;; *) OS=linux;; esac
ARG_LINE="$*"
BRANCH="develop"
BINARY_DIR="core/bin/${OS}_${ARCH}"
BINARY_NAME="luajit"
URL="https://gitlab.com/CapsAdmin/goluwa-binaries/raw/master/core/bin/${OS}_${ARCH}/$BINARY_NAME"
BASE_SCRIPT_URL="https://gitlab.com/CapsAdmin/goluwa/raw/${BRANCH}/"
SCRIPT_DIR="core/lua"
SCRIPT_PATH="$SCRIPT_DIR/boot.lua"

ZERO_ARG="$0"

if [ "$OS" = osx ] && [[ $ZERO_ARG =~ /Users/* ]]; then
	RAN_FROM_FILEBROWSER=1
fi

if [ "$OS" = linux ] && [ ! -t 1 ]; then
	RAN_FROM_FILEBROWSER=0
fi

#NYI
RAN_FROM_FILEBROWSER=0

Main() {
	GetLua "$URL" "$BINARY_DIR" "$BINARY_NAME"
	executable_path="$BINARY_DIR/$BINARY_NAME $SCRIPT_PATH"

	if [ ! -f "$SCRIPT_PATH" ]; then
        mkdir -p "$SCRIPT_DIR"
        DownloadFile "$BASE_SCRIPT_URL$SCRIPT_PATH" "$SCRIPT_PATH"
	fi

	export "GOLUWA_ARG_LINE=$ARG_LINE"
	export "GOLUWA_SCRIPT_PATH=$SCRIPT_PATH"
	export "GOLUWA_RAN_FROM_FILEBROWSER=$RAN_FROM_FILEBROWSER"
	export "GOLUWA_BINARY_DIR=$BINARY_DIR"
	export "GOLUWA_BRANCH=$BRANCH"

	export "LD_LIBRARY_PATH=."

	if [  -n "$TMUX_PANE" ]; then
		clear
	fi

	if [ "$RAN_FROM_FILEBROWSER" = 1 ]; then
		clear
		$executable_path
		exit_code=$?

		if [ $exit_code -ne 0 ]; then
			Alert "$SCRIPT_PATH exited with code $exit_code" "error"
			exit 1
		fi

		if [ "$OS" = osx ]; then
			osascript -e 'tell application "Terminal" to quit' & exit
		fi
	else
		$executable_path
		exit_code=$?
		stty sane
	fi
}

GetLua() {
	url="$1"
	directory="$2"
	filename="$3"

	if [ -f "$directory/lua_downloaded_and_validated" ]; then return; fi

	path="$directory/$filename"
	mkdir -p "$directory"

	DownloadFile "$url" "$path"

	echo "validating $path .. "
	chmod +x "$path"
	$path -e "os.exit(0)"
	status=$?

	if [ $status -eq 0 ]; then
		echo "validation successful"
		touch "$directory/lua_downloaded_and_validated"
	else
		Alert "exit code 'os.exit(0)' does not match $status" "error"
	fi
}

DownloadFile () {
	url="$1"
	output_path="$2"

	echo "downloading $url to $output_path"

	if command -v curl >/dev/null 2>&1; then
		http_code=$(curl -L --url "$url" --output "$output_path" -w "%{http_code}")
		error_code=$?
		if [ "$http_code" != "200" ]; then
			error_code=1
		fi
	elif command -v wget >/dev/null 2>&1; then
		wget --server-response -O "$output_path" "$url"
		error_code=$?
	else
		Alert "unable to find wget or curl" "error"
		exit 1
	fi

	if [ $error_code -ne 0 ]; then
		Alert "wget or curl failed with exit code $error_code" "error"
		rm "$output_path"
		exit 1
	fi
}

Alert() {
	msg=$1
	title=$2

	if [ "$RAN_FROM_FILEBROWSER" = 0 ]; then
		echo "$title: $msg"
	else
		if [ "$OS" = osx ]; then
			osascript -e "tell app \"System Events\" to display dialog \"$msg\" with title \"$title\""
		else
			if [ -x "$(command -v xmessage)" ]; then
				echo "$msg" | xmessage -nearmouse -title "$title" -file -
			else
				echo "$title: $msg"
			fi
		fi
	fi
}

if [ "$1" = "_DL" ]; then
	DownloadFile "$2" "$3"
	exit
fi

Main

#exit the tmux session if we're in a goluwa tmux session
if [  -n "$TMUX_PANE" ]; then
	session_name=$(tmux list-panes -t "$TMUX_PANE" -F '#S' | head -n1)
	if [ `echo $session_name | grep -c "goluwa" ` -gt 0 ]; then
		tmux send-keys -t $session_name "exit " C-m
	fi
fi

exit $exit_code
