#!/bin/bash

#----------------------------------------------------------------------------------------------
#  configure script for AviUtl plugins
#----------------------------------------------------------------------------------------------

# -- help -------------------------------------------------------------------------------------
if test x"$1" = x"-h" -o x"$1" = x"--help" ; then
cat << EOF
Usage: [PKG_CONFIG_PATH=/foo/bar/lib/pkgconfig] ./configure [options]
options:
  -h, --help               print help (this)

  --prefix=PREFIX          set dir for headers and lids [NONE]
  --libdir=DIR             set dir for libs    [NONE]
  --includedir=DIR         set dir for headers [NONE]

  --debug-video            debug video (-DDEBUG_VIDEO)
  --debug-audio            debug audio (-DDEBUG_AUDIO)

  --extra-cflags=XCFLAGS   add XCFLAGS to CFLAGS
  --extra-ldflags=XLDFLAGS add XLDFLAGS to LDFLAGS
  --extra-libs=XLIBS       add XLIBS to LIBS

  --cross-prefix=PREFIX    use PREFIX for compilation tools
  --sysroot=SYSROOT        root of cross-build tree

EOF
exit 1
fi

#-- func --------------------------------------------------------------------------------------
error_exit()
{
    echo error: $1
    exit 1
}

log_echo()
{
    echo $1
    echo >> config.log
    echo --------------------------------- >> config.log
    echo $1 >> config.log
}

cc_check()
{
    rm -f conftest.c
    if [ -n "$3" ]; then
        echo "#include <$3>" >> config.log
        echo "#include <$3>" > conftest.c
    fi
    echo "int main(void){$4 return 0;}" >> config.log
    echo "int main(void){$4 return 0;}" >> conftest.c
    echo $CC conftest.c -o conftest $1 $2 >> config.log
    $CC conftest.c -o conftest $1 $2 2>> config.log
    ret=$?
    echo $ret >> config.log
    rm -f conftest*
    return $ret
}
#----------------------------------------------------------------------------------------------
rm -f config.* .depend

SRCDIR="$(cd $(dirname $0); pwd)"
test "$SRCDIR" = "$(pwd)" && SRCDIR=.
test -n "$(echo $SRCDIR | grep ' ')" && \
    error_exit "out-of-tree builds are impossible with whitespace in source path"

# -- init -------------------------------------------------------------------------------------
CC="gcc"
LD="gcc"
RC="windres"
STRIP="strip"

prefix=""
includedir=""
libdir=""

CFLAGS="-Wall -std=c99 -pedantic -D__USE_MINGW_ANSI_STDIO=1 -I. -I$SRCDIR"
AU_LDFLAGS="-shared -Wl,--dll,--add-stdcall-alias"
LDFLAGS="-L."
DEPLIBS="liblsmash libavformat libavcodec libswscale libavresample libavutil"

SRC_INPUT="lwinput.c libavsmash_input.c lwlibav_input.c avs_input.c dummy_input.c            \
           vpy_input.c colorspace.c colorspace_simd.c                                        \
           video_output.c audio_output.c progress_dlg.c                                      \
           ../common/libavsmash.c ../common/libavsmash_video.c ../common/libavsmash_audio.c  \
           ../common/lwlibav_dec.c ../common/lwlibav_video.c ../common/lwlibav_audio.c       \
           ../common/lwindex.c ../common/resample.c ../common/audio_output.c                 \
           ../common/video_output.c ../common/lwsimd.c ../common/utils.c ../common/qsv.c     \
           ../common/decode.c ../common/osdep.c"
SRC_MUXER="lwmuxer.c progress_dlg.c ../common/utils.c"
SRC_DUMPER="lwdumper.c"
SRC_COLOR="lwcolor.c lwcolor_simd.c ../common/lwsimd.c"

# -- options ----------------------------------------------------------------------------------
echo all command lines: > config.log
echo "$*" >> config.log

for opt; do
    optarg="${opt#*=}"
    case "$opt" in
        --prefix=*)
            prefix="$optarg"
            ;;
        --libdir=*)
            libdir="$optarg"
            ;;
        --includedir=*)
            includedir="$optarg"
            ;;
        --debug-video)
            CFLAGS="$CFLAGS -DDEBUG_VIDEO"
            ;;
        --debug-audio)
            CFLAGS="$CFLAGS -DDEBUG_AUDIO"
            ;;
        --extra-cflags=*)
            XCFLAGS="$optarg"
            ;;
        --extra-ldflags=*)
            XLDFLAGS="$optarg"
            ;;
        --extra-libs=*)
            XLIBS="$optarg"
            ;;
        --cross-prefix=*)
            CROSS="$optarg"
            ;;
        --sysroot=*)
            CFLAGS="$CFLAGS --sysroot=$optarg"
            LDFLAGS="$LDFLAGS --sysroot=$optarg"
            ;;
        *)
            error_exit "unknown option $opt"
            ;;
    esac
done

# -- add extra --------------------------------------------------------------------------------
if test -n "$prefix"; then
    CFLAGS="$CFLAGS -I$prefix/include"
    LDFLAGS="$LDFLAGS -L$prefix/lib"
