#!/usr/bin/env bash

apply_patches()
{
    test -d /tmp/megapatches || return

    for patch in $(ls /tmp/megapatches/* 2> /dev/null); do
        patch -d / -i $patch -p 0 || return
    done
}

build_cmd()
{
    pushd MEGAcmd || return

    local prefix="$PWD/sdk/sdk_build/install"
    local flag_gtest="--with-gtest=$prefix"
    local flag_freeimage="--with-freeimage=$prefix"
    local flag_tests="--enable-tests"
    local result=0

    ./autogen.sh

    result=$?

    if [ $result -ne 0 ]; then
        popd
        return $result
    fi

    use_freeimage || flag_freeimage="--without-freeimage"

    if ! use_gtest; then
        flag_gtest="--without-gtest"
        flag_tests="--disable-tests"
    fi

    env AR=$AR \
        ARCH=$ARCH \
        CC=$CC \
        CFLAGS="$(compiler_flags $CFLAGS)" \
        CPPFLAGS="$(preprocessor_flags $CFLAGS $CPPFLAGS)" \
        CXX=$CXX \
        CXXFLAGS="$(compiler_flags -std=c++14 $CXXFLAGS)" \
        LD=$LD \
        NM=$NM \
        OBJCXX=$CXX \
        OBJDUMP=$OBJDUMP \
        RANLIB=$RANLIB \
        STRIP=$STRIP \
      ./configure $ConfigOpt \
                  --disable-curl-checks \
                  --disable-examples \
                  --disable-shared \
                  --enable-inotify \
                  --enable-static \
                  --enable-sync \
                  --with-cares="$prefix" \
                  --with-cryptopp="$prefix" \
                  --with-curl="$prefix" \
                  --with-libmediainfo="$prefix" \
                  --with-libuv="$prefix" \
                  --with-libzen="$prefix" \
                  --with-openssl="$prefix" \
                  --with-readline="$prefix" \
                  --with-sodium="$prefix" \
                  --with-sqlite="$prefix" \
                  --with-termcap="$prefix" \
                  --with-zlib="$prefix" \
                  --without-ffmpeg \
                  --without-libraw \
                  --without-pcre \
                  --without-pdfium \
                  $flag_gtest \
                  $flag_freeimage \
                  $flag_tests

    result=$?

    if [ $result -ne 0 ]; then
        popd
        return $result
    fi

    make

    result=$?

    popd

    return $result
}

build_sdk()
{
    local flag_configure_only="-c"
    local flag_cross_compiling="-X"
    local flag_disable_examples="-n"
    local flag_disable_freeimage=""
    local flag_enable_cares="-e"
    local flag_enable_cryptopp="-q"
    local flag_enable_curl="-g"
    local flag_enable_megaapi="-a"
    local flag_enable_sodium="-u"
    local flag_enable_tests="-T"
    local flag_enable_uv="-v"
    local flag_openssl_assembly=""
    local flag_openssl_compiler="linux-generic$BUILD_ARCH"
    local result=0

    pushd MEGAcmd/sdk || return

    mkdir -p sdk_build/build || return
    mkdir -p sdk_build/install/lib || return

    use_assembly  || flag_openssl_assembly="no-asm"
    use_gtest     || flag_enable_tests=""
    use_freeimage || flag_disable_freeimage="-f"

    env AR=$AR \
        ARCH=$ARCH \
        CC=$CC \
        CFLAGS="$(compiler_flags $CFLAGS)" \
        CPPFLAGS="$(preprocessor_flags $CFLAGS $CPPFLAGS)" \
        CXX=$CXX \
        CXXFLAGS="$(compiler_flags $CXXFLAGS)" \
        LD=$LD \
        NM=$NM \
        OBJCXX=$CXX \
        OBJDUMP=$OBJDUMP \
        RANLIB=$RANLIB \
        STRIP=$STRIP \
      ./contrib/build_sdk.sh -C "$ConfigOpt" \
                             -O "$flag_openssl_compiler" \
                             -S "$flag_openssl_assembly" \
                             $flag_configure_only \
                             $flag_cross_compiling \
                             $flag_disable_examples \
                             $flag_disable_freeimage \
                             $flag_enable_cares \
                             $flag_enable_cryptopp \
                             $flag_enable_curl \
                             $flag_enable_megaapi \
                             $flag_enable_sodium \
                             $flag_enable_tests \
                             $flag_enable_uv

    local result=$?

    popd

    return $result
}

die()
{
    echo "ERROR: $1"
    exit 1
}

compiler_flags()
{
    local result="$*"

    # Strip preprocessor flags.
    result="$(echo $result | sed -e 's/ *-[DI][^ ]\+//g')"

    # Strip NEON flags (as it breaks freeimage.)
    result="$(echo $result | sed -e 's/-mfpu=neon[^ ]*//g')"

    # GCC's ARM ABI changed between 7.0 and 7.1.
    result="$result -Wno-psabi"

    echo "$result"
}

fix_environment()
{
    find /usr -type f -name '*.la' \
        | xargs sed -i -e 's!\(/[^/]\+\)\+\(/usr.*\)!\2!g'

    find /usr -type f -name '*.la' \
        | xargs sed -i -e 's!/home/cpt/hi3535/x-tools!/usr/local!g'
}

preprocessor_flags()
{
    echo "$*" | grep -o -E -- '-[DI][^ ]+' | xargs
}

use_assembly()
{
    # kvmx64 doesn't support AESNI instructions.
    test "$PLATFORM_ABBR" != "kvmx64" \
         -a "$PLATFORM_ABBR" != "apollolake"
}

use_gtest()
{
    # The comcerto toolchain doesn't support all of C++11.
    test "$PLATFORM_ABBR" != "comcerto2k"
}

use_freeimage()
{
    true
}

printenv

apply_patches || die "Couldn't apply SDK patches"

fix_environment

build_sdk || die "Couldn't build the SDK"
build_cmd || die "Couldn't build MEGAcmd"

