#!/bin/bash

if [ "$1" == "test" ]; then
    if [ "$2" == "-s" ] || [ "$2" == "--run-selenium" ]; then
        if ! type selenium &> /dev/null; then
            echo "Failed to run Selenium. It must not be installed or not accessible on \$PATH!"
            exit 1
        fi

        echo "Running Selenium..."
        selenium 2> /dev/null &
        sleep 3s
    fi

    # Remove empty files under directory _site
    echo "Removing empty files..."
    find _site -type f -empty -exec rm -v {} \;

    # Test site
    cabal test --show-details=always --test-option=--color
else
    exec "./%site" "$@"
fi
