# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

EAPI=2

inherit cdrom eutils games

DESCRIPTION="Postal 2: Apocalypse Weekend expansion"
HOMEPAGE="http://www.gopostal.com/"
SRC_URI=""

LICENSE="postal2"
SLOT="0"
KEYWORDS="amd64 ~x86"
IUSE=""
RESTRICT="nostrip"

DEPEND=">=games-fps/postal2-1409.1
	|| ( app-arch/unrar
		app-arch/rar )
	app-arch/unzip"
RDEPEND="${DEPEND}"

GAMES_CHECK_LICENSE="yes"

p2dir=${GAMES_PREFIX_OPT}/postal2
awdir=${p2dir}/ApocalypseWeekend

src_unpack() {
	CDROM_NAME_SET=( "CD-ROM Version" "Downloadable Version" )
	cdrom_get_cds postal2aw-linux-installer.sh:postal2aw-linux.bin

	echo -e "\nUnpacking files..."
	case ${CDROM_SET} in
		0) unpack_makeself "${CDROM_ROOT}"/postal2aw-linux-installer.sh || die "Unpacking files" ;;
		1)
			# || die won't work here because unzip returns 1 even on success
			unzip -q "${CDROM_ROOT}/postal2aw-linux.bin"
			[ ! -f "${WORKDIR}/data/postal2aw" ] && die "Unpacking files" 
			;;
		*)
			die "Couldn't find recognized CD/installer"
	esac
}

src_install() {
	has_multilib_profile && ABI=x86

	dodir "${awdir}"
	insinto "${awdir}"

	echo -e "\nCopying files..."

	case ${CDROM_SET} in
		0)
			doins -r "${CDROM_ROOT}"/{Animations,KarmaData,Maps,Movies,Music,Sounds,StaticMeshes,System}
			# Extract textures from the SFX rar archive
			unrar e -o+ "${CDROM_ROOT}/Textures/Textures.exe"
			dodir "${awdir}/Textures"
			insinto "${awdir}/Textures/"
			doins *.utx
			# Cleanup Windows files
			rm -r "${D}/${awdir}"/System/{EditorRes,*.dll,*.DLL,*.exe,*.ico,*.lib,DefaultAW.ini,DefUserAW.ini,DefUnrealEd.ini,EdSplash.bmp}
			# Install manual
			insinto "/usr/share/doc/${PF}"
			doins "${CDROM_ROOT}/{AW Manual (US).pdf"
			# Install icon
			doicon ${PN}.xpm
			;;
		1)
			doins -r "${WORKDIR}"/data/ApocalypseWeekend/*
			dodoc "${WORKDIR}/data/${PN}_license.txt"
			# Install icon
			mv "${WORKDIR}/data/p2aw_rws.png" "${WORKDIR}/data/${PN}.png"
			doicon "${WORKDIR}/data/${PN}.png"
			;;
	esac

	games_make_wrapper ${PN} './postal2-bin "-INI=ApocalypseWeekend.ini" "-USERINI=ApocalypseWeekendUser.ini" "-SAVEDGAMEINI=ApocalypseWeekendSavedGameInfo.ini" "-BASEDIR=../ApocalypseWeekend/System"' "${p2dir}/System/"

	make_desktop_entry ${PN} "Postal 2: Apocalypse Weekend"

	prepgamesdirs
}

pkg_postinst() {
	games_pkg_postinst
	echo
	einfo "Postal 2: Apocalypse Weekend has been successfully installed."
	echo
	einfo "To play, run: ${PN}"
	echo
}