#!/bin/sh

# Copyright 2014-2025 Jesper Larsson
#
# This file is part of Klipspringer, <https://klipspringer.avadeaux.net/>
#
# Klipspringer is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Klipspringer is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# Klipspringer. If not, see <https:#www.gnu.org/licenses/>.

# ------------------------------------------------------------------------------
# OS specifics

if [ -z "$OS" ]
then
    OS=$(uname -s | tr '[:upper:]' '[:lower:]')
fi
echo "OS set to $OS"

INCLUDELIBS="-I/usr/include"
LINKLIBS=

if [ "$OS" = "darwin" ]
then
    CFLAGS="-O3 -lc -Wall -Wno-shift-op-parentheses -Wno-logical-op-parentheses -Wgnu-pointer-arith"
    JNIFLAGS="-dynamiclib -fPIC"
    SOEXT="dylib"
    if BREWPREFIX=$(brew --prefix 2>/dev/null)
    then
        INCLUDELIBS="-I$BREWPREFIX/include"
        LINKLIBS="-L$BREWPREFIX/lib"
    fi
else
    CFLAGS="-O3 -lc -Wall -Wno-parentheses -Wpointer-arith"
    JNIFLAGS="-shared -fPIC"
    SOEXT="so"
fi

# ------------------------------------------------------------------------------
# Set/verify klipspringer-hub runtime commands.


# ------------------------------------------------------------------------------
# Begin mkdefs for compile tests.

{
    echo "OS = $OS"
    echo "JAVA = $JAVA"
    echo "NODE = $NODE"
    echo "SOEXT = $SOEXT"
    echo "JNIFLAGS = $JNIFLAGS"
    echo "CFLAGS = $CFLAGS"
    echo "INCLUDELIBS = $INCLUDELIBS"
    echo "LINKLIBS = $LINKLIBS"
} > build/mkdefs

# ------------------------------------------------------------------------------
# Test make/compile.

cd build/cf_test || exit
cat ../mkdefs Makefile.prep > Makefile

EVENTSIZES=
LIBKLIP=

FLAC_UNSUPPORTED="-DFLAC_UNSUPPORTED"
FLACLINK=
COMPILEH_FLAC=
COMPILEC_FLAC=

OGGLINK=

VORBIS_UNSUPPORTED="-DLAME_UNSUPPORTED"
VORBISLINK=
COMPILEH_VORBIS=
COMPILEC_VORBIS=

OPUS_UNSUPPORTED="-DLAME_UNSUPPORTED"
OPUSINCLUDE=
OPUSLINK=
COMPILEH_OPUS=
COMPILEC_OPUS=

LAME_UNSUPPORTED="-DLAME_UNSUPPORTED"
LAMELINK=
COMPILEH_LAME=
COMPILEC_LAME=

ALSA_UNSUPPORTED="-DALSA_UNSUPPORTED"
ALSALINK=
COMPILEH_ALSA=
COMPILEC_ALSA=

if { echo "--- Test build from C source --"; make clean && make hello; } > ../configure.log 2>&1
then
    echo "Build from C source ok"

else
    echo "Build from C source failed, see build/configure.log for errors"

fi

# ------------------------------------------------------------------------------
# Build abslink

if { echo "--- Build helper abslink --"; make abslink; } > ../configure.log 2>&1
then
    echo "Build abslink ok"
else
    echo "Build abslink failed, any subsequent use of it will fail"
    ehco "   See build/configure.log for errors"
fi

# ------------------------------------------------------------------------------
# JAVA_HOME

if [ -z "$JAVA_HOME" ]
then
    if [ -x /usr/libexec/java_home ]
    then
        JAVA_HOME=$(/usr/libexec/java_home)
    elif [ -L "$(command -v javac)" ]
    then
        JAVACCMD=$(./abslink "$(command -v javac)")
        JAVA_HOME=$(echo "$JAVACCMD" | sed 's:/bin/javac::')
    elif [ -x "$JAVA" ]
    then
        JAVA_HOME=$("$JAVA" -XshowSettings:properties -version 2>&1 | grep java\.home | sed 's/[[:blank:]]*java.home[[:blank:]]*\=[[:blank:]]*//')
    else
        JAVA_HOME="/usr/lib/jvm/default-java"
    fi
