#!/bin/sh

PATH=$PATH:/usr/games

if test -e "$HOME/.crafty" -a ! -d "$HOME/.crafty"; then
	mv $HOME/.crafty $HOME/.crafty-backup
fi

test ! -d "$HOME/.crafty" && mkdir $HOME/.crafty
test ! -d "$HOME/.crafty/TB" && mkdir $HOME/.crafty/TB

for i in book.bin bookc.bin books.bin; do
	# don't overwrite opening books if user already have them
	if test -f /usr/share/games/crafty/$i -a ! -f $HOME/.crafty/$i; then
		install -m 644 /usr/share/games/crafty/$i $HOME/.crafty/$i
	fi
done

# crafty reads and writes everything in current path
cd $HOME/.crafty

exec /usr/games/crafty.real "$@"
