| 
									
										
										
										
											2013-09-21 02:20:19 +03:00
										 |  |  |  | #!/usr/bin/env bash | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-20 12:28:50 +03:00
										 |  |  |  | REPO=fat0troll/lorchess | 
					
						
							| 
									
										
										
										
											2014-03-02 17:31:08 +03:00
										 |  |  |  | TOURNEY=2014/1-tabiyas | 
					
						
							| 
									
										
										
										
											2013-09-21 02:20:19 +03:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-23 15:04:10 +03:00
										 |  |  |  | # Variables | 
					
						
							| 
									
										
										
										
											2013-10-20 12:28:50 +03:00
										 |  |  |  | date="[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{4\}" | 
					
						
							|  |  |  |  | result="\(0\|1\|0\.5\):\(0\|1\|0\.5\)" | 
					
						
							| 
									
										
										
										
											2013-09-23 15:04:10 +03:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-23 13:31:09 +03:00
										 |  |  |  | # Colors | 
					
						
							| 
									
										
										
										
											2013-10-20 12:28:50 +03:00
										 |  |  |  | restore="\033[00m" | 
					
						
							|  |  |  |  | red="\033[01;31m" | 
					
						
							|  |  |  |  | green="\033[01;32m" | 
					
						
							|  |  |  |  | yellow="\033[01;33m" | 
					
						
							|  |  |  |  | cyan="\033[01;36m" | 
					
						
							| 
									
										
										
										
											2013-09-23 13:31:09 +03:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-20 12:28:50 +03:00
										 |  |  |  | # Highlight output | 
					
						
							|  |  |  |  | highlight () { | 
					
						
							|  |  |  |  |     line=$1 | 
					
						
							| 
									
										
										
										
											2013-09-21 02:20:19 +03:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-20 12:28:50 +03:00
										 |  |  |  |     # Highlight the tour heading | 
					
						
							|  |  |  |  |     line=$(sed "s/Тур/\\${green}\0\\${restore}/g" <<< "$line") | 
					
						
							|  |  |  |  |     line=$(sed "s/№[0-9]\+/\\${cyan}\0\\${restore}/g" <<< "$line") | 
					
						
							|  |  |  |  |     line=$(sed "s/^=*$/\\${green}\0\\${restore}/g" <<< "$line") | 
					
						
							| 
									
										
										
										
											2013-09-21 02:20:19 +03:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-20 12:28:50 +03:00
										 |  |  |  |     # Highlight the tour duration | 
					
						
							|  |  |  |  |     line=$(sed "s/\(^.*\):/\\${green}\1\\${restore}:/g" <<< "$line") | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     # Highlight date | 
					
						
							|  |  |  |  |     line=$(sed "s/${date}/\\${cyan}\0\\${restore}/g" <<< "$line") | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     echo -e "$line" | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | # Note that we use '"$@"' to let each command-line parameter expand to a | 
					
						
							|  |  |  |  | # separate word. The quotes around '$@' are essential! | 
					
						
							|  |  |  |  | # We need 'opts' as the 'eval set --' would nuke the return value of getopt | 
					
						
							|  |  |  |  | opts=$(getopt --options p: --longoptions player: -- "$@") | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | # Note the quotes around '$opts': they are essential! | 
					
						
							|  |  |  |  | eval set -- "$opts" | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | name="" | 
					
						
							| 
									
										
										
										
											2013-09-25 23:11:13 +03:00
										 |  |  |  | if [[ $1 == -p || $1 == --player ]]; then | 
					
						
							| 
									
										
										
										
											2013-09-21 02:20:19 +03:00
										 |  |  |  |     name=$2 | 
					
						
							| 
									
										
										
										
											2013-09-23 17:12:56 +03:00
										 |  |  |  |     shift 2 | 
					
						
							| 
									
										
										
										
											2013-09-21 02:20:19 +03:00
										 |  |  |  | fi | 
					
						
							| 
									
										
										
										
											2013-09-23 17:12:56 +03:00
										 |  |  |  | shift | 
					
						
							| 
									
										
										
										
											2013-09-21 02:20:19 +03:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-20 12:28:50 +03:00
										 |  |  |  | # Generate the sequence of tours to search in | 
					
						
							|  |  |  |  | if [[ -z "$@" ]]; then | 
					
						
							| 
									
										
										
										
											2013-10-20 14:08:46 +03:00
										 |  |  |  |     seq=$(seq -f "%02g" 1 99) | 
					
						
							| 
									
										
										
										
											2013-10-20 12:28:50 +03:00
										 |  |  |  | else | 
					
						
							|  |  |  |  |     # Change tour numbers: '1' -> '01', '2' -> '02', and so on | 
					
						
							| 
									
										
										
										
											2013-10-20 14:30:02 +03:00
										 |  |  |  |     seq=$(for i in "$@"; do printf "%02g " "$i"; done) | 
					
						
							| 
									
										
										
										
											2013-10-20 12:28:50 +03:00
										 |  |  |  | fi | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-20 14:30:02 +03:00
										 |  |  |  | for i in $seq; do | 
					
						
							| 
									
										
										
										
											2013-10-20 12:28:50 +03:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-02 17:31:08 +03:00
										 |  |  |  |     url="https://raw.github.com/${REPO}/master/${TOURNEY}/tours/${i}/tour_info" | 
					
						
							| 
									
										
										
										
											2013-10-20 14:30:02 +03:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     # Store 'tour_info' in an array of lines. | 
					
						
							|  |  |  |  |     # *NOTE* The incorporation of newline at the end of 'tour_info' | 
					
						
							|  |  |  |  |     # (--write-out '\n') is important and allows one to read the last | 
					
						
							|  |  |  |  |     # line without trailing '\n' | 
					
						
							| 
									
										
										
										
											2013-10-20 12:28:50 +03:00
										 |  |  |  |     lines=() | 
					
						
							|  |  |  |  |     while read line; do | 
					
						
							|  |  |  |  |         lines+=("$line") | 
					
						
							|  |  |  |  |     done <<< "$(curl --fail -q --silent --write-out '\n' $url)" | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     # Produce output only if 'tour_info' exists | 
					
						
							|  |  |  |  |     if [[ -n $lines ]]; then | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         # Decide to skip the tour or not | 
					
						
							|  |  |  |  |         unskip=""; player="" | 
					
						
							| 
									
										
										
										
											2013-10-20 14:30:02 +03:00
										 |  |  |  |         for ((j=5; j<${#lines[@]}; ++j)); do | 
					
						
							|  |  |  |  |             line="${lines[$j]}" | 
					
						
							| 
									
										
										
										
											2013-10-20 12:28:50 +03:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |             # Don't skip if an unfinished game exists | 
					
						
							|  |  |  |  |             if [[ -z $(grep "$result" <<< "$line") ]]; then | 
					
						
							|  |  |  |  |                 if [[ -z "$name" ]]; then | 
					
						
							|  |  |  |  |                     unskip="yes" | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                 # In addition, if the player was passed as an | 
					
						
							|  |  |  |  |                 # argument, check if this is his game or not | 
					
						
							| 
									
										
										
										
											2013-10-20 14:08:46 +03:00
										 |  |  |  |                 elif [[ -n $(grep -o "$name" <<< "$line") ]]; then | 
					
						
							| 
									
										
										
										
											2013-10-20 12:28:50 +03:00
										 |  |  |  |                     unskip="yes" | 
					
						
							|  |  |  |  |                     # Complete player's name | 
					
						
							|  |  |  |  |                     player=$(grep -o "[^ ]*${name}[^ ]*" <<< "$line") | 
					
						
							|  |  |  |  |                 fi | 
					
						
							|  |  |  |  |             fi | 
					
						
							|  |  |  |  |         done | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if [[ -n "$unskip" ]]; then | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             # The separator between tours | 
					
						
							|  |  |  |  |             echo -en "$yellow" | 
					
						
							| 
									
										
										
										
											2013-10-20 14:30:02 +03:00
										 |  |  |  |             for j in {1..41}; do echo -n "-"; done | 
					
						
							| 
									
										
										
										
											2013-10-20 12:28:50 +03:00
										 |  |  |  |             echo -e "$restore" | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             # Output the tour heading as is | 
					
						
							| 
									
										
										
										
											2013-10-20 14:30:02 +03:00
										 |  |  |  |             for ((j=0; j<5; ++j)); do highlight "${lines[$j]}"; done | 
					
						
							| 
									
										
										
										
											2013-10-20 12:28:50 +03:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |             # Lines with game info | 
					
						
							| 
									
										
										
										
											2013-10-20 14:30:02 +03:00
										 |  |  |  |             for ((j=5; j<${#lines[@]}; ++j)); do | 
					
						
							|  |  |  |  |                 line="${lines[$j]}" | 
					
						
							| 
									
										
										
										
											2013-10-20 12:28:50 +03:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |                 # Output only unfinished games | 
					
						
							|  |  |  |  |                 if [[ -z $(grep "$result" <<< "$line") ]]; then | 
					
						
							|  |  |  |  |                     if [[ -z "$player" ]]; then | 
					
						
							|  |  |  |  |                         highlight "$line" | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                     # If the player was passed as an argument, | 
					
						
							|  |  |  |  |                     # highlight his name | 
					
						
							|  |  |  |  |                     elif [[ -n $(grep -o "${player}" <<< "$line") ]]; then | 
					
						
							|  |  |  |  |                         line=$(sed "s/${player}/\\${red}\0\\${restore}/g" <<< "$line") | 
					
						
							|  |  |  |  |                         highlight "$line" | 
					
						
							|  |  |  |  |                     fi | 
					
						
							|  |  |  |  |                 fi | 
					
						
							|  |  |  |  |             done | 
					
						
							|  |  |  |  |        fi | 
					
						
							|  |  |  |  |     else | 
					
						
							|  |  |  |  |         # If the tour numbers were not passed as arguments, exit once | 
					
						
							|  |  |  |  |         # the first non-existing 'tour_info' is reached | 
					
						
							|  |  |  |  |         [[ -z "$@" ]] && break | 
					
						
							|  |  |  |  |     fi | 
					
						
							| 
									
										
										
										
											2013-09-21 02:20:19 +03:00
										 |  |  |  | done | 
					
						
							| 
									
										
										
										
											2013-09-25 23:11:13 +03:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | exit 0 |