fi
echo "JAVA_HOME set to $JAVA_HOME"

# ------------------------------------------------------------------------------
# Test JNI compile.

echo "JAVA_HOME = $JAVA_HOME" >> ../mkdefs
cat ../mkdefs Makefile.prep > Makefile

if { echo "--- Test compile for JNI ---"; make "libhello.$SOEXT"; } >> ../configure.log 2>&1
then
    echo "Compile for JNI ok"
    LIBKLIP="lib/libklip.$SOEXT"
else
    echo "Compile for JNI failed, see build/configure.log for errors"
fi

# ------------------------------------------------------------------------------
# Test for optional supported libraries

if [ -z "$LIBKLIP" ]
then
    echo "   libklip.$SOEXT cannot not be built"
else
    # ---------------- FLAC

    if [ -z "$NO_FLAC" ] && { echo "--- Test compile and link with libFLAC ---"; make hello_flac; } >> ../configure.log 2>&1
    then
        echo "Compile and link with libFLAC ok"
        FLAC_UNSUPPORTED=
        FLACLINK="-lFLAC"
        COMPILEH_FLAC="\$(JNIH_FLAC)"
        COMPILEC_FLAC="\$(JNIC_FLAC)"
    else
        if [ -z "$NO_FLAC" ]
        then
            echo "Compile or link with libFLAC failed, see build/configure.log for errors"
        else
            echo "NO_FLAC specified"
        fi
        echo "   libklip.$SOEXT will be built without FLAC support"
        echo "   For more information, see https://klipspringer.avadeaux.net/guide/libflac/"
    fi

    # ---------------- LAME

    if [ -z "$NO_LAME" ] && { echo "--- Test compile and link with Lame API ---"; make hello_lame; } >> ../configure.log 2>&1
    then
        echo "Compile and link with Lame API ok"
        LAME_UNSUPPORTED=
        LAMELINK="-lmp3lame"
        COMPILEH_LAME="\$(JNIH_LAME)"
        COMPILEC_LAME="\$(JNIC_LAME)"
    else
        if [ -z "$NO_LAME" ]
        then
            echo "Compile or link with Lame API failed, see build/configure.log for errors"
        else
            echo "NO_LAME specified"
        fi
        echo "   libklip.$SOEXT will be built without MP3 streaming support"
        echo "   For more information, see https://klipspringer.avadeaux.net/guide/libmp3lame/"
    fi

    # ---------------- VORBIS

    if [ -z "$NO_VORBIS" ] && { echo "--- Test compile and link with Vorbis API ---"; make hello_vorbis; } >> ../configure.log 2>&1
    then
        echo "Compile and link with Vorbis API ok"
        OGGLINK="-logg"
        VORBIS_UNSUPPORTED=
        VORBISLINK="-lvorbisfile -lvorbisenc -lvorbis"
        COMPILEH_VORBIS="\$(JNIH_VORBIS)"
        COMPILEC_VORBIS="\$(JNIC_VORBIS)"
    else
        if [ -z "$NO_VORBIS" ]
        then
            echo "Compile or link with Vorbis API failed, see build/configure.log for errors"
        else
            echo "NO_VORBIS specified"
        fi
        echo "   libklip.$SOEXT will be built without Vorbis support"
        echo "   For more information, see https://klipspringer.avadeaux.net/guide/vorbis/"
    fi

    # ---------------- OPUS

    if [ -z "$NO_OPUS" ] && { echo "--- Test compile and link with Opus API ---"; make hello_opus; } >> ../configure.log 2>&1
    then
        echo "Compile and link with Opus API ok"
        OGGLINK="-logg"
        OPUS_UNSUPPORTED=
        OPUSINCLUDE="$INCLUDELIBS/opus"
        OPUSLINK="-lopusfile"
        OGGLINK="-logg"
        COMPILEH_OPUS="\$(JNIH_OPUS)"
        COMPILEC_OPUS="\$(JNIC_OPUS)"
    else
        if [ -z "$NO_OPUS" ]
        then
            echo "Compile or link with Opus API failed, see build/configure.log for errors"
        else
            echo "NO_OPUS specified"
        fi
        echo "   libklip.$SOEXT will be built without Opus support"
        echo "   For more information, see https://klipspringer.avadeaux.net/guide/opus/"
    fi

    # ---------------- ALSA

    if [ -z "$NO_ALSA" ] && { echo "--- Test compile and link with ALSA ---"; make hello_alsa; } >> ../configure.log 2>&1
    then
        echo "Compile and link with ALSA ok"
        ALSA_UNSUPPORTED=
        ALSALINK="-lasound"
        COMPILEH_ALSA="\$(JNIH_ALSA)"
        COMPILEC_ALSA="\$(JNIC_ALSA)"
    else
        if [ -z "$NO_ALSA" ]
        then
            echo "Compile or link with ALSA failed, see build/configure.log for errors"
        else
            echo "NO_ALSA specified"
        fi
        if [ "$OS" = "darwin" ]
        then
            echo "   libklipalsa.$SOEXT will not be built (normal for macOS)"
        else
            echo "   libklipalsa.$SOEXT will not be built"
        fi
        echo "   For more information, see https://klipspringer.avadeaux.net/guide/libalsa/"
    fi

    if [ -n "$FLAC_UNSUPPORTED" ] && [ -n "$LAME_UNSUPPORTED" ] && [ -n "$ALSA_UNSUPPORTED" ] && [ -n "$VORBIS_UNSUPPORTED" ]
    then

        echo "libklip.$SOEXT will not be built due to lack of FLAC, Vorbis, LAME, or ALSA support"
        LIBKLIP=
    fi
