#!/usr/bin/env bash

set -e

[ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en .stack-work.lock "$0" "$@" || :

unset HOST
export DETAILED_LOGGING=${DETAILED_LOGGIN:-true}
export LOG_ALL=${LOG_ALL:-false}
export LOGLEVEL=${LOGLEVEL:-info}
export DUMMY_LOGIN=${DUMMY_LOGIN:-true}
export ALLOW_DEPRECATED=${ALLOW_DEPRECATED:-true}

move-back() {
    mv -v .stack-work .stack-work-run
    [[ -d .stack-work-build ]] && mv -v .stack-work-build .stack-work
}

if [[ -d .stack-work-run ]]; then
    [[ -d .stack-work ]] && mv -v .stack-work .stack-work-build
    mv -v .stack-work-run .stack-work
    trap move-back EXIT
fi

stack exec -- yesod devel $@
