lorchess/game-add

140 lines
3.6 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
# Copyright 2014 Vladimir Ivanov <ivvl82@gmail.com>
# Distributed under the terms of the GNU General Public License v2
REPODIR="`dirname $0`"
TOURNAMENT="2014/2-summertime"
# Config file for players
ply_ini="${REPODIR}/${TOURNAMENT}/players.ini"
get_game_players() {
# Extract players on Lichess
local w_lichess=$(sed -En "s/\[White \"([^\"]*)\"\]/\1/p" < "$tmp_pgn")
local b_lichess=$(sed -En "s/\[Black \"([^\"]*)\"\]/\1/p" < "$tmp_pgn")
# Get names of white and black players
local counter=1 players=()
parse_config
while eval "config_section_player${counter}" 2>/dev/null; do
players+=("$name")
[[ "$lichess" == "$w_lichess" ]] && white="$name"
[[ "$lichess" == "$b_lichess" ]] && black="$name"
counter=$((counter+1))
done
[[ -z "$white" ]] && fix_white_player
[[ -z "$black" ]] && fix_black_player
}
parse_config() {
# Copy player INI file to the temporary location
2014-05-08 20:06:57 +04:00
# NOTE: an empty line is added to the file beginning in order to
# match the only first occurrence for non-GNU sed
echo > "$tmp_ini"
cat "$ply_ini" >> "$tmp_ini"
# Remove tabs or spaces around the `='
2014-05-08 20:06:57 +04:00
sed -i".prev" -E "s/[[:blank:]]*=[[:blank:]]*/=/g" "$tmp_ini"
# Transform section labels into function declaration
2014-05-08 20:06:57 +04:00
sed -i".prev" -E "1,/^\[.*\]/s/^\[([^]]*)\]/config_section_\1() {/g" "$tmp_ini"
sed -i".prev" -E "s/^\[([^]]*)\]/}\\"$'\n'"config_section_\1() {/g" "$tmp_ini"
echo -e "\n}" >> "$tmp_ini"
# Load the file
source "$tmp_ini"
}
fix_white_player() {
local number
2014-05-08 20:06:57 +04:00
echo "Lichess player '${w_lichess}' not found."
choose_player
white="${players[$number]}"
}
fix_black_player() {
local number
2014-05-08 20:06:57 +04:00
echo "Lichess player '${b_lichess}' not found."
choose_player
black="${players[$number]}"
}
choose_player() {
local answer
echo -n "Would you like to choose a name? (Y/n)> "
read answer
2014-05-08 20:06:57 +04:00
[[ "$answer" =~ ^(Y|y|Yes|yes)$ ]] || exit 1
for ((i=0; i<${#players[@]}; ++i)); do
echo "$((i+1)) ${players[$i]}"
done \
| column -t \
| sed -E "s/^([0-9]*)/$(tput setaf 6)\1$(tput sgr0)/g" # highlight number
echo -n "Put number> "
read number
2014-05-08 20:06:57 +04:00
if (( "$number" < 1 || "$number" > ${#players[@]} )); then
die "Incorrect player number."
fi
number=$((number-1))
}
die() {
echo "$@" 1>&2
exit 1
}
checkargs() {
if [[ "$opt" == t && ! "$OPTARG" =~ ^[0-9]+$ ]]; then
echo "Incorrect tour number" 1>&2
exit 1
fi
}
TOUR=""
while getopts "t:hv" opt; do
case "$opt" in
t) checkargs
TOUR=$(printf "%02g" "$OPTARG")
;;
h) usage && exit 0
;;
v) version && exit 0
;;
esac
done
shift $(($OPTIND - 1))
# For now, tour number should be given explicitly
[[ -z "$TOUR" ]] && die "Tour number not specified."
# Temporary files
tmp_ini=$(mktemp -t `basename $ply_ini`.XXXXXX)
tmp_pgn=$(mktemp -t pgn.XXXXXX)
trap "rm ${tmp_ini} ${tmp_ini}.prev ${tmp_pgn}" EXIT
# Download PGN file
[[ "$1" =~ ^(http://[^/]*)/([^/]*) ]]
pgn_url="${BASH_REMATCH[1]}/${BASH_REMATCH[2]:0:8}/pgn"
curl -q --fail --location --silent "$pgn_url" > "$tmp_pgn" \
|| die "PGN file not found."
white="" black=""
get_game_players
# Extract the game date
date=$(sed -En "s/\[Date \"([^\"]*)\"\]/\1/p" < "$tmp_pgn")
date=$(tr '.' '-' <<< "$date")
pgn_dir="${REPODIR}/${TOURNAMENT}/tours/${TOUR}/${date}-${white}-vs-${black}"
2014-05-08 20:06:57 +04:00
[[ -d "$pgn_dir" ]] && die "Directory ${pgn_dir} already exist."
2014-05-08 20:06:57 +04:00
echo "Creating directory ${pgn_dir}..."
mkdir -p "$pgn_dir"
echo "Storing PGN file..."
2014-05-08 20:06:57 +04:00
cp "$tmp_pgn" "${pgn_dir}/1.pgn"
exit 0