#!/bin/sh

#--
# Copyright (c) 2022-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#--

scriptdir=$(realpath $(dirname $0))
srcdir="$scriptdir/IntelSEAPI"
blddir="$srcdir/build_ibis/IntelSEAPI"
CMAKE_ARGS="--no-warn-unused-cli \\
    -G \\\"Unix Makefiles\\\" \\
    -DCMAKE_DISABLE_FIND_PACKAGE_Doxygen=TRUE"

if [ "$CMAKE" == "" ]; then
    CMAKE="cmake"
fi

notify() {
    echo "configure: $1"
}

help() {
    cat << EOF
\`configure' configures IntelSEAPI to adapt to many kinds of systems.

Usage: $0 [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
      --cache-file=FILE   cache test results in FILE [disabled]
      --srcdir=DIR        find the sources in DIR [configure dir or \`..']

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]

By default, \`make install' will install all the files in
\`/usr/local/bin', \`/usr/local/lib' etc.  You can specify
an installation prefix other than \`/usr/local' using \`--prefix',
for instance \`--prefix=\$HOME'.

For better control, use the options below.

Fine tuning of the installation directories:
  --bindir=DIR            user executables [EPREFIX/bin]
  --sbindir=DIR           system admin executables [EPREFIX/sbin]
  --libexecdir=DIR        program executables [EPREFIX/libexec]
  --sysconfdir=DIR        read-only single-machine data [PREFIX/etc]
  --sharedstatedir=DIR    modifiable architecture-independent data [PREFIX/com]
  --localstatedir=DIR     modifiable single-machine data [PREFIX/var]
  --libdir=DIR            object code libraries [EPREFIX/lib]
  --includedir=DIR        C header files [PREFIX/include]
  --oldincludedir=DIR     C header files for non-gcc [/usr/include]
  --datarootdir=DIR       read-only arch.-independent data root [PREFIX/share]
  --datadir=DIR           read-only architecture-independent data [DATAROOTDIR]
  --infodir=DIR           info documentation [DATAROOTDIR/info]
  --localedir=DIR         locale-dependent data [DATAROOTDIR/locale]
  --mandir=DIR            man documentation [DATAROOTDIR/man]
  --docdir=DIR            documentation root [DATAROOTDIR/doc/ibis_tools]
  --htmldir=DIR           html documentation [DOCDIR]
  --dvidir=DIR            dvi documentation [DOCDIR]
  --pdfdir=DIR            pdf documentation [DOCDIR]
  --psdir=DIR             ps documentation [DOCDIR]

Optional Features:
  --disable-option-checking  ignore unrecognized --enable/--with options
  --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
  --enable-debug    Turn on debugging

Optional Packages:
  --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
  --without-PACKAGE       do not use PACKAGE (same as --with-PACKAGE=no)

Some influential environment variables:
  CC          C compiler command
  CFLAGS      C compiler flags
  LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a
              nonstandard directory <lib dir>
  CXX         C++ compiler command
  CXXFLAGS    C++ compiler flags

Use these variables to override the choices made by \`configure' or to help
it to find libraries and programs with nonstandard names/locations.

Report bugs to the package provider.
EOF
}

require_arg() {
    if [ -z "$val" ]; then
        val=$1

        if [ -z "$val" ]; then
            notify "error: missing argument to $key"
            exit 1
        fi

        shift_count=2
    fi
}

handle_feature() {
    if [ -z "$val" ]; then
        if [ "x$1" == "xy" ]; then
            val="y"
        else
            val="n"
        fi
    fi

    name=`echo $key | cut -d'-' -f4-`

    case "$name" in
    "option-checking")
        option_checking="$val";;
    "debug")
        # debug="$val"
        ;;
    *)
        if [ "x$option_checking" != "xn" ]; then
            notify "WARNING: unrecognized options: $key"
        fi
        ;;
    esac
}

while [ $# != 0 ]; do
    key=`echo $1 | cut -d'=' -f1`
    val=`echo $1 | cut -s -d'=' -f2-`
    shift_count=1

    case "$key" in
    "-h" | "--help")
        help
        exit 0;;
    "--cache-file")
        ;;
    "--srcdir")
        ;;

    "--prefix")
        require_arg "$2"; prefix="$val";;
    "--exec-prefix")
        require_arg "$2"; exec_prefix="$val";;
    "--bindir")
        require_arg "$2"; bindir="$val";;
    "--sbindir")
        require_arg "$2"; sbindir="$val";;
    "--libexecdir")
        require_arg "$2"; libexecdir="$val";;
    "--sysconfdir")
        require_arg "$2"; sysconfdir="$val";;
    "--sharedstatedir")
        require_arg "$2"; sharedstatedir="$val";;
    "--localstatedir")
        require_arg "$2"; localstatedir="$val";;
    "--libdir")
        require_arg "$2"; libdir="$val";;
    "--includedir")
        require_arg "$2"; includedir="$val";;
    "--oldincludedir")
        require_arg "$2"; oldincludedir="$val";;
    "--datarootdir")
        require_arg "$2"; datarootdir="$val";;
    "--datadir")
        require_arg "$2"; datadir="$val";;
    "--infodir")
        require_arg "$2"; infodir="$val";;
    "--localedir")
        require_arg "$2"; localedir="$val";;
    "--mandir")
        require_arg "$2"; mandir="$val";;
    "--docdir")
        require_arg "$2"; docdir="$val";;

    "--enable-"*)
        handle_feature "y";;
    "--disable-"*)
        handle_feature "n";;
    "--with-"*)
        ;;
    "--without-"*)
        ;;

    "CXX")
        CMAKE_ARGS="$CMAKE_ARGS \\
    -DCMAKE_CXX_COMPILER=\"${1#*=}\"";;
    "CXXFLAGS")
        CMAKE_ARGS="$CMAKE_ARGS \\
    -DCMAKE_CXX_FLAGS=\"${1#*=}\"";;
    "LDFLAGS")
        CMAKE_ARGS="$CMAKE_ARGS \\
    -DCMAKE_EXE_LINKER_FLAGS=\"${1#*=}\" \\
    -DCMAKE_MODULE_LINKER_FLAGS=\"${1#*=}\" \\
    -DCMAKE_SHARED_LINKER_FLAGS=\"${1#*=}\" \\
    -DCMAKE_STATIC_LINKER_FLAGS=\"${1#*=}\"";;
    "CC")
        CMAKE_ARGS="$CMAKE_ARGS \\
    -DCMAKE_C_COMPILER=\"${1#*=}\"";;
    "CFLAGS")
        CMAKE_ARGS="$CMAKE_ARGS \\
    -DCMAKE_C_FLAGS=\"${1#*=}\"";;

    *)
        notify "error: unrecognized option: \`$key'
Try \`$0 --help' for more information"
        exit 1;;
    esac

    shift $shift_count
done

notify "creating ./config.status"

cat > "./config.status" << EOF
#!/bin/sh

#--
# Copyright (c) 2022-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#--

notify() {
    echo "config.status: \$1"
}

srcdir=$srcdir
blddir=$blddir
debug=${debug:="n"}
prefix=${prefix:=/usr/local}
exec_prefix=${exec_prefix:=$prefix}
bindir=${bindir:=$exec_prefix/bin}
sbindir=${sbindir:=$exec_prefix/sbin}
libexecdir=${libexecdir:=$exec_prefix/libexec}
sysconfdir=${sysconfdir:=$prefix/etc}
sharedstatedir=${sharedstatedir:=$prefix/com}
localstatedir=${localstatedir:=$prefix/var}
libdir=${libdir:=$exec_prefix/lib}
includedir=${includedir:=$prefix/include}
oldincludedir=${oldincludedir:=/usr/include}
datarootdir=${datarootdir:=$prefix/share}
datadir=${datadir:=$datarootdir}
infodir=${infodir:=$datarootdir/info}
localedir=${localedir:=$datarootdir/locale}
mandir=${mandir:=$datarootdir/man}
docdir=${docdir:=$datarootdir/doc/misc}
htmldir=${htmldir:=$docdir}
dvidir=${dvidir:=$docdir}
pdfdir=${pdfdir:=$docdir}
psdir=${psdir:=$docdir}

CMAKE_CMD="$CMAKE \\
    -S \$srcdir \\
    -B \$blddir \\
    -DCMAKE_BUILD_TYPE=\`[ \"x\$debug\" == \"xy\" ] && echo Debug || echo Release\` \\
    -DCMAKE_INSTALL_PREFIX=\$prefix \\
    -DCMAKE_INSTALL_BINDIR=\$bindir \\
    -DCMAKE_INSTALL_SBINDIR=\$sbindir \\
    -DCMAKE_INSTALL_LIBEXECDIR=\$libexecdir \\
    -DCMAKE_INSTALL_SYSCONFDIR=\$sysconfdir \\
    -DCMAKE_INSTALL_SHAREDSTATEDIR=\$sharedstatedir \\
    -DCMAKE_INSTALL_LOCALSTATEDIR=\$localstatedir \\
    -DCMAKE_INSTALL_LIBDIR=\$libdir \\
    -DCMAKE_INSTALL_INCLUDEDIR=\$includedir \\
    -DCMAKE_INSTALL_OLDINCLUDEDIR=\$oldincludedir \\
    -DCMAKE_INSTALL_DATAROOTDIR=\$datarootdir \\
    -DCMAKE_INSTALL_DATADIR=\$datadir \\
    -DCMAKE_INSTALL_INFODIR=\$infodir \\
    -DCMAKE_INSTALL_LOCALEDIR=\$localedir \\
    -DCMAKE_INSTALL_MANDIR=\$mandir \\
    -DCMAKE_INSTALL_DOCDIR=\$docdir \\
    $CMAKE_ARGS"

notify "downloading IntelSEAPI repository"
cd $scriptdir && (git submodule update --init IntelSEAPI ; cd -)
notify "running CMake command: \$CMAKE_CMD"
eval \$CMAKE_CMD
cmake_exit_code=\$?
if [ "\$cmake_exit_code" != "0" ]; then
    exit \$cmake_exit_code
fi
EOF

chmod +x "./config.status"
./config.status
exit $?
