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

# download and install a2server tools:
# mkatinit, mkvolinfo, afptype, afpsync, aliases, nulib2

a2sScriptURL="https://raw.githubusercontent.com/RasppleII/a2server/master"
a2sBinaryURL="http://blocksfree.com/downloads"

# Find the path of our source directory
top_src="$( dirname "${BASH_SOURCE[0]}" )/.."
pushd $top_src >/dev/null
top_src="$PWD"
popd >/dev/null
if [[ ! -f "$top_src/.a2server_source" ]]; then
	printf "\na2server: cannot find a2server source directory in $top_src.\n\n"
	exit 1
fi

useExternalURL=1
[[ $A2SERVER_NO_EXTERNAL ]] && useExternalURL=

debianVersion="$(lsb_release -rs)"
isRpi=
arch=
if [[ -f /usr/bin/raspi-config ]]; then
	isRpi=1
	arch='rpi'
elif [[ "$(lsb_release -ds)" = Debian* ]]; then
	if [[ "$debianVersion" -ge 7 || $debianVersion == [a-z]* ]]; then
		uname_m="$(uname -m)"
		if [[ $uname_m == "i686" ]]; then
			arch='debian_x86'
		elif [[ $uname_m == "x86_64" ]]; then
			arch='debian_x64'
		fi
	fi
fi

debianName=$(lsb_release -cs)

echo "A2SERVER: Installing A2SERVER tools..."

# delete older nulib2 which doesn't correctly handle zero-length forks in GSHK-created archives
if hash nulib2 &> /dev/null; then
	# FIXME make this sed more robust
	nulib2version=$(nulib2 | sed -n 2p | sed 's|^.*v\([0-9]\)\.\([0-9]\)\.\([0-9]\).*$|\1\2\3|' 2> /dev/null)
	if (( $nulib2version < 310 )); then
		sudo apt-get -y purge nulib2 &> /dev/null
		sudo rm $(command -v nulib2) &> /dev/null
	fi
fi

if ! hash nulib2 &> /dev/null; then

	echo "A2SERVER: Installing nulib2..."

	cd /tmp
	if [[ $arch && ! -f /tmp/a2server-compileAlways ]]; then
		{ wget -qO- "${a2sBinaryURL}/picopkg/nulib2-3.1.0a2-${arch}.tgz" | sudo tar Pzx; } &> /dev/null
	fi

	if ! hash nulib2 &> /dev/null; then

		if [[ ! -f /tmp/a2server-packageReposUpdated ]]; then
			# prepare for installing packages
			sudo apt-get -y update
			touch /tmp/a2server-packageReposUpdated
		fi

		# Dependencies: build-dep for nulib
		sudo apt-get -y install build-essential zlib1g-dev
		sudo apt-get -y clean

		cd /tmp
		rm -rf /tmp/nulib2 &> /dev/null
		mkdir /tmp/nulib2
		cd /tmp/nulib2
		if [[ $useExternalURL ]]; then
			# nulib2 does not have this tagged so we use github's auto-zipball feature
			# note: topdir is nulib2-<full hash>
			# note2: .tar.gz also works
			wget -q -O nulib2-3.1.0a2.zip "https://github.com/fadden/nulib2/archive/20fe7ef.zip"
			unzip nulib2-3.1.0a2.zip 2> /dev/null
			rm nulib2-3.1.0a2.zip &> /dev/null
		fi
		if [ ! -d nulib2* ]; then
			wget -q -O nulib2-3.1.0a2.zip "${a2sBinaryURL}/source/nulib2-3.1.0a2.zip"
			unzip nulib2-3.1.0a2.zip 2> /dev/null
			rm nulib2-3.1.0a2.zip &> /dev/null
		fi
		cd nulib2*
		cd nufxlib
		./configure
		make
		sudo make install
		cd ../nulib2
		./configure
		make
		sudo make install
		cd
		rm -rf /tmp/nulib2
	fi

else
	echo "A2SERVER: Nulib2 has already been installed."
fi

# download and install The Unarchiver, for expanding Apple disk images
# http://wakaba.c3.cx/s/apps/unarchiver.html