fi
test -n "$includedir" && CFLAGS="$CFLAGS -I$includedir"
test -n "$libdir" && LDFLAGS="$LDFLAGS -L$libdir"

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

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

# -- check & set cflags and ldflags  ----------------------------------------------------------
log_echo "CFLAGS/LDFLAGS checking..."
if ! cc_check "$CFLAGS" "$LDFLAGS"; then
    error_exit "invalid CFLAGS/LDFLAGS"
fi
if cc_check "-Os -ffast-math $CFLAGS" "$LDFLAGS"; then
    CFLAGS="-Os -ffast-math $CFLAGS"
fi
if cc_check "$CFLAGS -fexcess-precision=fast" "$LDFLAGS"; then
    CFLAGS="$CFLAGS -fexcess-precision=fast"
fi

# -- check pkg-config ----------------------------------------------------------------
PKGCONFIGEXE="pkg-config"
test -n "$(which ${CROSS}${PKGCONFIGEXE} 2> /dev/null)" && \
    PKGCONFIGEXE=${CROSS}${PKGCONFIGEXE}

if $PKGCONFIGEXE --exists $DEPLIBS 2> /dev/null; then
    LIBS="$($PKGCONFIGEXE --libs $DEPLIBS)"
    CFLAGS="$CFLAGS $($PKGCONFIGEXE --cflags $DEPLIBS)"
else
    for lib in $DEPLIBS; do
        LIBS="$LIBS -l${lib#lib}"
    done
    log_echo "warning: pkg-config or pc files not found, lib detection may be inaccurate."
fi

# -- check lsmash -----------------------------------------------------------------------------
log_echo "checking for liblsmash..."
if ! cc_check "$CFLAGS" "$LDFLAGS $LIBS $XLIBS" "lsmash.h" "lsmash_create_root();" ; then
    log_echo "error: liblsmash checking failed"
    error_exit "lsmash.h might not be installed or some libs missing."
fi

# -- check libav ------------------------------------------------------------------------------
log_echo "checking for libavformat..."
if ! cc_check "$CFLAGS" "$LDFLAGS $LIBS $XLIBS" "libavformat/avformat.h" "avformat_find_stream_info(0,0);" ; then
    log_echo "error: libavformat checking failed."
    error_exit "libavformat/avformat.h might not be installed or some libs missing."
fi

log_echo "checking for libavcodec..."
if ! cc_check "$CFLAGS" "$LDFLAGS $LIBS $XLIBS" "libavcodec/avcodec.h" "avcodec_find_decoder(0);" ; then
    log_echo "error: libavcodec checking failed."
    error_exit "libavcodec/avcodec.h might not be installed or some libs missing."
fi

log_echo "checking for libswscale..."
if ! cc_check "$CFLAGS" "$LDFLAGS $LIBS $XLIBS" "libswscale/swscale.h" "sws_getCachedContext(0,0,0,0,0,0,0,0,0,0,0);" ; then
    log_echo "error: libswscale checking failed."
    error_exit "libswscale/swscale.h might not be installed or some libs missing."
fi

log_echo "checking for libavresample..."
if ! cc_check "$CFLAGS" "$LDFLAGS $LIBS $XLIBS" "libavresample/avresample.h" "avresample_open(0);" ; then
    log_echo "error: libavresample checking failed."
    error_exit "libavresample/avresample.h might not be installed or some libs missing."
fi

LDFLAGS="$AU_LDFLAGS $LDFLAGS"
LIBS="-lcomctl32 -lwinmm -lgdi32 $LIBS $XLIBS"

# -- output config.h --------------------------------------------------------------------------
pushd $SRCDIR
REV="$(git rev-list --count HEAD 2> /dev/null)"
HASH="$(git rev-parse --short HEAD 2> /dev/null)"
popd
cat >> config.h << EOF
#define LSMASHWORKS_REV "$REV"
#define LSMASHWORKS_GIT_HASH "$HASH"
EOF

# -- output config.mak ------------------------------------------------------------------------
rm -f config.mak
cat >> config.mak << EOF
CC = $CC
LD = $LD
RC = $RC
STRIP = $STRIP
CFLAGS = $CFLAGS
LDFLAGS = $LDFLAGS
LIBS = $LIBS
SRCDIR = $SRCDIR
SRC_INPUT = $SRC_INPUT
SRC_MUXER = $SRC_MUXER
SRC_DUMPER = $SRC_DUMPER
SRC_COLOR = $SRC_COLOR
EOF

cat >> config.log << EOF
---------------------------------
    setting
---------------------------------
EOF
cat config.mak >> config.log

cat << EOF

settings...
CC          = $CC
LD          = $LD
RC          = $RC
STRIP       = $STRIP
CFLAGS      = $CFLAGS
LDFLAGS     = $LDFLAGS
LIBS        = $LIBS
EOF

test "$SRCDIR" = "." || cp -f $SRCDIR/GNUmakefile .

# ---------------------------------------------------------------------------------------------

cat << EOF

configure finished.
type 'make'        : compile all plugins
type 'make input'  : compile lwinput.aui
type 'make muxer'  : compile lwmuxer.auf
type 'make dumper' : compile lwdumper.auf
type 'make color'  : compile lwcolor.auc
EOF