#! /bin/sh
#
# Run xboard with glaurung121 as chess program.
#
# modified from "xcrafty" originally from:
#
# Copyright (c) 1999 SuSE GmbH Nuernberg, Germany.  All rights reserved.
#
# Author: Steffen Winterfeldt <wfeldt@suse.de>
#

NAME_OF_ENGINE=glaurung121
NAME_OF_BOOKFILE=book.bin
NAME_OF_EXTRA_FILE1=kpk.bin

[ "$DISPLAY" ] || {
  echo >&2 "This is not a text mode application!"
  exit 63
}

emsg="You\'ll have to install package \'xboard\' first to run x$NAME_OF_ENGINE."
emsg2="You\'ll have to install package \'polyglot\' first to run x$NAME_OF_ENGINE."

which xboard >/dev/null 2>&1 || {
  if tty -s ; then
    sh -c "echo >&2 $emsg"
  else
    xterm +sb -T "x$NAME_OF_ENGINE error" -bg white -fg red -e sh -c "echo $emsg ; read"
  fi
  exit 42
}

which polyglot >/dev/null 2>&1 || {
  if tty -s ; then
    sh -c "echo >&2 $emsg2"
  else
    xterm +sb -T "x$NAME_OF_ENGINE error" -bg white -fg red -e sh -c "echo $emsg2 ; read"
  fi
  exit 42
}

# Check if all files are present in "$HOME" and if not, create them
if [ ! -d "$HOME"/.$NAME_OF_ENGINE ] ; then
	mkdir -p "$HOME"/.$NAME_OF_ENGINE ;
fi

if [ ! -e "$HOME"/.$NAME_OF_ENGINE/$NAME_OF_ENGINE.ini ] ; then
	cp /usr/share/$NAME_OF_ENGINE/$NAME_OF_ENGINE.ini "$HOME"/.$NAME_OF_ENGINE/
	sed -i "s|/usr/share/$NAME_OF_ENGINE|"$HOME"/.$NAME_OF_ENGINE|g" "$HOME"/.$NAME_OF_ENGINE/$NAME_OF_ENGINE.ini
	sed -i "s|/tmp|"$HOME"/.$NAME_OF_ENGINE|g" "$HOME"/.$NAME_OF_ENGINE/$NAME_OF_ENGINE.ini ;
fi

if [ ! -e "$HOME"/.$NAME_OF_ENGINE/$NAME_OF_BOOKFILE ] ; then
	cp /usr/share/$NAME_OF_ENGINE/$NAME_OF_BOOKFILE "$HOME"/.$NAME_OF_ENGINE/ ;
fi

if [ ! -e "$HOME"/.$NAME_OF_ENGINE/$NAME_OF_EXTRA_FILE1 ] ; then
	cp /usr/share/$NAME_OF_ENGINE/$NAME_OF_EXTRA_FILE1 "$HOME"/.$NAME_OF_ENGINE/ ;
fi


OPT="-tc 5 -inc 0 -size bulky -ponder -thinking"

exec xboard $OPT -fd "$HOME"/.$NAME_OF_ENGINE/ -fcp "polyglot $NAME_OF_ENGINE.ini" -sd "$HOME"/.$NAME_OF_ENGINE/ -scp "polyglot $NAME_OF_ENGINE.ini" "$@" &
