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

require desktop-utils gtk-icon-cache

SUMMARY="Pillars Of Eternity"
HOMEPAGE="http://www.gog.com/game/pillars_of_eternity_hero_edition"

BASE_SRC_DL="gog_pillars_of_eternity_2.13.0.17.sh"
KSI_SRC_DL="gog_pillars_of_eternity_kickstarter_item_dlc_2.0.0.2.sh"
KSP_SRC_DL="gog_pillars_of_eternity_kickstarter_pet_dlc_2.0.0.2.sh"
PRE_SRC_DL="gog_pillars_of_eternity_preorder_item_and_pet_dlc_2.0.0.2.sh"
WM1_SRC_DL="gog_pillars_of_eternity_white_march_part_1_dlc_2.7.0.9.sh"
WM2_SRC_DL="gog_pillars_of_eternity_white_march_part_2_dlc_2.3.0.4.sh"
DOWNLOADS="
    manual:
        ${BASE_SRC_DL}
        kickstarter-item? ( ${KSI_SRC_DL} )
        kickstarter-pet? ( ${KSP_SRC_DL} )
        pre-order-items? ( ${PRE_SRC_DL} )
        white-march-1? ( ${WM1_SRC_DL} )
        white-march-2? ( ${WM2_SRC_DL} )
"

LICENCES="all-rights-reserved GOG-EULA"
SLOT="0"
PLATFORMS="-* ~amd64"
MYOPTIONS="
    kickstarter-item [[ description = [ Install the Kickstarter Bonus Item ] ]]
    kickstarter-pet [[ description = [ Install the Kickstarter Bonus Pet ] ]]
    pre-order-items [[ description = [ Install the Pre-Order Bonus Item and
                                       Pet ] ]]
    white-march-1 [[ description = [ Install \"The White March - Part 1\"
                                     expansion pack ] ]]
    white-march-2 [[ description = [ Install \"The White March - Part 2\"
                                     expansion pack ] ]]
"
RESTRICT="fetch"

DEPENDENCIES="
    build:
        virtual/unzip
    run:
        dev-libs/atk
        media-libs/fontconfig
        media-libs/freetype:2
        x11-dri/mesa
        x11-libs/cairo
        x11-libs/gdk-pixbuf:2.0
        x11-libs/gtk+:2
        x11-libs/pango
"

WORK="${WORKBASE}/data/noarch"

pkg_nofetch() {
    einfo
    einfo "Please buy & download \"${BASE_SRC_DL}\""
    option kickstarter-item && einfo "and \"${KSI_SRC_DL}\""
    option kickstarter-pet && einfo "and \"${KSP_SRC_DL}\""
    option pre-order-items && einfo "and \"${PRE_SRC_DL}\""
    option white-march-1 && einfo "and \"${WM1_SRC_DL}\""
    option white-march-2 && einfo "and \"${WM2_SRC_DL}\""
    einfo "from:"
    einfo "  ${HOMEPAGE}"
    einfo "and move/link it to \"${FETCHEDDIR}\""
    einfo
}

pkg_setup() {
    exdirectory --allow /opt
}

unpack_zip() {
    local file="$1"

    unzip -qo "${file}"
    [[ $? -le 1 ]] || die "unpacking ${file} failed!"
}

src_unpack() {
    # unzip
    unpack_zip "${FETCHEDDIR}/${DOWNLOADS}"
}

src_unpack() {
    unpack_zip "${FETCHEDDIR}/${BASE_SRC_DL}"

    if option kickstarter-item ; then
        unpack_zip "${FETCHEDDIR}/${KSI_SRC_DL}"
    fi

    if option kickstarter-pet ; then
        unpack_zip "${FETCHEDDIR}/${KSP_SRC_DL}"
    fi

    if option pre-order-items ; then
        unpack_zip "${FETCHEDDIR}/${PRE_SRC_DL}"
    fi

    if option white-march-1 ; then
        unpack_zip "${FETCHEDDIR}/${WM1_SRC_DL}"
    fi

    if option white-march-2 ; then
        unpack_zip "${FETCHEDDIR}/${WM2_SRC_DL}"
    fi
}

src_install() {
    local dir=/opt/${PN}

    insinto /usr/share/icons/hicolor/512x512/apps
    newins game/PillarsOfEternity.png ${PN}.png

    herebin ${PN} <<EOF
#!/bin/sh
cd "${dir}/game"
exec "./PillarsOfEternity" "\$@"
EOF

    install_desktop_entry "Name=Pillars Of Eternity"

    dodoc docs/pe-game-manual.pdf

    dodir "${dir}"
    edo rm "${WORK}"/game/PillarsOfEternity_Data/Plugins/x86_64/libCSteamworks.so \
        "${WORK}"/game/PillarsOfEternity_Data/Plugins/x86_64/libsteam_api.so
    edo mv "${WORK}/game" "${IMAGE}${dir}/"
    edo chmod +x "${IMAGE}${dir}"/game/PillarsOfEternity

    # there are empty installed dirs for some reason, just keep em
    find "${IMAGE}" -depth -type d -empty -exec sh -c 'touch "${1}"' - '{}'/.keep_${CATEGORY}_${PN}-${SLOT%/*} \; || die
}