# Copyright 1999-2011 Gentoo Foundation
# Copyright 2014-2016 Julian Ospald <hasufell@posteo.de>
# Distributed under the terms of the GNU General Public License v2

require sourceforge [ project=oarena pnv=${PN}-engine-source-${PV} ]

SUMMARY="Open-source replacement for Quake 3 Arena"
HOMEPAGE="http://openarena.ws/"
DOWNLOADS+=" mirror://sourceforge/oarena/${PNV}.zip"

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

DEPENDENCIES="
    build:
        virtual/unzip
    build+run:
        media-libs/SDL:0[X]
        media-libs/libvorbis
        media-libs/openal
        media-libs/speex
        net-misc/curl
        x11-dri/mesa
        x11-libs/libX11
        x11-libs/libXau
        x11-libs/libXdmcp
        x11-libs/libXext
        providers:ijg-jpeg? ( media-libs/jpeg:= )
        providers:jpeg-turbo? ( media-libs/libjpeg-turbo )
"

MY_S=${WORKBASE}/${PN}-engine-source-${PV}
BUILD_DIR=${PN}-build
OA_SHARE_DIR=/usr/share/${PN}

src_prepare() {
    edo pushd "${WORKBASE}"
    expatch -p0 "${FILES}"/${PNV}-makefile.patch
    expatch -p0 "${FILES}"/${PNV}-unbundling.patch
    edo popd
    edo touch "${MY_S}"/jpegint.h
}

src_compile() {
    emake -C ${MY_S} \
        V=1 \
        DEFAULT_BASEDIR="${OA_SHARE_DIR}" \
        BR="${BUILD_DIR}" \
        USE_INTERNAL_SPEEX=0 \
        USE_VOIP=1 \
        USE_MUMBLE=0 \
        BUILD_SERVER=1 \
        BUILD_CLIENT_SMP=1 \
        USE_LOCAL_HEADERS=0 \
        USE_CURL=0 \
        USE_OPENAL=0 \
        USE_CODEC_VORBIS=0 \
        OPTIMIZE=
}

src_install() {
    edo pushd "${MY_S}/${BUILD_DIR}"
    newbin openarena-smp.* ${PN}
    newbin oa_ded.* "${PN}-ded"
    edo popd

    insinto "${OA_SHARE_DIR}"
    doins -r baseoa missionpack

    dodoc CHANGES CREDITS LINUXNOTES README

    insinto /usr/share/pixmaps
    newins "${MY_S}"/misc/quake3.png ${PN}.png

    insinto /usr/share/applications/
    hereins ${PN}.desktop << EOF
[Desktop Entry]
Name=OpenArena
Type=Application
Comment=${SUMMARY}
Exec=${PN}
TryExec=${PN}
Icon=${PN}
Categories=Game;ActionGame;
EOF
}