# Template file for 'bsd-games'
pkgname=bsd-games
version=2.17
revision=5
build_style=gnu-configure
make_dirs="
 /var/games/bsdgames/hack 0775 root users
 /var/games/bsdgames/hack/save 0775 root users
 /var/games/bsdgames/phantasia 0775 root users"
hostmakedepends="flex words-en ncurses-devel"
makedepends="ncurses-devel libfl-devel"
depends="words-en"
short_desc="Linux port for a collection of BSD command line games"
maintainer="Leah Neukirchen <leah@vuxu.org>"
license="BSD-3-Clause, BSD-4-Clause"
homepage="ftp://ftp.ibiblio.org/pub/Linux/games/"
distfiles="ftp://ftp.lip6.fr/pub/linux/sunsite/games/${pkgname}-${version}.tar.gz"
checksum=066f924aef6c1c5ea946f588e36f303021f5dfc093944738f025d8edbc6fff60
mutable_files="/var/games/bsdgames/atc_score
 /var/games/bsdgames/battlestar.log
 /var/games/bsdgames/cfscores
 /var/games/bsdgames/criblog
 /var/games/bsdgames/saillog
 /var/games/bsdgames/snake.log
 /var/games/bsdgames/snakerawscores
 /var/games/bsdgames/tetris-bsd.scores
 /var/games/bsdgames/robots_roll
 /var/games/bsdgames/phantasia/characs
 /var/games/bsdgames/phantasia/gold
 /var/games/bsdgames/phantasia/lastdead
 /var/games/bsdgames/phantasia/mess
 /var/games/bsdgames/phantasia/motd
 /var/games/bsdgames/phantasia/scoreboard
 /var/games/bsdgames/phantasia/void
 /var/games/bsdgames/phantasia/monsters
 /var/games/bsdgames/hack/record
 /var/games/bsdgames/hack/perm"
patch_args="-Np1"

if [ "$CROSS_BUILD" ]; then
	local _file _var
	_native_target="adventure/setup boggle/mkdict/mkdict boggle/mkindex/mkindex
		hack/makedefs"
	for _file in $_native_target; do
		_var=${_file//\//_}
		make_build_args+=" ${_var^^}=native/$_file"
	done
	_native_target+=" phantasia/setup"
	make_build_args+=" PHANTASIA_SETUP=../native/phantasia/setup"
fi

if [ "$XBPS_LIBC" = musl ]; then
	hostmakedepends+=" musl-legacy-compat"
fi

if [ "$XBPS_TARGET_LIBC" = musl ]; then
	makedepends+=" musl-legacy-compat"
fi

post_extract() {
	vsed -i "s/FISH/GO-FISH/g; s/\.Nm fish/\.Nm go-fish/g" fish/fish.6
	vsed -i "s/tenths/tenth/g" tests/number.-0.1
	vsed -i "s/Elegy{ Written in a Country Church{-| }Yard:/Elegy{ Written in a Country Church{-| }Yard}:/g" quiz/datfiles/poetry
	vsed -i "s/\.tI friend/\.It friend/g" hunt/hunt/hunt.6.in
	vsed -i  "s/\.I range/\.It range/g" arithmetic/arithmetic.6
	vsed -i "s/game were the/game where the/g" gomoku/gomoku.6
	vsed -i 's|/usr/games|/usr/bin|g' wargames/wargames

	vsed -i '1i#include <sys/types.h>' banner/banner.c
	vsed -i 's/getdate/get_date/g' hack/extern.h hack/hack.{end,unix}.c
	vsed -i '1i#include <fcntl.h>' wump/wump.c
	vsed -i 's/-DOTTO//' hunt/Makeconfig
}

pre_configure() {
	cp ${FILESDIR}/config.params .
	if [ "$CROSS_BUILD" ]; then
		mkdir -p native
		cp -a * native 2>/dev/null || true
		pushd native
		echo "bsd_games_cfg_cc='$CC_FOR_BUILD'" >>config.params
		echo "bsd_games_cfg_cxx='$CXX_FOR_BUILD'" >>config.params
		echo "bsd_games_cfg_other_cflags='$CFLAGS_FOR_BUILD'" >>config.params
		echo "bsd_games_cfg_other_ldflags='$LDFLAGS_FOR_BUILD'" >>config.params
		./configure
		make $_native_target
		popd
	fi
	echo "bsd_games_cfg_cc='$CC'" >>config.params
	echo "bsd_games_cfg_cxx='$CXX'" >>config.params
	echo "bsd_games_cfg_other_cflags='$CFLAGS'" >>config.params
	echo "bsd_games_cfg_other_ldflags='$LDFLAGS'" >>config.params
	echo "bsd_games_cfg_install_prefix='$DESTDIR'" >>config.params
}

do_install() {
	echo >install-score
	make $make_build_args install

	# Remove conflict with fish-shell
	mv "${DESTDIR}/usr/bin/fish" "${DESTDIR}/usr/bin/go-fish"
	mv "${DESTDIR}/usr/share/man/man6/fish.6.gz" "${DESTDIR}/usr/share/man/man6/go-fish.6.gz"

	vdoc "${DESTDIR}/usr/share/doc/trek.me"
	rm "${DESTDIR}/usr/share/doc/trek.me"
	vlicense COPYING
}