#!/bin/sh
CANONPATH=$(readlink -f "$0")
DIRPATH="$(dirname "$CANONPATH")"
cd "$DIRPATH" || exit
cd ../../../ || exit
VERSION=

while [ $# -gt 0 ]; do
    case "$1" in
        --version)
            if [ -n "$2" ]; then
                VERSION="$2"
                shift
            else
                echo "Error: --version requires an argument." >&2
                exit 1
            fi
            ;;
        --)
            shift
            break
            ;;
        -*)
            echo "Unknown option: $1" >&2
            exit 1
            ;;
        *)
            break
            ;;
    esac
    shift
done

CONFIG_NAME=
SO_LIB_PATH=
if command -v premake5 &> /dev/null
then
    premake5 gmake || exit
    CONFIG_NAME=release_x86_64
    SO_LIB_PATH=../../../libs/bsd/x86_64/libeepp.so
elif command -v premake4 &> /dev/null
then
    premake4 gmake || exit
    CONFIG_NAME=release
    SO_LIB_PATH=../../../libs/bsd/libeepp.so
else
    echo "Neither premake5 nor premake4 is available. Please install one."
    exit 1
fi

cd make/bsd || exit

if command -v nproc &> /dev/null # nproc is only available on FreeBSD 13.2 and later
then
  parallel_tasks=$(nproc)
else
  parallel_tasks=$(getconf NPROCESSORS_ONLN 2>/dev/null || sysctl -n hw.ncpu)
  # the former is the number of online cpus, the later is the total number of cpus
fi
gmake -j"${parallel_tasks}" config="${CONFIG_NAME}" ecode || exit

cd "$DIRPATH" || exit
bash ../../scripts/copy_ecode_assets.sh ../../bin ecode.app || exit
mkdir -p ecode.app/libs
chmod +x AppRun
cp AppRun ecode.app/
cp ecode.desktop ecode.app/
cp ../../../bin/assets/icon/ecode.png ecode.app/ecode.png
cp $SO_LIB_PATH ecode.app/libs/
cp ../../../bin/ecode ecode.app/ecode.bin
cp -L /usr/local/lib/libSDL2-2.0.so.0 ecode.app/libs/
strip ecode.app/libs/libSDL2-2.0.so.0

if [ -n "$VERSION" ];
then
ECODE_VERSION="$VERSION"
else
VERSIONPATH=../../../src/tools/ecode/version.hpp
ECODE_MAJOR_VERSION=$(grep "define ECODE_MAJOR_VERSION" $VERSIONPATH | awk '{print $3}')
ECODE_MINOR_VERSION=$(grep "define ECODE_MINOR_VERSION" $VERSIONPATH | awk '{print $3}')
ECODE_PATCH_LEVEL=$(grep "define ECODE_PATCH_LEVEL" $VERSIONPATH | awk '{print $3}')
ECODE_VERSION="$ECODE_MAJOR_VERSION"."$ECODE_MINOR_VERSION"."$ECODE_PATCH_LEVEL"
fi

ECODE_NAME=ecode-freebsd-"$ECODE_VERSION"-x86_64

mv ecode.app/AppRun ecode.app/ecode
mv ecode.app ecode

echo "Generating $ECODE_NAME.tar.gz"
tar -czf "$ECODE_NAME".tar.gz ecode

mv ecode ecode.app
