#!/bin/bash

# https://labraj.feri.um.si/en/Tournaments_play_(XBoard)

GAMES=2
TITLE=Test
RATINGOFFSET=2000
#MODE="-depth 6"
#MODE="-st 0:5"
MODE="-tc 1"
ENGINE[0]="gnuchess"
DIR[0]="/usr/games"
ENGINE[1]="crafty"
DIR[1]="/usr/games"
PGNFILE=$TITLE"_games_`date '+%d_%m_%G'`.pgn"
RATINGFILE=$TITLE"_games_`date '+%d_%m_%G'`.rating"
rm -f "$PGNFILE" "$RATINGFILE"
SIZE=${#ENGINE[*]};

for ((i=0; i<$SIZE; i++)) do
  for ((j=$i+1; j<$SIZE; j++)) do
    if [ $j%2 == 0 ]; then
      /home/borko/Prenos/xboard-4.4.0.beta1/xboard $MODE -xexit -size Big -coords -thinking -highlight \
      -mg $GAMES -sgf "$PGNFILE" -pgnExtendedInfo true -pgnEventHeader "$TITLE" \
      -fd "${DIR[$i]}" -fcp "${ENGINE[$i]}" -sd "${DIR[$j]}" -scp "${ENGINE[$j]}"
    else
      /home/borko/Prenos/xboard-4.4.0.beta1/xboard $MODE -xexit -size Big -coords -thinking -highlight \
      -mg $GAMES -sgf "$PGNFILE" -pgnExtendedInfo true -pgnEventHeader "$TITLE" \
      -sd "${DIR[$i]}" -scp "${ENGINE[$i]}" -fd "${DIR[$j]}" -fcp "${ENGINE[$j]}"
    fi
  done
done

echo -e "readpgn $PGNFILE\nelo\n\t offset $RATINGOFFSET\n\t mm\n\t ratings>$RATINGFILE\n\t x\n\nx\n" | bayeselo > /dev/null 2> /dev/null cat $RATINGFILE
