# Copyright 2016 Julian Ospald <hasufell@posteo.de>
# Distributed under the terms of the GNU General Public License v2

require desktop-utils github [ user=JACoders ] cmake [ api=2 ]
require gtk-icon-cache

SUMMARY="Open Source Jedi Academy and Jedi Outcast games engine"

LICENCES="GPL-2"
SLOT="0"
PLATFORMS="-* ~amd64 ~x86"
MYOPTIONS="
    ( platform: amd64 x86 )
    ( providers: ijg-jpeg jpeg-turbo ) [[ number-selected = exactly-one ]]
"

DEPENDENCIES="
    build+run:
        media-libs/SDL:2[X]
        media-libs/libpng:=
        sys-libs/zlib
        x11-dri/mesa
        providers:ijg-jpeg? ( media-libs/jpeg )
        providers:jpeg-turbo? ( media-libs/libjpeg-turbo )
"

CMAKE_SRC_CONFIGURE_PARAMS=(
    # cmake.exlib wrongly overrides the build type with a random "None",
    # breaking the build system
    -DCMAKE_BUILD_TYPE=RELEASE

    -DCMAKE_INSTALL_PREFIX="/usr/$(exhost --target)/lib"

    -DBuildMPEngine=YES
    -DBuildMPRdVanilla=YES
    -DBuildSPEngine=YES
    -DBuildSPGame=YES
    -DBuildSPRdVanilla=YES
)

src_install() {
    local i
    local mylibdir="/usr/$(exhost --target)/lib"
    # silly, but common for games crap
    local myext=$(option platform:amd64 "x86_64" "i386")

    cmake_src_install

    for i in openjk openjk_sp openjkded ; do
        herebin ${i} <<EOF
#!/bin/sh
cd "${mylibdir}/JediAcademy/"
if [ "\${LD_LIBRARY_PATH+set}" = "set" ] ; then
export LD_LIBRARY_PATH="\${LD_LIBRARY_PATH}:${mylibdir}/JediAcademy/base:${mylibdir}/JediAcademy/OpenJK"
else
export LD_LIBRARY_PATH="${mylibdir}/JediAcademy/base:${mylibdir}/JediAcademy/OpenJK"
fi
exec ./${i}.${myext} "\$@"
EOF
    done

    install_desktop_entry \
        "Name=Jedi Academy Multi Player" \
        "Comment=Open Source Jedi Academy and Jedi Outcast games engine" \
        "Exec=openjk" \
        "TryExec=openjk" \
        "Icon=OpenJK"

    install_desktop_entry \
        "Name=Jedi Academy Single Player" \
        "Comment=Open Source Jedi Academy and Jedi Outcast games engine" \
        "Exec=openjk_sp" \
        "TryExec=openjk_sp" \
        "Icon=OpenJK"

    insinto /usr/share/icons/hicolor/128x128/apps
    newins "${CMAKE_SOURCE}"/shared/icons/OpenJK_Icon_128.png ${PN}.png
}

pkg_postinst() {
    elog "You need to copy GameData/base/*.{pk3,cfg} from either your"
    elog "installation media or your hard drive to"
    elog "~/.local/share/openjk/base before running the game."

    gtk-icon-cache_pkg_postinst
}