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

require 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.12.0.16.sh"
DLC1_SRC_DL="gog_pillars_of_eternity_kickstarter_item_dlc_2.0.0.2.sh"
DLC2_SRC_DL="gog_pillars_of_eternity_kickstarter_pet_dlc_2.0.0.2.sh"
DLC3_SRC_DL="gog_pillars_of_eternity_preorder_item_and_pet_dlc_2.0.0.2.sh"
DOWNLOADS="
    manual:
        ${BASE_SRC_DL}
        dlc1? ( ${DLC1_SRC_DL} )
        dlc2? ( ${DLC2_SRC_DL} )
        dlc3? ( ${DLC3_SRC_DL} )
"

LICENCES="all-rights-reserved GOG-EULA"
SLOT="0"
PLATFORMS="-* ~amd64"
MYOPTIONS="
    dlc1 [[ description = [ Install the Kickstarter Bonus Item ] ]]
    dlc2 [[ description = [ Install the Kickstarter Bonus Pet ] ]]
    dlc3 [[ description = [ Install the Pre-Order Bonus Item and Pet ] ]]
"
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 dlc1 && einfo "and \"${DLC1_SRC_DL}\""
    option dlc2 && einfo "and \"${DLC2_SRC_DL}\""
    option dlc3 && einfo "and \"${DLC3_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() {
    if option dlc1 ; then
        unpack_zip "${FETCHEDDIR}/${DLC1_SRC_DL}"
    fi

    if option dlc2 ; then
        unpack_zip "${FETCHEDDIR}/${DLC2_SRC_DL}"
    fi

    if option dlc3 ; then
        unpack_zip "${FETCHEDDIR}/${DLC3_SRC_DL}"
    fi

    unpack_zip "${FETCHEDDIR}/${BASE_SRC_DL}"
}

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

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

    dodoc game/Docs/{pe-game-manual.pdf,readme.txt}

    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
}