#!/usr/bin/env bash # Copyright 2014 Vladimir Ivanov # Distributed under the terms of the GNU General Public License v2 # REPO_DIR="https://github.com/fat0troll/lorchess" REPO_DIR=`dirname "$0"` # Specify the tournament here TOURNAMENT= # Version information VERSION="0.5" argv0=${0##*/} function usage { cat < '01', '2' -> '02', and so on tour=$(printf "%02g" $tour) # *NOTE* The incorporation of a newline at the end of # 'tour_info' is important and allows one to read the last # line with no trailing '\n' if [[ $REPO_DIR =~ ^https://github\.com/(.*) ]]; then local tour_info=$(curl $CURL_OPTS --write-out '\n' \ ${REPO_DIR}/raw/master/${TOURNAMENT}/tours/${tour}/tour_info) [[ -n $tour_info ]] && output_tour_info else local info_file=${REPO_DIR}/${TOURNAMENT}/tours/${tour}/tour_info if [[ -f $info_file ]]; then local tour_info=$(cat $info_file; echo) output_tour_info fi fi done } function output_tour_info { local tour_num date_beg date_end local white black result game_date game_url # Lines with game info local output_lines= while read line; do # Read 'tour_num' get_tour_num "$line" && continue # Read 'date_beg' and 'date_end' get_tour_dates "$line" && continue # Keep game info if needed, and store it in variables 'white', # 'black', 'result', and 'game_date' if keep_game_info "$line"; then # Store the link to game in 'game_url' game_url= if $SHOW_LINK; then [[ -n $result ]] && store_game_url fi output_lines+="${white} ${black} ${result} ${game_url}\n" fi done <<< "$tour_info" [[ -n $output_lines ]] && eval "info_output_$FORMAT \"${output_lines}\"" } function get_tour_num { if [[ $1 =~ ^"Тур №"([0-9]+) ]]; then tour_num=${BASH_REMATCH[1]} return 0 else return 1 fi } function get_tour_dates { local date_re="[0-9?]{2}\.[0-9?]{2}\.[0-9?]{4}" if [[ $1 =~ ^"Время проведения:"\ +($date_re)\ +-\ +($date_re) ]]; then date_beg=${BASH_REMATCH[1]} date_end=${BASH_REMATCH[2]} # Use short dates date_beg=${date_beg::5} date_end=${date_end::5} return 0 else return 1 fi } function keep_game_info { local date_re="([0-9?]{2})\.([0-9?]{2})\.([0-9?]{4})" local res_re="(1|1/2|0)-(1|1/2|0)" local line=$1 keep=1 if [[ $line =~ ^($date_re)\ +([^\ ]+)\ +-\ +([^\ ]+)\ *($res_re)? ]]; then white=${BASH_REMATCH[5]} black=${BASH_REMATCH[6]} result=${BASH_REMATCH[7]} game_date=${BASH_REMATCH[4]}-${BASH_REMATCH[3]}-${BASH_REMATCH[2]} # If the player was passed as an argument, check if it is his game or not if [[ -z $PLAYER || $white =~ $PLAYER || $black =~ $PLAYER ]]; then if $SHOW_ALL; then keep=0 else # Keep line if the game is not finished [[ -z $result ]] && keep=0 fi fi fi return $keep } function store_game_url { local tour=$(printf "%02g" "$tour_num") local game_dir=${game_date}-${white}-vs-${black} local pgn_url=${REPO}/${TOURNAMENT}/tours/${tour}/${game_dir}/1.pgn while read line; do # Search for a URL inside PGN file [[ $line =~ "[Site \""([a-z]+:[^\"]+)"\"]" ]] game_url=${BASH_REMATCH[1]} [[ -n $game_url ]] && break done <<< $(curl -q --fail --location --silent "$pgn_url") } function info_output_term { echo -n "$(tput setaf 2)${tour_num} тур " echo "$(tput setaf 6)(${date_beg} - ${date_end})$(tput sgr0)" echo -en "$1" | column -t | while IFS= read line; do # Highlight player's name if [[ -n $PLAYER ]]; then players=$(grep -o "[^ ]*${PLAYER}[^ ]*" <<< "$line") for name in $players; do # PLAYER is a part of 'game_url' or 'result' [[ $name =~ ^http:// || $name =~ ^(1-0|1/2-1/2|0-1|)$ ]] && continue line=$(sed -E "s/(${name})/$(tput setaf 1)\1$(tput sgr0)/g" <<< "$line") done fi # Highlight result line=$(sed -E "s/ (1-0|1\/2-1\/2|0-1)( |$)/ $(tput setaf 6)\1$(tput sgr0)\2/g" <<< "$line") echo "$line" done echo } function info_output_lor { echo "[b]${tour_num} тур (${date_beg} - ${date_end})[/b]" echo "[list]" echo -en "$1" | while read white black result url; do [[ -n $url ]] && result="[url=${url}]${result}[/url]" echo " [*] [user]${white}[/user] - [user]${black}[/user] ${result}" done echo "[/list]" echo } function info_output_html { echo "
" echo " " echo " " echo " " echo -en "$1" | while read white black result url; do [[ -n $url ]] && result="${result}" echo " " echo " " echo " " done echo " " echo "
" echo " ${tour_num} тур (${date_beg} - ${date_end})" echo "
${white} - ${black}${result}
" echo "
" echo } function die { echo "$@" 1>&2 exit 1 } function checkargs { [[ $OPTARG =~ ^-[apflhv]$ ]] && die "Option -${opt}: argument not found" [[ $opt == f && ! $OPTARG =~ ^(term|html|lor)$ ]] && die "Incorrect FORMAT specified" } FORMAT=term SHOW_ALL=false SHOW_LINK=false while getopts ap:f:lhv opt; do case $opt in a) SHOW_ALL=true ;; p) checkargs PLAYER=$OPTARG ;; f) checkargs FORMAT=$OPTARG ;; l) SHOW_LINK=true ;; h) usage ;; v) version ;; *) usage 1 ;; esac done shift $(($OPTIND - 1)) setup_repo show_tour_sequence $@ exit 0