#! /bin/bash
set -eu

APT="sudo apt-get -q -y"
CABAL_INSTALL_DEPS="cabal install --only-dependencies --force-reinstall"

$APT update
$APT install dctrl-tools

# Find potential system packages to satisfy cabal dependencies
deps()
{
	local M='^\([^ ]\+\)-[0-9.]\+ (.*$'
	local G=' -o ( -FPackage -X libghc-\L\1\E-dev )'
	local E="$($CABAL_INSTALL_DEPS "$@" --dry-run -v 2> /dev/null \
		| sed -ne "s/$M/$G/p" | sort -u)"
	grep-aptavail -n -sPackage \( -FNone -X None \) $E | sort -u
}

$APT install $(deps "$@") libghc-quickcheck2-dev # QuickCheck is special
$CABAL_INSTALL_DEPS --constraint='hlint installed' "$@" # Install the rest via Hackage

