#!/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.


if [ -z "$JAVA" ]
then
    JAVA="$(command -v java)"
    if [ -z "$JAVA" ]
    then
        echo "Error: JAVA not set and no java command found"
        echo "   For more information, see https://klipspringer.avadeaux.net/guide/exec/"
        exit 1
    fi
fi
echo "Java command set to $JAVA"

if [ -z "$NODE" ]
then
    NODE="$(command -v node)"
    if [ -z "$NODE" ]
    then
        echo "Error: NODE not set and no node command found"
        echo "   For more information, see https://klipspringer.avadeaux.net/guide/exec/"
        exit 1
    fi
fi
echo "Node.js command set to $NODE"

# ------------------------------------------------------------------------------
# Test Java and Node.js execution. No effect other than printing diagnostics.

if { echo "--- Test Java execution ---"; $JAVA -cp build/test Hello; } >> ../configure.log 2>&1
then
    echo "Java executiom ok"
else
    echo "Java execution failed, see build/configure.log for errors"
    echo "   Klipspringer can still be built, but the track player cannot be run without Java"
    echo "   For more information, see https://klipspringer.avadeaux.net/guide/exec/"
fi

if { echo "--- Test Node.js execution ---"; $NODE build/test/hello.js; } >> ../configure.log 2>&1
then
    echo "Node.js execution ok"
else
    echo "Node.js execution failed, see build/configure.log for errors."
    echo "   Klipspringer can still be built, but the Klipspringer hub cannot be run without node"
    echo "   For more information, see https://klipspringer.avadeaux.net/guide/exec/"
fi


# ------------------------------------------------------------------------------
# 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 [ -d /dev/input ]
    then
        EVENTSIZES="lib/eventsizes.json"
        echo "$EVENTSIZES (needed for remote control support) will be created"
    fi

    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"
    
    if [ -d /dev/input ]
    then
        echo "   $EVENTSIZES (needed for remote control support) will not be created"
    fi
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


if [ -n "$NO_SYSTEMD" ]
then
    SYSTEMDDIR=
else
    if [ -z "$SYSTEMDDIR" ] && [ -x "$(command -v pkg-config)" ]
    then
        SYSTEMDDIR=$(pkg-config systemd --variable=systemdsystemunitdir 2>/dev/null)
    fi
    if [ -z "$SYSTEMDDIR" ] && [ -d /etc/systemd/system ]
    then
        SYSTEMDDIR=/etc/systemd/system
    fi
fi

if [ -n "$SYSTEMDDIR" ]
then
    SYSTEMDDIR="\$(DESTDIR)$SYSTEMDDIR"
    echo "systemd unit directory set to $SYSTEMDDIR"
    INSTALLSYSTEMD="install_systemd"
    SYSTEMDSERVICE="lib/klipspringer-hub.service"
fi


# ------------------------------------------------------------------------------
# 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
