# Copyright 2011 Elias Pipping <pipping@exherbo.org>
# Copyright 2016 Julian Ospald <hasufell@posteo.de>
# Distributed under the terms of the GNU General Public License v2

require gtk-icon-cache game

MY_PN=stone_soup
MY_PNV=${MY_PN}-${PV}

SUMMARY="Single-player, role-playing roguelike game"
DESCRIPTION="
    Dungeon Crawl Stone Soup is a free roguelike game of exploration
    and treasure-hunting in dungeons filled with dangerous and
    unfriendly monsters in a quest for the mystifyingly fabulous Orb
    of Zot.

    Dungeon Crawl Stone Soup has diverse species and many different
    character backgrounds to choose from, deep tactical game-play,
    sophisticated magic, religion and skill systems, and a grand
    variety of monsters to fight and run from, making each game
    unique and challenging.

    Dungeon Crawl Stone Soup can be played offline, or online on a
    public telnet/ssh server thanks to the good folks at
    crawl.akrasiac.org (CAO) and crawl.develz.org (CDO). These public
    servers allow you to meet other players’ ghosts, watch other people
    playing, and, in general, have a blast!
"
HOMEPAGE="https://crawl.develz.org/wordpress/"
DOWNLOADS="https://crawl.develz.org/release/${MY_PNV}-nodeps.tar.xz"

LICENCES="
    GPL-2
    BSD-3 [[ note = [ mt19937ar.cc, MSVC/stdint.h ] ]]
    BSD-2 [[ note = [ all contributions by Steve Noonan and Jesse Luehrs ] ]]
    public-domain [[ note = [ most of tiles ] ]]
    CC0 [[ note = [ most of tiles ] ]]
    MIT [[ note = [ json.cc/json.h, some .js files in
                    webserver/static/scripts/contrib/ ] ]]
"
SLOT="0"
PLATFORMS="~amd64 ~x86"
MYOPTIONS="
    tiles [[ description = [ Do graphical (tiled) build instead of
                             ncurses build ] ]]
"
RESTRICT="test" # lots of sydbox access violations

DEPENDENCIES="
    build:
        dev-lang/perl:*
        sys-devel/bison
        sys-devel/flex
        virtual/pkg-config
        tiles? ( sys-libs/ncurses )
    build+run:
        dev-db/sqlite:3
        dev-lang/lua:5.1
        sys-libs/zlib
        !tiles? ( sys-libs/ncurses )
        tiles? (
            fonts/dejavu
            media-libs/freetype:2
            media-libs/libpng:=
            media-libs/SDL:2[X]
            media-libs/SDL_image:2
            x11-dri/glu
            x11-dri/mesa
        )
"

WORK=${WORKBASE}/${MY_PNV}/source

MY_SAVEDIR=/var/lib/games/${MY_PN}

# merged upstream: https://github.com/crawl/crawl/pull/237
DEFAULT_SRC_PREPARE_PATCHES=(
    -p3 "${FILES}"/0001-${PV}-MAKE-use-PKGCONFIG-to-get-ncurses-flags-if-available.patch
    -p3 "${FILES}"/0002-${PV}-MAKE-always-use-PKGCONFIG-instead-of-pkg-config.patch
    -p3 "${FILES}"/0003-${PV}-MAKE-allow-to-skip-automagic-gold-linker-detection.patch
)

DEFAULT_SRC_COMPILE_PARAMS=(
    DATADIR="/usr/share/${PN}"
    SAVEDIR=${MY_SAVEDIR}
    DESTDIR="${IMAGE}"
    bin_prefix="${IMAGE}/usr/$(exhost --target)/bin"
    prefix_fp=""
    prefix="/usr"

    USE_LUAJIT=
    BUILD_ALL=
    NO_TRY_GOLD=y
    V=1

    INSTALL_UGRP=wizard:games
    MCHMOD=ug+s

    EXTERNAL_FLAGS="${CXXFLAGS}"
    EXTERNAL_LDFLAGS="${LDFLAGS}"

    AR="${AR}"
    RANLIB="${RANLIB}"
    CC="${CC}"
    CXX="${CXX}"
    PKGCONFIG="${PKG_CONFIG}"
    STRIP=echo
)

src_compile() {
    export HOSTCXX=${CXX} GXX=${CXX}

    emake "${DEFAULT_SRC_COMPILE_PARAMS[@]}" \
        $(option tiles "TILES=y" "")
}

src_install() {
    emake "${DEFAULT_SRC_COMPILE_PARAMS[@]}" \
        $(option tiles "TILES=y" "") \
        install

    keepdir ${MY_SAVEDIR}/morgue
    keepdir ${MY_SAVEDIR}/saves/db
    preserve_scores "${IMAGE}"${MY_SAVEDIR}

    dovarlibgames -R
}

pkg_preinst() {
    game_pkg_preinst
    gtk-icon-cache_pkg_preinst
}

pkg_postinst() {
    game_pkg_postinst
    gtk-icon-cache_pkg_postinst
}