#! /bin/bash
# vim: set tabstop=4 shiftwidth=4 noexpandtab filetype=sh:

# update/index.txt - upgrade path from Ivan's a2server tree
#
# Ivan's a2server installed with a single copy-paste of a command line that
# downloaded a script and ran it sight-unseen.  It also installed an alias
# command that would perform a similar one-liner to upgrade a2server to the
# latest version.  This script provides that interface for upgrades.

a2sBranch="master"
a2sScriptURL="https://raw.githubusercontent.com/RasppleII/a2server/${a2sBranch}"
a2sTarball="https://github.com/RasppleII/a2server/archive/${a2sBranch}.tar.gz"

# Set top_src to the location of the source tree if we're running in one
top_src="$( dirname "${BASH_SOURCE[0]}" )/.."
pushd $top_src >/dev/null
top_src="$PWD"
popd >/dev/null
if [[ ! -f "$top_src/.a2server_source" ]]; then
	a2sWebUpdate=1
	top_src=$(mktemp -d /tmp/a2server.XXXXXXXXXXXX)
fi

installedVersion=
if [[ -f /usr/local/etc/A2SERVER-version ]]; then
	read installedVersion < /usr/local/etc/A2SERVER-version
	# Convert old three-digit version if needed
    if [[ $installedVersion != *.*.* ]]; then
        installedVersion="${installedVersion:0:1}.${installedVersion:1:1}.${installedVersion:2}"
    fi
fi

autoAnswerYes=
for arg in $@; do
	if [[ $arg == "-y" ]]; then
		autoAnswerYes=1
		break
	fi
done

printf "\na2server web update\n\n"

if (( $a2sWebUpdate )); then
	newVersion=$(wget -qO- "${a2sScriptURL}/install.sh" | grep '^a2serverVersion' | cut -d '"' -f 2)
	cat <<-EOT
	You've started the single command a2server update script.  This script will
	download things behind the scenes, run them without much verification, and
	then it will delete what it ran whether the upgrade finished successfully or
	not.  For this reason, we are going to be phasing this script out in the
	future.
	EOT
else
	newVersion=$(grep '^a2serverVersion' "$top_src/install.sh" | cut -d '"' -f 2)
	cat <<-EOT
	You've started the single command a2server update script from inside the
	a2server source directory for version $newVersion.  This script will only
	upgrade to or reinstall that version.
	EOT
fi

if [[ $installedVersion ]]; then
	printf "\nRight now you have a2server %s installed\n" "$installedVersion"
else
	printf "\nRight now you don't appear to have a2server installed\n"
fi

if [[ ! $autoAnswerYes ]]; then
	printf "\nPress Enter to continue "
	read
fi

# If running from local source tree, that's what you'll be upgrading to
printf "\nProject history:\n"
if (( $a2sWebUpdate )); then
	wget -qO- "${a2sScriptURL}/update/versionhistory.txt"
else
	cat "$top_src/update/versionhistory.txt"
fi

cat <<EOT

installed version: ${installedVersion}
available version: ${newVersion}

EOT

if [[ ! $autoAnswerYes ]]; then
	printf "Do you want to update (or reinstall) a2server? "
	read
	if [[ ${REPLY:0:1} != "y" && ${REPLY:0:1} != "Y" ]]; then
		printf "\nOkay, not updating anything now!\n\n"
		exit 1
	fi
fi

if (( $a2sWebUpdate )); then
	top_src=$(mktemp -d /tmp/a2server.XXXXXXXXXXXX)

	# Normally tarballs unpack in a subdirectory, but we want it to unpack into
	# $top_src directly (even though the directory has the tarball in it.)
	printf "\na2server: Downloading installer to $top_src.\n"
	wget -O "$top_src/a2server-$a2sBranch.tar.gz" "$a2sTarball"
	tar -C $top_src --strip-components=1 -zxf "$top_src/a2server-${a2sBranch}.tar.gz"
fi

# Now we actually run the installer as if we were installing it by hand
printf "\na2server: Running the update installer now.\n"
"${top_src}/install.sh" -i "$@"

if (( $a2sWebUpdate )); then
	printf "\na2server: Finished with installer, now deleting ${top_src}.\n"
	rm -rf "$top_src"
fi
