#!/bin/sh
PWD=`dirname "${0}"`
cd "${PWD}"
#thanks to Iriel for figuring this out
OSREV=`uname -r | cut -d. -f1`
if [ "$OSREV" -ge 11 ] ; then
    export DYLD_LIBRARY_PATH="./hack:./libs:./hack/libs"
    export DYLD_FRAMEWORK_PATH="./hack:./libs:./hack/libs"
else
    export DYLD_FALLBACK_LIBRARY_PATH="./hack:./libs:./hack/libs"
    export DYLD_FALLBACK_FRAMEWORK_PATH="./hack:./libs:./hack/libs"
fi

# attempt to remove quarantine flag: https://github.com/DFHack/dfhack/issues/1465
if ! test -f hack/quarantine-removed; then
    find hack/ libs/ dwarfort.exe \( -name '*.dylib' -or -name '*.framework' -or -name '*.exe' \) -print0 | xargs -0 xattr -d com.apple.quarantine 2>&1 | grep -iv 'no such xattr'
    echo "quarantine flag removed on $(date); remove this file to re-run" > hack/quarantine-removed
fi

if [ ! -t 0 ]; then
    stty() {
        return
    }
fi

old_tty_settings=$(stty -g)
DYLD_INSERT_LIBRARIES=./hack/libdfhack.dylib ./dwarfort.exe "$@"
stty "$old_tty_settings"
tput sgr0
echo ""
