#!/bin/sh -e

TEMP=/tmp/gnubgtest.$$

if [ -n "$srcdir" ]; then
    dirparam="--datadir=$srcdir"
else
    dirparam=
fi

die () {
    echo "$1"
    exit 1
}

# Feature test
./gnubg -v > $TEMP || die '--- Feature test fails.'
grep Guile $TEMP > /dev/null && GUILE=yes || GUILE=""
grep databases $TEMP > /dev/null && DATABASES=yes || DATABASES=""
grep Window $TEMP > /dev/null && WINDOW=yes || WINDOW=""
rm -f $TEMP
echo '--- Feature test succeeds.'

# Make sure an opening 31 is played correctly.
./gnubg $dirparam -t -q -r << "EOF" | grep sGfwATDgc/ABMA > /dev/null || die \
    '--- Opening move test fails.'
set player both human
new game
set turn 0
set dice 3 1
set player 0 gnubg
play
EOF
echo '--- Opening move test succeeds.'

# Play a game with pubeval.
./gnubg $dirparam -t -q -r << "EOF" > /dev/null || die '--- Pubeval test fails.'
set automatic game no
set cube use no
set player both pubeval
new game
EOF
echo '--- Pubeval test succeeds.'

# Play a cubeless game.
./gnubg $dirparam -t -q -r << "EOF" > /dev/null || die '--- Self-play test fails.'
set automatic game no
set cube use no
set player both gnubg
set player both chequerplay type eval
set player both chequerplay eval plies 0
new game
EOF
echo '--- Self-play test succeeds.'

# Play a cubeful game.
./gnubg $dirparam -t -q -r << "EOF" > /dev/null || die \
    '--- Cubeful self-play test fails.'
set automatic game no
set player both gnubg
set player both chequerplay type eval
set player both chequerplay eval plies 0
set player both cubedecision type eval
set player both cubedecision eval plies 0
new game
EOF
echo '--- Cubeful self-play test succeeds.'

# Test game navigation (and save game for later load test).
./gnubg $dirparam -t -q -r << EOF | grep '167.*163' > /dev/null || die \
    '--- Game navigation fails.'
set rng manual
set player both human
new game
31
8/5 6/5
roll
42
8/4 6/4
previous
previous
next
next
previous
save game $TEMP
show pipcount
EOF
echo '--- Game navigation succeeds.'

# Test resignation.
./gnubg $dirparam -t -q -r << "EOF" | grep 'a 0, b 4' > /dev/null || die \
    '--- Resignation test fails.'
set rng manual
set player both human
set player 0 name a
set player 1 name b
set automatic game no
new game
31
8/5 6/5
double
take
roll
42
8/4 6/4
resign gammon
accept
EOF
echo '--- Resignation test succeeds.'

# Test beavers.
./gnubg $dirparam -t -q -r << "EOF" | grep 'a 0, b 4' > /dev/null || die \
    '--- Beaver test fails.'
set rng manual
set player 0 human
set player 0 name a
set player 1 gnu
set player 1 name b
set player 1 chequerplay type eval
set player 1 chequerplay eval plies 0
set player 1 cubedecision type eval
set player 1 cubedecision eval plies 0
set automatic game no
new game
21
6/5 6/4
11
double
take
11
resign normal
EOF
echo '--- Beaver test succeeds.'

# Test saving.
./gnubg $dirparam -t -q -r << EOF | grep '163.*161' > /dev/null || die \
    '--- Load test fails.'
set player both human
load game $TEMP
show pipcount
EOF
rm -f $TEMP
echo '--- Load test succeeds.'

# Test miscellaneous commands.
./gnubg $dirparam -t -q -r << EOF > /dev/null || die \
    '--- Miscellaneous test fails.'
set evaluation chequerplay eval plies 1
set evaluation cubedecision eval plies 1
eval
eval 4HPwATDgc/ABMA
eq2mwc
mwc2eq
rollout
rollout =cube
help
new match 1
eval
eq2mwc
mwc2eq
set rollout trials 8
set rollout truncation 8
set rollout cubeful no
set rollout player both chequerplay plies 0
set rollout player both cubedecision plies 0
set rollout varredn no
rollout
rollout =cube
show analysis
show automatic
show beavers
show board
show cache
show clockwise
show commands
show confirm
show copying
show crawford
show cube
show delay
show dice
show display
show egyptian
show engine
show evaluation
show gammonprice
show jacoby
show kleinman
show marketwindow
show matchequitytable
show nackgammon
show output
show pipcount
show player
show postcrawford
show prompt
show rng
show rollout
show score
show seed
show statistics game
show thorp
show training
show turn
show version
show warranty
eq2mwc
mwc2eq
list game
list match
list session
export match mat $TEMP
export match latex $TEMP
export match pdf $TEMP
export match postscript $TEMP
export position eps $TEMP
export position pos $TEMP
EOF
rm -f $TEMP
echo '--- Miscellaneous test succeeds.'

# Make sure the example game in the manual works.
./gnubg $dirparam -t -q -r << "EOF" | grep \
   'refuses the cube and gives up 1 point.' > /dev/null || die \
   '--- Manual example test fails.'
set seed 15
new game
8 5 6 5
roll
13 10 6 1
roll
13 11 6 1
double
EOF
echo '--- Manual example test succeeds.'

# Test Guile.
if [ "$GUILE" = "yes" ]; then
    cat << 'EOF' > $TEMP
(catch #t (lambda ()
	    (or (= foo 123)
		(throw #t))
	    (or (equal? (board->position-id (current-board)) "sGfwATDgc/ABMA")
		(throw #t))
	    (or (< (vector-ref (evaluate-position (current-board)) 0) 0.5)
		(throw #t))
	    (display "successful")
	    (newline)
	    (primitive-exit 0))
       (lambda (tag . args)
	 (primitive-exit 1)))
EOF
    ./gnubg $dirparam -t -q -r << EOF | grep successful > /dev/null || \
    die '--- Guile test fails.'
set rng manual
set player both human
set automatic game no
new game
31
8/5 6/5
:(define foo 123)
:(load "$TEMP")
EOF
    rm -f $TEMP
    echo '--- Guile test succeeds.'
else
    echo '--- Guile test not available.'
fi