if ! hash unar &> /dev/null; then

	echo "A2SERVER: Installing The Unarchiver..."

	if [[ ! -f /tmp/a2server-packageReposUpdated ]]; then
		# prepare for installing packages
		sudo apt-get -y update
		touch /tmp/a2server-packageReposUpdated
	fi

	# jessie and later: Just use the unar package
	if [[ "$debianName" != "wheezy" ]]; then
		sudo apt-get -y install unar
		sudo apt-get clean
	fi

	if ! hash unar &> /dev/null; then
		if [[ $arch && ! -f /tmp/a2server-compileAlways ]]; then
			# Dependencies: for unar
			sudo apt-get -y install libgnustep-base1.22
			sudo apt-get clean
			{ wget -qO- "${a2sBinaryURL}/picopkg/unar-${arch}_${debianName}.tgz" | sudo tar Pzx; } &> /dev/null
		fi

		# If all else fails, compile from source.
		if ! hash unar &> /dev/null; then

			# Dependencies: build-deps for unar
			sudo apt-get -y install build-essential libgnustep-base-dev libz-dev libbz2-dev libssl-dev libicu-dev unzip
			sudo apt-get clean

			rm -rf /tmp/unar &> /dev/null
			mkdir /tmp/unar
			cd /tmp/unar
			if [[ $useExternalURL ]]; then
				wget -O unar-1.8.1.zip "https://github.com/incbee/Unarchiver/archive/unar-1.8.1.zip"
				unzip -o unar-1.8.1.zip &> /dev/null
			fi
			if [ ! -d *Unarchiver*/XADMaster ]; then # need single bracket for glob
				wget -O unar-1.8.1.zip "${a2sBinaryURL}/source/unar-1.8.1.zip"
				unzip -o unar-1.8.1.zip &> /dev/null
			fi
			cd *Unarchiver*/XADMaster
			make -f Makefile.linux
			sudo mv lsar unar /usr/local/bin
			cd ../Extra
			sudo mv lsar.1 unar.1 /usr/local/man/man1
			cd
			rm -rf /tmp/unar
		fi
		sudo mandb &> /dev/null
	fi
else
	echo "A2SERVER: The Unarchiver has already been installed."
fi

if ! hash unzip &> /dev/null; then
	echo "A2SERVER: Installing unzip..."
	if [[ ! -f /tmp/a2server-packageReposUpdated ]]; then
		# prepare for installing packages
		sudo apt-get -y update
		touch /tmp/a2server-packageReposUpdated
	fi

	# Dependencies: unzip
	sudo apt-get -y install unzip
	sudo apt-get clean
else
	echo "A2SERVER: unzip has already been installed."
fi

a2sTools="afpsync afptype mkatinit mkvolinfo cppo debupdate"
a2sHelp="a2server-help.txt"
a2sConfScripts="a2server-aliases a2serverrc"
for _tool in $a2sTools; do
	sudo install -m 755 "$top_src/scripts/tools/$_tool" "/usr/local/bin/$_tool"
done
for _help in $a2sHelp; do
	sudo install -m 644 "$top_src/scripts/tools/$_help" "/usr/local/etc/$_help"
done
for _confscript in $a2sConfScripts; do
	sudo install -m 755 "$top_src/scripts/tools/$_confscript" "/usr/local/etc/$_tool"
done

# 1.3.0: a2serverrc is now called from /etc/bash.bashrc,
#   which in turn calls a2server-aliases
if grep 'a2server-aliases' /etc/bash.bashrc >/dev/null; then
	sudo sed -i 's/a2server-aliases/a2serverrc/' /etc/bash.bashrc
fi
if ! grep 'a2serverrc' /etc/bash.bashrc >/dev/null; then
	echo "source /usr/local/etc/a2serverrc" | sudo tee -a /etc/bash.bashrc >/dev/null
fi

motd="/etc/motd"
if [[ ! $(grep A2SERVER $motd) ]]; then
	sudo tee -a $motd >/dev/null <<EOF

Type 'system-shutdown' to turn off A2SERVER.
Type 'a2server-setup' to configure network boot.
Type 'a2server-help' for a list of other commands.

EOF
fi
