#!/usr/bin/env bash

cd "$(dirname "$0")"

# turn on verbose debugging output for parabuild logs.
exec 4>&1; export BASH_XTRACEFD=4; set -x
# make errors fatal
set -e
# complain about unset env variables
set -u

if [ -z "$AUTOBUILD" ] ; then
    exit 1
fi

if [ "$OSTYPE" = "cygwin" ] ; then
    autobuild="$(cygpath -u $AUTOBUILD)"
else
    autobuild="$AUTOBUILD"
fi

top="$(pwd)"
stage="$(pwd)/stage"

# load autobuild provided shell functions and variables
source_environment_tempfile="$stage/source_environment.sh"
"$autobuild" source_environment > "$source_environment_tempfile"
. "$source_environment_tempfile"

NGHTTP2_VERSION_HEADER_DIR="$top/nghttp2/lib/includes/nghttp2"
version="$(sed -n -E 's/#define NGHTTP2_VERSION "([^"]+)"/\1/p' "${NGHTTP2_VERSION_HEADER_DIR}/nghttp2ver.h" | tr -d '\r' )"
build=${AUTOBUILD_BUILD_ID:=0}
echo "${version}.${build}" > "${stage}/VERSION.txt"

# Restore all .sos
restore_sos ()
{
    for solib in "${stage}"/packages/lib/release/lib*.so*.disable; do
        if [ -f "$solib" ]; then
            mv -f "$solib" "${solib%.disable}"
        fi
    done
}


# Restore all .dylibs
restore_dylibs ()
{
    for dylib in "$stage/packages/lib"/release/*.dylib.disable; do
        if [ -f "$dylib" ]; then
            mv "$dylib" "${dylib%.disable}"
        fi
    done
}

mkdir -p "$stage/include/nghttp2"
mkdir -p "$stage/lib/debug"
mkdir -p "$stage/lib/release"

pushd "$top/nghttp2"
    case "$AUTOBUILD_PLATFORM" in
        windows*)
            load_vsvars

            mkdir -p "build"
            pushd "build"
                cmake .. -G"$AUTOBUILD_WIN_CMAKE_GEN" -T host=x64 -DENABLE_LIB_ONLY=ON \
                    -DCMAKE_SYSTEM_VERSION="10.0.17763.0" -DCMAKE_INSTALL_PREFIX="$(cygpath -m "$stage")"

                cmake --build . --config Debug --clean-first
                cmake --build . --config Release --clean-first
				
                cp -a "lib/Debug/nghttp2.dll" \
                    "$stage/lib/debug/"
                cp -a "lib/Debug/nghttp2.lib" \
                    "$stage/lib/debug/"
                cp -a "lib/Debug/nghttp2.pdb" \
                    "$stage/lib/debug/"
                cp -a "lib/Release/nghttp2.dll" \
                    "$stage/lib/release/"
                cp -a "lib/Release/nghttp2.lib" \
                    "$stage/lib/release/"

                cp -a lib/includes/nghttp2/nghttp2ver.h "$stage/include/nghttp2"
            popd
        ;;

        darwin*)
            opts="${TARGET_OPTS:--arch $AUTOBUILD_CONFIGURE_ARCH -gdwarf-2}"

            mkdir -p "build"
            pushd "build"
                cmake -G "Xcode" .. -DCMAKE_C_FLAGS:STRING="$opts" \
                    -DCMAKE_CXX_FLAGS:STRING="$opts" \
                    -DCMAKE_INSTALL_PREFIX="$stage" \
                    -DCMAKE_OSX_SYSROOT="macosx10.14" \
                    -DCMAKE_OSX_DEPLOYMENT_TARGET="10.13" \
                    -DENABLE_LIB_ONLY=ON 

                cmake --build . --config Debug
                cmake --build . --config Release

                mv lib/Debug/libnghttp2*.dylib "$stage/lib/debug/"
                mv lib/Release/libnghttp2*.dylib "$stage/lib/release/"
                cp -a lib/includes/nghttp2/nghttp2ver.h "$stage/include/nghttp2"
            popd
            rm -rf "build"

            # SL-807: fix_dylib_id doesn't really handle symlinks, even though
            # it's coded to try to do so. Chase the multiple levels of
            # indirection to find the real dylib.
            pushd "$stage/lib/debug"
                dylib="libnghttp2.dylib"
                while [ -L "$dylib" ]
                do dylib="$(readlink "$dylib")"
                done
                fix_dylib_id "$dylib"
            popd
            pushd "$stage/lib/release"
                dylib="libnghttp2.dylib"
                while [ -L "$dylib" ]
                do dylib="$(readlink "$dylib")"
                done
                fix_dylib_id "$dylib"
            popd
        ;;

        linux*)
            # Linux build environment at Linden comes pre-polluted with stuff that can
            # seriously damage 3rd-party builds.  Environmental garbage you can expect
            # includes:
            #
            #    DISTCC_POTENTIAL_HOSTS     arch           root        CXXFLAGS
            #    DISTCC_LOCATION            top            branch      CC
            #    DISTCC_HOSTS               build_name     suffix      CXX
            #    LSDISTCC_ARGS              repo           prefix      CFLAGS
            #    cxx_version                AUTOBUILD      SIGN        CPPFLAGS
            #
            # So, clear out bits that shouldn't affect our configure-directed build
            # but which do nonetheless.
            #
            # unset DISTCC_HOSTS CC CXX CFLAGS CPPFLAGS CXXFLAGS

            # Default target per --address-size
            opts="${TARGET_OPTS:--m$AUTOBUILD_ADDRSIZE $LL_BUILD_RELEASE}"

            # Handle any deliberate platform targeting
            if [ -z "${TARGET_CPPFLAGS:-}" ]; then
                # Remove sysroot contamination from build environment
                unset CPPFLAGS
            else
                # Incorporate special pre-processing flags
                export CPPFLAGS="$TARGET_CPPFLAGS"
            fi

            # Release configure and build
            ./configure --enable-lib-only CFLAGS="$opts" CXXFLAGS="$opts"
            make
            make check

            # ?! Unclear why this build tucks built libraries into a hidden
            # .libs directory.
            mv "$top/nghttp2/lib/.libs/libnghttp2.a" "$stage/lib/release/"
        ;;
    esac
    mkdir -p "$stage/LICENSES"
    cp "$top/nghttp2/COPYING" "$stage/LICENSES/nghttp2.txt"
popd

cp "$NGHTTP2_VERSION_HEADER_DIR"/nghttp2.h "$stage/include/nghttp2/"
