#!/bin/bash

# Perform a test build on a Haskell package, to be done between
# `darcs dist` and Hackage upload

if [ ! -f "$1" ]
then
   echo "usage: $0 PACKAGE_ARCHIVE"
   echo
   echo "Also, '.' should be the root of your development dir"
   exit 1
fi

set -e

devDir=$(pwd)

# Get absolute path to the passed Haskell package
package=$(readlink --canonicalize-missing $1)

# Unpack it in /tmp
cd /tmp
tar xzf $package

buildDir=$(basename $package .tar.gz)

# Compare the directories
echo
echo "Checking directory contents"
diff --brief --recursive --exclude _darcs --exclude .git --exclude *.swp --exclude .stack-work $devDir "/tmp/$buildDir"
echo "Done checking directory contents"
echo

# Perform the full build, test, the works
cd $buildDir
stack build
stack test

# Clean up that mess
cd ..
rm --recursive --force $buildDir

# If we got here, this was successful
echo "Package $buildDir builds successfully!"
