# 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="Baldur's Gate: Enhanced Edition"
HOMEPAGE="http://www.gog.com/game/baldurs_gate_enhanced_edition"
BG_SH="gog_baldur_s_gate_enhanced_edition_2.5.0.7.sh"
DOWNLOADS="manual: ${BG_SH}"

LICENCES="all-rights-reserved GOG-EULA"
SLOT="0"
PLATFORMS="-* ~x86"
MYOPTIONS=""
RESTRICT="fetch"

DEPENDENCIES="
    build:
        virtual/unzip
"

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

pkg_pretend() {
    if [[ $(exhost --target) != i686-* ]];then
        die "${CATEGORY}/${PNVR} is i686 only, you'll have to cross compile it :("
    fi
}

pkg_nofetch() {
    einfo
    einfo "Please buy & download \"${BG_SH}\" from:"
    einfo "  ${HOMEPAGE}"
    einfo "and move/link it to \"${FETCHEDDIR}\""
    einfo
}

pkg_setup() {
    exdirectory --allow /opt
}

src_unpack() {
    unzip -qo "${FETCHEDDIR}/${BG_SH}"
    [[ $? -le 1 ]] || die "unpacking ${BG_SH} failed!"
}

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

    insinto "${dir}"
    doins -r game
    edo chmod +x "${IMAGE}${dir}"/game/BaldursGate

    dodir "${dir}/lib"

    edo pushd "${WORK}"/lib
    exeinto "${dir}/lib"
    for f in * ; do
        if [[ -L ${f} ]] ; then
            dosym "$(readlink ${f})" "${dir}"/lib/${f}
        else
            doexe ${f}
        fi
    done
    unset f
    edo popd

    insinto /usr/share/icons/hicolor/256x256/apps
    newins support/icon.png ${PN}.png

    local LD_PATH="/usr/$(exhost --target)/lib/ld-linux.so.2";
    herebin ${PN} <<EOF
#!/bin/sh
cd "${dir}/game"
if [ "\${LD_LIBRARY_PATH+set}" = "set" ] ; then
export LD_LIBRARY_PATH="\${LD_LIBRARY_PATH}:/opt/baldurs-gate-ee/lib"
else
export LD_LIBRARY_PATH="/opt/baldurs-gate-ee/lib"
fi
exec ${LD_PATH} "${dir}/game/BaldursGate" "\$@"
EOF

    install_desktop_entry "Name=Baldurs Gate Enhanced Edition"

    dodoc -r docs/Manuals
}