fi

cd ../.. || exit

# ------------------------------------------------------------------------------
# systemd


# ------------------------------------------------------------------------------
# macOS app


# ------------------------------------------------------------------------------
# Finalize mkdefs and Makefile

{
    echo "LIBKLIP = $LIBKLIP"
    echo "EVENTSIZES = $EVENTSIZES"
    echo "FLAC_UNSUPPORTED = $FLAC_UNSUPPORTED"
    echo "FLACLINK = $FLACLINK"
    echo "COMPILEH_FLAC = $COMPILEH_FLAC"
    echo "COMPILEC_FLAC = $COMPILEC_FLAC"
    echo "LAME_UNSUPPORTED = $LAME_UNSUPPORTED"
    echo "LAMELINK = $LAMELINK"
    echo "COMPILEH_LAME = $COMPILEH_LAME"
    echo "COMPILEC_LAME = $COMPILEC_LAME"
    echo "OGGLINK = $OGGLINK"
    echo "VORBIS_UNSUPPORTED = $VORBIS_UNSUPPORTED"
    echo "VORBISLINK = $VORBISLINK"
    echo "COMPILEH_VORBIS = $COMPILEH_VORBIS"
    echo "COMPILEC_VORBIS = $COMPILEC_VORBIS"
    echo "OPUSINCLUDE = $OPUSINCLUDE"
    echo "OPUSLINK = $OPUSLINK"
    echo "COMPILEH_OPUS = $COMPILEH_OPUS"
    echo "COMPILEC_OPUS = $COMPILEC_OPUS"
    echo "ALSA_UNSUPPORTED = $ALSA_UNSUPPORTED"
    echo "ALSALINK = $ALSALINK"
    echo "COMPILEH_ALSA = $COMPILEH_ALSA"
    echo "COMPILEC_ALSA = $COMPILEC_ALSA"
    echo "SYSTEMDDIR = $SYSTEMDDIR"
    echo "INSTALLSYSTEMD = $INSTALLSYSTEMD"
    echo "SYSTEMDSERVICE = $SYSTEMDSERVICE"
    echo "INSTALLMACOSAPP = $INSTALLMACOSAPP"
} >> build/mkdefs

cat build/mkdefs build/Makefile.prep > Makefile
