Refactor and tidy BOSL's shell scripts

* Fixed ShellCheck.net warnings, including
  * exit -1: https://github.com/koalaman/shellcheck/wiki/SC2242
  * cd || exit: https://github.com/koalaman/shellcheck/wiki/SC2164
  * "$foo": https://github.com/koalaman/shellcheck/wiki/SC2086
  * $(( $foo )): https://github.com/koalaman/shellcheck/wiki/SC2004
  * grep | wc -l: https://github.com/koalaman/shellcheck/wiki/SC2126
* Use arrays instead of unquoted strings for loops and command arguments
* Use (( ... )) arithmetic context instead of string comparisons
* Use bash's built-in regex support instead of grep+sed+awk
* Write errors to stderr
This commit is contained in:
Michael Diamond
2021-05-21 00:46:17 -07:00
parent 57b1d0412c
commit 0666f6482c
5 changed files with 58 additions and 69 deletions

View File

@@ -1,40 +1,38 @@
#!/bin/bash
FORCED=""
FILES=""
DISPMD=""
DISPMD=0
GEN_ARGS=()
FILES=()
for opt in "$@" ; do
case $opt in
-f ) FORCED=$opt ;;
-d ) DISPMD=$opt ;;
-* ) echo "Unknown option $opt"; exit -1 ;;
* ) FILES="$FILES $opt" ;;
case "$opt" in
-f ) GEN_ARGS+=(-f) ;;
-d ) DISPMD=1 ;;
-* ) echo "Unknown option $opt" >&2; exit 1 ;;
* ) FILES+=("$opt") ;;
esac
done
if [[ "$FILES" != "" ]]; then
PREVIEW_LIBS="$FILES"
else
PREVIEW_LIBS="Shapes2d Shapes3d Transforms Distributors Mutators Attachments Paths FractalTree"
if (( ${#FILES[@]} == 0 )); then
FILES=(Shapes2d Shapes3d Transforms Distributors Mutators Attachments Paths FractalTree)
fi
dir="$(basename $PWD)"
if [ "$dir" = "BOSL2" ]; then
cd BOSL2.wiki
elif [ "$dir" != "BOSL2.wiki" ]; then
echo "Must run this script from the BOSL2 or BOSL2/BOSL2.wiki directories."
# Try to cd to the BOSL2.wiki directory if run from the BOSL2 root
if [[ "$(basename "$PWD")" != "BOSL2.wiki" ]]; then
if ! cd BOSL2.wiki; then
echo "BOSL2.wiki directory not found, try running from the BOSL2 or BOSL2/BOSL2.wiki directory" >&2
exit 1
fi
fi
rm -f tmp_*.scad
for base in $PREVIEW_LIBS; do
base="$(basename $base .md)"
for base in "${FILES[@]}"; do
base="$(basename "$base" .md)"
mkdir -p images/tutorials
rm -f images/tutorials/${base}_*.png images/tutorials/${base}_*.gif
echo "$base.md"
../scripts/tutorial_gen.py ../tutorials/$base.md -o Tutorial-$base.md $FORCED -I images/tutorials/ || exit 1
if [ "$DISPMD" != "" ]; then
open -a Typora Tutorial-$base.md
rm -f "images/tutorials/${base}"_*.png "images/tutorials/${base}"_*.gif
echo "${base}.md"
../scripts/tutorial_gen.py "../tutorials/${base}.md" -o "Tutorial-${base}.md" "${GEN_ARGS[@]}" -I images/tutorials/ || exit 1
if (( DISPMD )); then
open -a Typora "Tutorial-${base}.md"
fi
done