#!/bin/bash

if test x"$1" = x"-h" -o x"$1" = x"--help" ; then
cat << EOF
Usage: ./configure [options]

options:
  -h, --help               print this message

  --cc=CC                  use a defined compiler for compilation and linking [gcc]
  --target-os=OS           build programs to run on OS [auto]
  --cross-prefix=PREFIX    use PREFIX for compilation tools [none]
  --sysroot=DIR            specify toolchain's directory [none]
  --enable-debug           compile with debug symbols and never strip

  --extra-cflags=XCFLAGS   add XCFLAGS to CFLAGS
  --extra-ldflags=XLDFLAGS add XLDFLAGS to LDFLAGS

EOF
exit 1
fi

error_exit()
{
    echo error: $1
    exit 1
}

cc_check()
{
    echo 'int main(void){return 0;}' > conftest.c
    $CC conftest.c $1 $2 -o conftest 2> /dev/null
    ret=$?
    rm -f conftest*
    return $ret
}

rm -f config.mak conftest* .depend


echo
echo generating config.mak ...
echo

TARGET_OS=""
CROSS=""
SYSROOT=""
CC="gcc"
LD="gcc"
STRIP="strip"
DEBUG=""
LIBNAME=""
CFLAGS="-Wshadow -Wall -std=gnu99 -I."

for opt; do
    optarg="${opt#*=}"
    case "$opt" in
        --cc=*)
            CC="$optarg"
            LD="$optarg"
            ;;
        --target-os=*)
            TARGET_OS="$optarg"
            ;;
        --cross-prefix=*)
            CROSS="$optarg"
            ;;
        --sysroot=*)
            CFLAGS="$CFLAGS --sysroot=$optarg"
            LDFLAGS="$LDFLAGS --sysroot=$optarg"
            ;;
        --enable-debug)
            DEBUG="enabled"
            ;;
        --extra-cflags=*)
            XCFLAGS="$optarg"
            ;;
        --extra-ldflags=*)
            XLDFLAGS="$optarg"
            ;;
        *)
            error_exit "unknown option $opt"
            ;;
    esac
done

CC="${CROSS}${CC}"
LD="${CROSS}${LD}"
STRIP="${CROSS}${STRIP}"
for f in "$CC" "$LD" "$STRIP"; do
    test -n "$(which $f 2> /dev/null)" || error_exit "$f is not executable"
done

if test -n "$TARGET_OS"; then
    TARGET_OS=$(echo $TARGET_OS | tr '[A-Z]' '[a-z]')
else
    TARGET_OS=$($CC -dumpmachine | tr '[A-Z]' '[a-z]')
fi
case "$TARGET_OS" in
    *mingw* | *cygwin*)
        LIBNAME="vsrawsource.dll"
        LDFLAGS="-shared -Wl,--add-stdcall-alias -L."
        ;;
    *linux*)
        LIBNAME="libvsrawsource.so"
        CFLAGS="$CFLAGS -fPIC"
        LDFLAGS="-shared -fPIC -L."
        ;;
    *)
        error_exit "patches welcome"
        ;;
esac

CFLAGS="$CFLAGS $XCFLAGS"
LDFLAGS="$LDFLAGS $XLDFLAGS"

if test -n "$DEBUG"; then
    CFLAGS="$CFLAGS -g3 -O0"
    STRIP=""
else
    CFLAGS="-Os -g0 -ffast-math -fomit-frame-pointer $CFLAGS"
fi

if ! cc_check "$CFLAGS" "$LDFLAGS"; then
    error_exit "invalid CFLAGS/LDFLAGS"
fi

if cc_check "-march=i686 $CFLAGS" "$LDFLAGS"; then
    CFLAGS="-march=i686 $CFLAGS"
fi

if cc_check "$CFLAGS -fexcess-precision=fast" "$LDFLAGS"; then
    CFLAGS="$CFLAGS -fexcess-precision=fast"
fi

if cc_check "-msse2 -mfpmath=sse $CFLAGS" "$LDFLAGS"; then
    CFLAGS="-msse2 -mfpmath=sse $CFLAGS"
fi

cat >> config.mak << EOF
CC = $CC
LD = $LD
STRIP = $STRIP
LIBNAME = $LIBNAME
CFLAGS = $CFLAGS
LDFLAGS = $LDFLAGS
EOF

echo configure finished
exit 0
