scripts

Various scripts for UNIX-like systems
git clone https://git.tronto.net/scripts
Download | Log | Files | Refs | README

alg (10256B)


      1 #!/bin/sh
      2 
      3 # Script to store and access blindsolving algs (speedcubing stuff)
      4 
      5 # Example: alg e FG - opens up editor to edit $basedir/edges/$ebuffer/RD/LD
      6 # Example: alg c UBL E - prints out all first algs for UBL-FDR-*
      7 
      8 usage() {
      9 	echo "Usage:"
     10 	echo "alg"
     11 	echo "alg edit (c|e|w|x|t) [buffer] (1 letter|2 letters)"
     12 	echo "alg export (c|e|w|x|t|all)"
     13 }
     14 
     15 default_editor=vi
     16 basedir="$HOME/box/speedcubing/bld/algs"
     17 ebuffer=UR
     18 cbuffer=UFR
     19 wbuffer=UFr
     20 xbuffer=Ubl
     21 tbuffer=Uf
     22 
     23 edge() {
     24 	[ "$1" = "a" ] && echo FU
     25 	[ "$1" = "b" ] && echo UF
     26 	[ "$1" = "c" ] && echo UL
     27 	[ "$1" = "d" ] && echo UB
     28 	[ "$1" = "e" ] && echo FD
     29 	[ "$1" = "f" ] && echo RD
     30 	[ "$1" = "g" ] && echo LD
     31 	[ "$1" = "i" ] && echo FR
     32 	[ "$1" = "j" ] && echo LU
     33 	[ "$1" = "k" ] && echo RB
     34 	[ "$1" = "l" ] && echo DF
     35 	[ "$1" = "m" ] && echo DB
     36 	[ "$1" = "n" ] && echo DR
     37 	[ "$1" = "o" ] && echo FL
     38 	[ "$1" = "p" ] && echo DL
     39 	[ "$1" = "r" ] && echo BU
     40 	[ "$1" = "s" ] && echo BD
     41 	[ "$1" = "t" ] && echo BL
     42 	[ "$1" = "u" ] && echo BR
     43 	[ "$1" = "v" ] && echo LF
     44 	[ "$1" = "w" ] && echo LB
     45 	[ "$1" = "z" ] && echo RF
     46 }
     47 
     48 corner() {
     49 	[ "$1" = "a" ] && echo FUL
     50 	[ "$1" = "b" ] && echo UBL
     51 	[ "$1" = "c" ] && echo UFL
     52 	[ "$1" = "d" ] && echo UBR
     53 	[ "$1" = "e" ] && echo FDR
     54 	[ "$1" = "f" ] && echo RDB
     55 	[ "$1" = "g" ] && echo LDF
     56 	[ "$1" = "j" ] && echo LUF
     57 	[ "$1" = "k" ] && echo RUB
     58 	[ "$1" = "l" ] && echo DFR
     59 	[ "$1" = "m" ] && echo DBR
     60 	[ "$1" = "n" ] && echo DFL
     61 	[ "$1" = "o" ] && echo FDL
     62 	[ "$1" = "p" ] && echo DBL
     63 	[ "$1" = "r" ] && echo BUR
     64 	[ "$1" = "s" ] && echo BUL
     65 	[ "$1" = "t" ] && echo BDL
     66 	[ "$1" = "u" ] && echo BDR
     67 	[ "$1" = "v" ] && echo LUB
     68 	[ "$1" = "w" ] && echo LDB
     69 	[ "$1" = "z" ] && echo RDF
     70 }
     71 
     72 wing() {
     73 	[ "$1" = "a" ] && echo FUl
     74 	[ "$1" = "b" ] && echo URb
     75 	[ "$1" = "c" ] && echo ULf
     76 	[ "$1" = "d" ] && echo UBl
     77 	[ "$1" = "e" ] && echo FDr
     78 	[ "$1" = "f" ] && echo RDb
     79 	[ "$1" = "g" ] && echo LDf
     80 	[ "$1" = "i" ] && echo FRu
     81 	[ "$1" = "j" ] && echo LUb
     82 	[ "$1" = "k" ] && echo RBu
     83 	[ "$1" = "l" ] && echo DFl
     84 	[ "$1" = "m" ] && echo DBr
     85 	[ "$1" = "n" ] && echo DRf
     86 	[ "$1" = "o" ] && echo FLd
     87 	[ "$1" = "p" ] && echo DLb
     88 	[ "$1" = "r" ] && echo BUr
     89 	[ "$1" = "s" ] && echo BDl
     90 	[ "$1" = "t" ] && echo BLu
     91 	[ "$1" = "u" ] && echo BRd
     92 	[ "$1" = "v" ] && echo LFu
     93 	[ "$1" = "w" ] && echo LBd
     94 	[ "$1" = "x" ] && echo RUf
     95 	[ "$1" = "z" ] && echo RFd
     96 }
     97 
     98 xcenter() {
     99 	[ "$1" = "a" ] && echo Ful
    100 	[ "$1" = "b" ] && echo Ufr
    101 	[ "$1" = "c" ] && echo Ufl
    102 	[ "$1" = "d" ] && echo Ubr
    103 	[ "$1" = "e" ] && echo Fdr
    104 	[ "$1" = "f" ] && echo Rdb
    105 	[ "$1" = "g" ] && echo Ldf
    106 	[ "$1" = "i" ] && echo Fur
    107 	[ "$1" = "j" ] && echo Luf
    108 	[ "$1" = "k" ] && echo Rub
    109 	[ "$1" = "l" ] && echo Dfr
    110 	[ "$1" = "m" ] && echo Dbr
    111 	[ "$1" = "n" ] && echo Dfl
    112 	[ "$1" = "o" ] && echo Fdl
    113 	[ "$1" = "p" ] && echo Dbl
    114 	[ "$1" = "r" ] && echo Bur
    115 	[ "$1" = "s" ] && echo Bul
    116 	[ "$1" = "t" ] && echo Bdl
    117 	[ "$1" = "u" ] && echo Bdr
    118 	[ "$1" = "v" ] && echo Lub
    119 	[ "$1" = "w" ] && echo Ldb
    120 	[ "$1" = "x" ] && echo Ruf
    121 	[ "$1" = "z" ] && echo Rdf
    122 }
    123 
    124 tcenter() {
    125 	[ "$1" = "a" ] && echo Fu
    126 	[ "$1" = "b" ] && echo Ur
    127 	[ "$1" = "c" ] && echo Ul
    128 	[ "$1" = "d" ] && echo Ub
    129 	[ "$1" = "e" ] && echo Fd
    130 	[ "$1" = "f" ] && echo Rd
    131 	[ "$1" = "g" ] && echo Ld
    132 	[ "$1" = "i" ] && echo Fr
    133 	[ "$1" = "j" ] && echo Lu
    134 	[ "$1" = "k" ] && echo Rb
    135 	[ "$1" = "l" ] && echo Df
    136 	[ "$1" = "m" ] && echo Db
    137 	[ "$1" = "n" ] && echo Dr
    138 	[ "$1" = "o" ] && echo Fl
    139 	[ "$1" = "p" ] && echo Dl
    140 	[ "$1" = "r" ] && echo Bu
    141 	[ "$1" = "s" ] && echo Bd
    142 	[ "$1" = "t" ] && echo Bl
    143 	[ "$1" = "u" ] && echo Br
    144 	[ "$1" = "v" ] && echo Lf
    145 	[ "$1" = "w" ] && echo Lb
    146 	[ "$1" = "x" ] && echo Ru
    147 	[ "$1" = "z" ] && echo Rf
    148 }
    149 
    150 edit() {
    151 	mkdir -p "$basedir/$1/$2/$3"
    152 	$editor "$basedir/$1/$2/$3/$4"
    153 }
    154 
    155 showall_c() {
    156 	all_letters="a b c d e f g j k l m n o p r s t u v w z"
    157 	buffer="$1"
    158 	firstletter="$2"
    159 	firsttarget="$(corner $firstletter)"
    160 	for secondletter in $all_letters; do
    161 		secondtarget="$(corner $secondletter)"
    162 		f="$basedir/corners/$buffer/$firsttarget/$secondtarget"
    163 		[ -f "$f" ] || continue
    164 		line="$(head -n 1 "$f")"
    165 		printf '%s   %s\n' "$firstletter$secondletter" "$line"
    166 	done
    167 }
    168 
    169 showall_e() {
    170 	all_letters="a b c d e f g i j k l m n o p r s t u v w z"
    171 	buffer="$1"
    172 	firstletter="$2"
    173 	firsttarget="$(edge $firstletter)"
    174 	for secondletter in $all_letters; do
    175 		secondtarget="$(edge $secondletter)"
    176 		f="$basedir/edges/$buffer/$firsttarget/$secondtarget"
    177 		[ -f "$f" ] || continue
    178 		line="$(head -n 1 "$f")"
    179 		printf '%s   %s\n' "$firstletter$secondletter" "$line"
    180 	done
    181 }
    182 
    183 showall_w() {
    184 	all_letters="a b c d e f g i j k l m n o p r s t u v w x z"
    185 	buffer="$1"
    186 	firstletter="$2"
    187 	firsttarget="$(wing $firstletter)"
    188 	for secondletter in $all_letters; do
    189 		secondtarget="$(wing $secondletter)"
    190 		f="$basedir/wings/$buffer/$firsttarget/$secondtarget"
    191 		[ -f "$f" ] || continue
    192 		line="$(head -n 1 "$f")"
    193 		printf '%s   %s\n' "$firstletter$secondletter" "$line"
    194 	done
    195 }
    196 
    197 showall_x() {
    198 	all_letters="a b c d e f g i j k l m n o p r s t u v w x z"
    199 	buffer="$1"
    200 	firstletter="$2"
    201 	firsttarget="$(xcenter $firstletter)"
    202 	for secondletter in $all_letters; do
    203 		secondtarget="$(xcenter $secondletter)"
    204 		f="$basedir/xcenters/$buffer/$firsttarget/$secondtarget"
    205 		[ -f "$f" ] || continue
    206 		line="$(head -n 1 "$f")"
    207 		printf '%s   %s\n' "$firstletter$secondletter" "$line"
    208 	done
    209 }
    210 
    211 showall_t() {
    212 	all_letters="a b c d e f g i j k l m n o p r s t u v w x z"
    213 	buffer="$1"
    214 	firstletter="$2"
    215 	firsttarget="$(tcenter $firstletter)"
    216 	for secondletter in $all_letters; do
    217 		secondtarget="$(tcenter $secondletter)"
    218 		f="$basedir/tcenters/$buffer/$firsttarget/$secondtarget"
    219 		[ -f "$f" ] || continue
    220 		line="$(head -n 1 "$f")"
    221 		printf '%s   %s\n' "$firstletter$secondletter" "$line"
    222 	done
    223 }
    224 
    225 export_csv_c() {
    226 	sorted_letters="d b c l n p m k z f j v w g a o e s r u t"
    227 	buffer="$cbuffer"
    228 	f="$basedir/corners/$buffer"
    229 	printf '"",'
    230 	for first in $sorted_letters; do
    231 		printf '"%s",' "$(corner $first)"
    232 	done
    233 	printf '\n'
    234 	for first in $sorted_letters; do
    235 		firstpiece="$(corner $first)"
    236 		printf '"%s",' "$firstpiece"
    237 		for second in $sorted_letters; do
    238 			g="$f/$firstpiece/$(corner $second)"
    239 			if [ "$first" = "$second" ] || [ ! -f "$g" ]; then
    240 				printf '"",'
    241 			else
    242 				printf '"%s",' "$(head -n 1 "$g")"
    243 			fi
    244 		done
    245 		printf '\n'
    246 	done
    247 }
    248 
    249 export_csv_e() {
    250 	sorted_letters="d c b n l p m k z f v j w g i a o e t r u s"
    251 	buffer="$ebuffer"
    252 	f="$basedir/edges/$buffer"
    253 	printf '"",'
    254 	for first in $sorted_letters; do
    255 		printf '"%s",' "$(edge $first)"
    256 	done
    257 	printf '\n'
    258 	for first in $sorted_letters; do
    259 		firstpiece="$(edge $first)"
    260 		printf '"%s",' "$firstpiece"
    261 		for second in $sorted_letters; do
    262 			g="$f/$firstpiece/$(edge $second)"
    263 			if [ "$first" = "$second" ] || [ ! -f "$g" ]; then
    264 				printf '"",'
    265 			else
    266 				printf '"%s",' "$(head -n 1 "$g")"
    267 			fi
    268 		done
    269 		printf '\n'
    270 	done
    271 }
    272 
    273 export_csv_w() {
    274 	sorted_letters="b d c n l p m k x z f v j w g i a o e t r u s"
    275 	buffer="$wbuffer"
    276 	f="$basedir/wings/$buffer"
    277 	printf '"",'
    278 	for first in $sorted_letters; do
    279 		printf '"%s",' "$(wing $first)"
    280 	done
    281 	printf '\n'
    282 	for first in $sorted_letters; do
    283 		firstpiece="$(wing $first)"
    284 		printf '"%s",' "$firstpiece"
    285 		for second in $sorted_letters; do
    286 			g="$f/$firstpiece/$(wing $second)"
    287 			if [ "$first" = "$second" ] || [ ! -f "$g" ]; then
    288 				printf '"",'
    289 			else
    290 				printf '"%s",' "$(head -n 1 "$g")"
    291 			fi
    292 		done
    293 		printf '\n'
    294 	done
    295 }
    296 
    297 export_csv_x() {
    298 	sorted_letters="d c b l n p m k x z f j v w g i a o e s r u t"
    299 	buffer="$xbuffer"
    300 	f="$basedir/xcenters/$buffer"
    301 	printf '"",'
    302 	for first in $sorted_letters; do
    303 		printf '"%s",' "$(xcenter $first)"
    304 	done
    305 	printf '\n'
    306 	for first in $sorted_letters; do
    307 		firstpiece="$(xcenter $first)"
    308 		printf '"%s",' "$firstpiece"
    309 		for second in $sorted_letters; do
    310 			g="$f/$firstpiece/$(xcenter $second)"
    311 			if [ "$first" = "$second" ] || [ ! -f "$g" ]; then
    312 				printf '"",'
    313 			else
    314 				printf '"%s",' "$(head -n 1 "$g")"
    315 			fi
    316 		done
    317 		printf '\n'
    318 	done
    319 }
    320 
    321 export_csv_t() {
    322 	sorted_letters="d c b l n p m k x z f j v w g i a o e s r u t"
    323 	buffer="$tbuffer"
    324 	f="$basedir/tcenters/$buffer"
    325 	printf '"",'
    326 	for first in $sorted_letters; do
    327 		printf '"%s",' "$(tcenter $first)"
    328 	done
    329 	printf '\n'
    330 	for first in $sorted_letters; do
    331 		firstpiece="$(tcenter $first)"
    332 		printf '"%s",' "$firstpiece"
    333 		for second in $sorted_letters; do
    334 			g="$f/$firstpiece/$(tcenter $second)"
    335 			if [ "$first" = "$second" ] || [ ! -f "$g" ]; then
    336 				printf '"",'
    337 			else
    338 				printf '"%s",' "$(head -n 1 "$g")"
    339 			fi
    340 		done
    341 		printf '\n'
    342 	done
    343 }
    344 
    345 run() {
    346 	case "$1" in
    347 	edit)
    348 		editor="$default_editor"
    349 		shift
    350 		;;
    351 	export)
    352 		if [ "$2" = "all" ]; then
    353 			mkdir -p bldsheets
    354 			export_csv_c > bldsheets/corners.csv
    355 			export_csv_e > bldsheets/edges.csv
    356 			export_csv_w > bldsheets/wings.csv
    357 			export_csv_x > bldsheets/xcenters.csv
    358 			export_csv_t > bldsheets/tcenters.csv
    359 			echo "Sheets exported to bldsheets/"
    360 			return
    361 		fi
    362 		#TODO add option for alternative buffers
    363 		[ "$2" != "c" ] && [ "$2" != "e" ] && \
    364 		[ "$2" != "w" ] && [ "$2" != "x" ] && \
    365 		[ "$2" != "t" ] && usage && return
    366 		export_csv_$2
    367 		return
    368 		;;
    369 	*)
    370 		editor="cat"
    371 		;;
    372 	esac
    373 
    374 	type="$1"
    375 	if [ -z "$3" ]; then
    376 		letters="$2"
    377 		cbuf="$cbuffer"
    378 		ebuf="$ebuffer"
    379 		wbuf="$wbuffer"
    380 		xbuf="$xbuffer"
    381 		tbuf="$tbuffer"
    382 	else
    383 		letters="$3"
    384 		cbuf="$2"
    385 		ebuf="$2"
    386 		wbuf="$2"
    387 		xbuf="$2"
    388 		tbuf="$2"
    389 	fi
    390 
    391 	letter1="$(echo "$letters" | cut -c 1)"
    392 	letter2="$(echo "$letters" | cut -b 2)"
    393 
    394 	[ -z "$letter1" ] && usage && return
    395 
    396 	case "$type" in
    397 	c)
    398 		if [ -n "$letter2" ]; then
    399 			edit corners $cbuf $(corner $letter1) $(corner $letter2)
    400 		else
    401 			showall_c $cbuf $letter1
    402 		fi
    403 		;;
    404 	e)
    405 		if [ -n "$letter2" ]; then
    406 			edit edges $ebuf $(edge $letter1) $(edge $letter2)
    407 		else
    408 			showall_e $ebuf $letter1
    409 		fi
    410 		;;
    411 	w)
    412 		if [ -n "$letter2" ]; then
    413 			edit wings $wbuf $(wing $letter1) $(wing $letter2)
    414 		else
    415 			showall_w $wbuf $letter1
    416 		fi
    417 		;;
    418 	x)
    419 		if [ -n "$letter2" ]; then
    420 			edit xcenters $xbuf $(xcenter $letter1) $(xcenter $letter2)
    421 		else
    422 			showall_x $xbuf $letter1
    423 		fi
    424 		;;
    425 	t)
    426 		if [ -n "$letter2" ]; then
    427 			edit tcenters $tbuf $(tcenter $letter1) $(tcenter $letter2)
    428 		else
    429 			showall_t $tbuf $letter1
    430 		fi
    431 		;;
    432 	*)
    433 		usage
    434 		;;
    435 	esac
    436 }
    437 
    438 if [ -n "$1" ]; then
    439 	run $@
    440 else
    441 	while read -r args; do
    442 		[ -n "$args" ] && run $args
    443 	done
    444 fi