#!/bin/bash
# Bootstraps from an empty cabal to all the necessary haskell packages
# being installed, with the necessary patches to work on Android.
#
# You should install ghc-android first.
#
# The cabal.config is used to pin the haskell packages to the last
# versions that have been gotten working. To update, delete the
# cabal.config, run this script with an empty cabal and fix up the broken
# patches, and then use cabal freeze to generate a new cabal.config.
#
# This only installs haskell packages for ghc-android. The host ghc
# also needs to have all the git-annex build deps installed, in similar
# versions. Those are installed from Debian packages using apt.

set -e

if [ ! -d haskell-patches ]; then
	cd standalone/android
fi

setupcabal () {
	# Some packages fail to install in a non unicode locale.
	LANG=en_US.UTF-8
	export LANG
}

patched () {
	pkg=$1
	ver=$2
	if [ -z "$ver" ]; then
		ver="$(grep " $pkg " ../cabal.config | cut -d= -f 3 | sed 's/,$//')"
	fi
	if [ -z "$ver" ]; then
		cabal unpack --pristine $pkg
	else
		cabal unpack --pristine $pkg-$ver
	fi
	cd $pkg*
	git init
	git config user.name dummy
	git config user.email dummy@example.com
	git add .
	git commit -m "pre-patched state of $pkg"
	ln -sf ../../cabal.config
	for patch in ../../haskell-patches/${pkg}_* ../../../no-th/haskell-patches/${pkg}_*; do
		if [ -e "$patch" ]; then
			echo trying $patch
			if ! patch -p1 < $patch; then
				echo "failed to apply $patch"
				echo "please resolve this, replace the patch with a new version, and exit the subshell to continue"
				$SHELL
			fi
		fi
	done
	if [ -e config.sub ]; then
		cp /usr/share/misc/config.sub .
	fi
	if [ -e config.guess ]; then
		cp /usr/share/misc/config.guess .
	fi
	cabal install # --reinstall --force-reinstalls
	rm -f cabal.config

	rm -rf $pkg*
	cd ..
}

installgitannexdeps () {
	pushd ../..
	ln -sf standalone/android/cabal.config
	cabal install --only-dependencies "$@"
	rm -f cabal.config
	popd
}

install_pkgs () {
	rm -rf tmp
	mkdir tmp
	cd tmp
cat <<EOF
EOF
	patched network
	patched unix-time
	patched lifted-base
	patched zlib
	patched MissingH
	patched distributive
	patched comonad
	patched iproute
	patched primitive
	patched socks
	# patched entropy # needed for newer version, not current pinned version
	patched vector
	patched stm-chans
	patched persistent
	patched profunctors
	patched skein
	patched lens
	patched certificate
	patched x509-system
	patched persistent-template
	patched system-filepath
	patched optparse-applicative
	patched wai-app-static
	patched shakespeare
	patched shakespeare-css
	patched shakespeare-js
	patched yesod-routes
	patched hamlet
	patched yesod-core
	patched yesod-persistent
	patched yesod-form
	patched crypto-numbers
	patched clock
	patched yesod-auth
	patched yesod
	patched shakespeare-text
	patched process-conduit
	patched DAV
	patched yesod-static
	patched uuid
	patched dns
	patched gnutls
	patched unbounded-delays
	patched gnuidn
	patched network-protocol-xmpp

	cd ..

	installgitannexdeps -fAndroid -f-Pairing
}

# native cabal needs its own update
cabal update

PATH=$HOME/.ghc/$(cat abiversion)/bin:$HOME/.ghc/$(cat abiversion)/arm-linux-androideabi/bin:$PATH
setupcabal
cabal update

install_pkgs