#!/bin/sh
exedir="/usr/pkg/lib/ghc-9.10.1/bin"
exeprog="./hsc2hs-ghc-9.10.1"
executablename="/usr/pkg/lib/ghc-9.10.1/bin/./hsc2hs-ghc-9.10.1"
bindir="/usr/pkg/bin"
libdir="/usr/pkg/lib/ghc-9.10.1/lib"
docdir="/usr/pkg/share/doc/ghc-9.10.1"
includedir="/usr/pkg/include"

HSC2HS_C="-O2 -I/usr/include -I/usr/pkg/include -fPIC"

HSC2HS_L="-Wl,-zrelro -L/usr/lib -Wl,-R/usr/lib -L/usr/pkg/lib -Wl,-R/usr/pkg/lib "

tflag="--template=$libdir/template-hsc.h"
Iflag="-I$includedir/include/"

for f in ${HSC2HS_C}; do
  cflags="${cflags} --cflag=$f"
done

for f in ${HSC2HS_L}; do
  lflags="${lflags} --lflag=$f"
done

HSC2HS_EXTRA="$cflags $lflags"

read_response() {
    response_file=$1
    if [ -f "$response_file" ]; then
        while read -r arg; do
            case "$arg" in
                -t*)          tflag=;;
                --template=*) tflag=;;
                @*)           read_response "${arg#"@"}" ;;
                --)           break;;
            esac
        done < "$response_file"
    fi
}

for arg do
    case "$arg" in
        -t*)          tflag=;;
        --template=*) tflag=;;
        @*)           read_response "${arg#"@"}" ;;
        --)           break;;
    esac
done

exec "$executablename" ${tflag:+"$tflag"} $HSC2HS_EXTRA ${1+"$@"} "$Iflag"
