#!/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.
#
# Note that the newest version of packages are installed. 
# It attempts to reuse patches for older versions, but 
# new versions of packages often break cross-compilation by adding TH, 
# etc
#
# Future work: Convert to using the method used here:
# https://github.com/kaoskorobase/ghc-ios-cabal-scripts/

set -e

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

cabalopts="$@"

setupcabal () {
	cabal update

	# Workaround for http://www.reddit.com/r/haskell/comments/26045a/if_youre_finding_cabal_cant_build_your_project/
	# should be able to remove this eventually.
	cabal install transformers-compat -fthree
	cabal install mtl-2.1.3.1

	# Some packages fail to install in a non unicode locale.
	LANG=en_US.UTF-8
	export LANG

	# The android build chroot has recent versions of alex and happy
	# installed here.
	PATH=$HOME/bin:$PATH
	export PATH
}

cabalinstall () {
	echo cabal install "$@" "$cabalopts"
	eval cabal install "$@" "$cabalopts"
}

patched () {
	pkg=$1
	ver=$2
	if [ -z "$ver" ]; then
		cabal unpack $pkg
	else
		cabal unpack $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"
	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
	cabalinstall
	rm -rf $pkg*
	cd ..
}

installgitannexdeps () {
	pushd ../..
	echo cabal install --only-dependencies "$@"
	cabal install --only-dependencies "$@"
	popd
}

install_pkgs () {
	rm -rf tmp
	mkdir tmp
	cd tmp

	patched network
	patched unix-time
	patched lifted-base
	patched zlib
	patched MissingH
	patched distributive
	patched comonad
	patched iproute
	patched primitive
	patched socks
	patched entropy
	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 wai-app-static
	patched shakespeare
	patched shakespeare-css
	patched shakespeare-js
	patched yesod-routes
	patched yesod-core
	patched yesod-persistent
	patched yesod-form
	patched crypto-numbers
	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

	cd ..

	installgitannexdeps -fAndroid -f-Pairing
}

echo
echo
echo native build
echo
setupcabal
installgitannexdeps

echo 
echo
echo cross build
echo
PATH=$HOME/.ghc/$(cat abiversion)/bin:$HOME/.ghc/$(cat abiversion)/arm-linux-androideabi/bin:$PATH
setupcabal
install_pkgs
