#!/bin/sh
# Bootstraps from an empty cabal to all the necessary haskell packages
# being installed, with the necessary patches to work on Android.
#
# Packages are installed at specific versions we have patches for. Newer
# versions often break cross-compilation by adding TH, etc.
# 
# Needs some extra C libraries to be installed inside the cross-compiler
# lib directory: libgnutls libxml2
#
# When run with "native" as a parameter, the same versions are installed
# in the host system. This is needed in order to use the EvilSplicer to
# expand Template Haskell.

# lib dir
set -e

if [ "$1" ]; then
	mode="$1"
	shift 1
fi
cabalopts="$@"

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

patched () {
	pkg=$1
	version=$2
	if [ "$native" ]; then
		cabalinstall --force-reinstalls $pkg-$version
	else
		shift 2
		cabal unpack $pkg-$version
		cd $pkg-$version
		for patch in ../../haskell-patches/${pkg}_*; do
			echo applying $patch
			patch -p1 < $patch
		done
		cabalinstall "$@"
		cd ..
	fi
}

unpatched () {
	cabalinstall "$@"
}

onlycross () {
	if [ ! "$native" ]; then
		eval "$@"
	fi
}

onlynative () {
	if [ "$native" ]; then
		eval "$@"
	fi
}

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

	onlycross unpatched bytestring-0.10.3.0 text-0.11.3.1 parsec-3.1.3
	patched network 2.4.1.0
	unpatched cereal-0.3.5.2
	patched socks 0.4.2
	unpatched hslogger-1.2.1
	patched MissingH 1.2.0.0
	patched unix-time 0.1.4
	patched async 2.0.1.4
	patched zlib 0.5.4.0
	patched primitive 0.5.0.1
	patched vector 0.10.0.1
	patched distributive 0.3
	unpatched hashable-1.1.2.5
	patched case-insensitive 0.4.0.1
	unpatched nats-0.1 semigroups-0.9 tagged-0.4.4 comonad-3.0.1.1 comonad-transformers-3.0.1
	patched profunctors 3.3
	patched split 0.2.1.2
	unpatched monads-tf-0.1.0.1
	onlycross patched gnutls 0.1.4
	unpatched attoparsec-0.10.4.0 blaze-builder-0.3.1.1 
	patched syb 0.3.7
	patched aeson 0.6.1.0
	patched lifted-base 0.2.0.2
	patched resourcet 0.4.4
	patched monad-control 0.3.1.4
	unpatched conduit-0.5.6
	patched monad-logger 0.2.3.2
	unpatched reflection-1.1.7 bifunctors-3.2 semigroupoids-3.0.2
	unpatched bifunctors-3.2 comonads-fd-3.0.1 groupoids-3.0.1.1
	unpatched profunctor-extras-3.3
	patched lens 3.8.5
	unpatched xml-types-0.3.3
	patched libxml-sax 0.7.3
	patched network-conduit 0.6.2.2
	unpatched asn1-data-0.7.1 asn1-types-0.1.3 attoparsec-conduit-0.5.0.3
	unpatched blaze-builder-conduit-0.5.0.3 blaze-markup-0.5.1.5 blaze-html-0.5.1.3
	patched cipher-aes 0.1.7
	unpatched crypto-api-0.10.2
	unpatched cprng-aes-0.3.4
	unpatched http-types-0.8.0 mime-types-0.1.0.3
	patched certificate 1.3.7
	unpatched system-fileio-0.3.11 tls-1.1.2
	unpatched utf8-string-0.3.7
	unpatched publicsuffixlist-0.1
	unpatched xml-conduit-1.0.3.3
	unpatched zlib-bindings-0.1.1.3 zlib-conduit-0.5.0.3
	patched shakespeare 1.0.3
	patched hamlet 1.1.6.1
	patched xml-hamlet 0.4.0.3
	unpatched certificate-1.3.7
	unpatched dataenc-0.12 hxt-charproperties-9.1.1 \
		hxt-regex-xmlschema-9.1.0 hxt-unicode-9.0.2 hxt-9.3.1.1 
	unpatched -f-templateHaskell QuickCheck-2.5.1.1
	unpatched Crypto-4.2.5.1
	patched HTTP 4000.2.8
	patched hS3 0.5.7
	patched file-embed 0.0.4.7
	patched gsasl 0.3.5 \
		--ghc-options=-I$HOME/.ghc/android-14/arm-linux-androideabi-4.7/arm-linux-androideabi/sysroot/usr/include/ \
		--ld-options="-L$HOME/.ghc/android-14/arm-linux-androideabi-4.7/arm-linux-androideabi/sysroot/usr/lib/"
	onlycross patched network-protocol-xmpp 0.4.4
	onlynative network-protocol-xmpp
	patched shakespeare-css 1.0.2
	patched shakespeare-i18n 1.0.0.2
	patched shakespeare-js 1.1.2
	patched persistent 1.1.5.1
	onlycross unpatched largeword-1.0.4 crypto-api-0.10.2 http-date-0.0.4 \
		cryptohash-0.8.3 vault-0.2.0.4 unix-compat-0.4.1.1 \
		crypto-conduit-0.4.3 wai-1.3.0.3
	patched wai-app-static 1.3.1
	onlycross patched wai-extra 1.3.2.1
	patched yesod-routes 1.1.2
	onlycross unpatched http-conduit-1.8.7.1
	onlycross patched DAV 0.3
	onlynative unpatched DAV
	patched yesod-core 1.1.8
	patched yesod-persistent 1.1.0.1
	patched yesod-form 1.2.1.1
	onlycross unpatched warp-1.3.7.2 yaml-0.8.2
	patched yesod-default 1.1.3.2
	patched yesod 1.1.8
	patched yesod-static 1.1.2
	unpatched ifelse-0.85
	unpatched SafeSemaphore-0.9.0
	if [ ! "$native" ]; then cabal install bloomfilter-1.2.6.10 --constraint 'bytestring >= 0.10.3.0'; fi
	onlynative unpatched bloomfilter-1.2.6.10
	unpatched edit-distance-0.2.1.2
	unpatched uuid-1.2.12
	unpatched json-0.7
	unpatched SHA-1.6.1
	onlycross unpatched data-endian-0.0.1
	unpatched hinotify-0.3.5
	patched iproute 1.2.11
	unpatched dns 0.3.6
	
	cd ..
	rm -rf tmp
}

native_install () {
	echo "Native install"
	native=1
	if [ ! -e $HOME/.cabal/packages/hackage.haskell.org ]; then
		cabal update
	fi
	install_pkgs
}

cross_path () {
	PATH=$HOME/.ghc/android-14/arm-linux-androideabi-4.7/bin:$HOME/.ghc/android-14/arm-linux-androideabi-4.7/arm-linux-androideabi/bin:$PATH
}

cross_install () {
	echo "Cross install"
	native=
	cross_path
	if [ ! -e $HOME/.ghc/android-14/arm-linux-androideabi-4.7/cabal/packages/hackage.haskell.org ]; then
		cabal update
	fi
	install_pkgs
}

case "$mode" in
	native)
		native_install
	;;
	cross)
		cross_install
	;;
	cleancross)
		# cross install, first removing all currently installed
		# packages except those part of ghc
		rm -f $(grep -l $HOME/.ghc/android-14/arm-linux-androideabi-4.7/.cabal/lib/ $HOME/.ghc/android-14/arm-linux-androideabi-4.7/lib/ghc-*/package.conf.d/*.conf)
		cross_path
		ghc-pkg recache
		cross_install
	;;
	"")
		cross_install
		native_install
	;;
esac
