#!/bin/sh

# Copyright 2014-2024 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/>.

# ------------------------------------------------------------------------------
# General stuff

CFLAGS="-O -lc"

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

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

if [ "$OS" = "darwin" ]
then
    JNIFLAGS="-dynamiclib -fPIC"
    SOEXT="dylib"
else
    JNIFLAGS="-shared -fPIC"
    SOEXT="so"
fi

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


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

if [ -z "$JAVA_HOME" ]
then
    if [ -x /usr/libexec/java_home ]
    then
        JAVA_HOME=$(/usr/libexec/java_home)
    elif [ -x "$(command -v javac)" ]
    then
        JAVACCMD=$(command -v javac)
        while [ -h "$JAVACCMD" ]
        do
            JAVACCMD=$(readlink "$JAVACCMD")
        done
        JAVA_HOME=$(echo "$JAVACCMD" | sed 's:/bin/javac::')
    elif [ -x "$JAVA" ]
    then
        JAVA_HOME=$("$JAVA" -XshowSettings:properties -version | 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"

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

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

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

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

LIBKLIPFLAC=
EVENTSIZES=
FLACUNS=
FLACLINK=
LAMEUNS=
LAMELINK=

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

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

if [ -z "$LIBKLIPFLAC" ]
then
    echo "   libklipflac.$SOEXT (needed for MP3 streaming and FLAC support) will not be built"
else
    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"
        FLACLINK="-lFLAC"
    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 "   libklipflac.$SOEXT will be built without FLAC support"
        echo "   For more information, see https://klipspringer.avadeaux.net/guide/libflac/"
        FLACUNS="/unsup"
    fi
    
    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"
        LAMELINK="-lmp3lame"
    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 "   libklipflac.$SOEXT will be built without MP3 streaming support"
        echo "   For more information, see https://klipspringer.avadeaux.net/guide/libmp3lame/"
        LAMEUNS="/unsup"
    fi
    
    if [ -n "$FLACUNS" ] && [ -n "$LAMEUNS" ]
    then
        echo "libklipflac.$SOEXT will be a stub without MP3 streaming or FLAC support"
    fi    
fi

cd ../.. || exit

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


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

{
    echo "LIBKLIPFLAC = $LIBKLIPFLAC"
    echo "EVENTSIZES = $EVENTSIZES"
    echo "FLACUNS = $FLACUNS"
    echo "FLACLINK = $FLACLINK"
    echo "LAMEUNS = $LAMEUNS"
    echo "LAMELINK = $LAMELINK"
    echo "SYSTEMDDIR = $SYSTEMDDIR"
    echo "INSTALLSYSTEMD = $INSTALLSYSTEMD"
    echo "SYSTEMDSERVICE = $SYSTEMDSERVICE"
    echo "MADEICONS = $ICONS"
} >> build/mkdefs

cat build/mkdefs build/Makefile.prep > Makefile
