From 8146dbc12b8ac3cce6883e16375460e98b432c8d Mon Sep 17 00:00:00 2001
From: wkwk <12408-wkwk@users.norepy.gitgud.io>
Date: Wed, 8 Apr 2020 03:22:53 -0500
Subject: [PATCH] Do a shellcheck / shfmt run on shellscripts

Fix SC2145: Argument mixes string and array. Use * or separate argument.
Fix SC2162: read without -r will mangle backslashes.
Fix SC2086: Double quote to prevent globbing and word splitting.
Fix SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.
---
 compile.sh     | 110 ++++++++++++++++++++++++-------------------------
 sanityCheck.sh |  27 ++++++------
 2 files changed, 68 insertions(+), 69 deletions(-)

diff --git a/compile.sh b/compile.sh
index 53265b8640a..ef62d7c3edc 100755
--- a/compile.sh
+++ b/compile.sh
@@ -3,8 +3,8 @@
 output=/dev/stdout
 
 # displays help text
-function displayHelp {
-  cat << HelpText
+function displayHelp() {
+	cat <<HelpText
 Usage: compile.sh [OPTION]...
 
 Options:
@@ -17,17 +17,17 @@ HelpText
 }
 
 #display an error message
-function echoError {
-	echo -e "\033[0;31m$@\033[0m"
+function echoError() {
+	echo -e "\033[0;31m$*\033[0m"
 }
 
 #display message
-function echoMessage {
-	echo "$1" > "${output}"
+function echoMessage() {
+	echo "$1" >"${output}"
 }
 
 #compile the HTML file
-function compile {
+function compile() {
 	mkdir -p bin/resources
 	export TWEEGO_PATH=devTools/tweeGo/storyFormats
 	TWEEGO_EXE="tweego"
@@ -36,30 +36,31 @@ function compile {
 		echoMessage "system tweego binary"
 	else
 		case "$(uname -m)" in
-		x86_64|amd64)
-			echoMessage "x64 arch"
-			if [ "$(uname -s)" = "Darwin" ]; then
-				TWEEGO_EXE="./devTools/tweeGo/tweego_osx64"
-			elif [ $OSTYPE = "msys" ]; then
-				TWEEGO_EXE="./devTools/tweeGo/tweego_win64"
-			else
-				TWEEGO_EXE="./devTools/tweeGo/tweego_nix64"
-			fi
-			;;
-		x86|i[3-6]86)
-			echoMessage "x86 arch"
-			if [ "$(uname -s)" = "Darwin" ]; then
-				TWEEGO_EXE="./devTools/tweeGo/tweego_osx86"
-			elif [ $OSTYPE = "msys" ]; then
-				TWEEGO_EXE="./devTools/tweeGo/tweego_win86"
-			else
-				TWEEGO_EXE="./devTools/tweeGo/tweego_nix86"
-			fi
-			;;
-		*)
-			echoError "No system tweego binary found, and no precompiled binary for your platform available."
-			echoError "Please compile tweego and put the executable in PATH."
-			exit 2
+			x86_64 | amd64)
+				echoMessage "x64 arch"
+				if [ "$(uname -s)" = "Darwin" ]; then
+					TWEEGO_EXE="./devTools/tweeGo/tweego_osx64"
+				elif [ "$OSTYPE" = "msys" ]; then
+					TWEEGO_EXE="./devTools/tweeGo/tweego_win64"
+				else
+					TWEEGO_EXE="./devTools/tweeGo/tweego_nix64"
+				fi
+				;;
+			x86 | i[3-6]86)
+				echoMessage "x86 arch"
+				if [ "$(uname -s)" = "Darwin" ]; then
+					TWEEGO_EXE="./devTools/tweeGo/tweego_osx86"
+				elif [ "$OSTYPE" = "msys" ]; then
+					TWEEGO_EXE="./devTools/tweeGo/tweego_win86"
+				else
+					TWEEGO_EXE="./devTools/tweeGo/tweego_nix86"
+				fi
+				;;
+			*)
+				echoError "No system tweego binary found, and no precompiled binary for your platform available."
+				echoError "Please compile tweego and put the executable in PATH."
+				exit 2
+				;;
 		esac
 	fi
 
@@ -78,8 +79,7 @@ function compile {
 	devTools/concatFiles.sh js/ '*.js' bin/fc.js
 	$TWEEGO_EXE -o $file --module=bin/fc.js --head devTools/head.html src/ || build_failed="true"
 	rm -f bin/fc.js
-	if [ "$build_failed" = "true" ]
-	then
+	if [ "$build_failed" = "true" ]; then
 		echoError "Build failed."
 		exit 1
 	fi
@@ -97,29 +97,29 @@ if [[ "$1" == "" ]]; then
 	echoMessage "For more options see compile.sh -h."
 else
 	#parse options
-	while [[ "$1" ]]
-	do
+	while [[ "$1" ]]; do
 		case $1 in
-		-d|--dry)
-			dry="true"
-		;;
-		-g|--git)
-			usehash="true"
-		;;
-		-h|--help)
-			displayHelp
-			exit 0
-		;;
-		-s|--sanity)
-			sanity="true"
-		;;
-		-q|--quiet)
-			output=/dev/null
-		;;
-		*)
-			echoError "Unknown argument $1."
-			displayHelp
-			exit 1
+			-d | --dry)
+				dry="true"
+				;;
+			-g | --git)
+				usehash="true"
+				;;
+			-h | --help)
+				displayHelp
+				exit 0
+				;;
+			-s | --sanity)
+				sanity="true"
+				;;
+			-q | --quiet)
+				output=/dev/null
+				;;
+			*)
+				echoError "Unknown argument $1."
+				displayHelp
+				exit 1
+				;;
 		esac
 		shift
 	done
diff --git a/sanityCheck.sh b/sanityCheck.sh
index 268742e99c4..500558d0cfb 100755
--- a/sanityCheck.sh
+++ b/sanityCheck.sh
@@ -8,7 +8,7 @@ fi
 WARNING='\033[93m'
 
 myprint() {
-	while read data; do
+	while read -r data; do
 		echo -n -e "[$1]$WARNING"
 		echo "$data"
 	done
@@ -25,17 +25,17 @@ $GREP "<<[^\"<>]*\"[^\"<>]*>>" -- 'src/*' | myprint "MissingSpeechMark"
 # Check for missing ".  e.g.:   <<if $foo = "hello)
 $GREP -e "<<[^\"<>]*\([^\"<>]*\"[^><\"]*\"\| [<>] \)*\"\([^\"<>]*\"[^><\"]*\"\| [<>] \)*\([^\"<>]\| [<>] \)*>>" --and --not -e "*[^']*" -- 'src/*' | myprint "MissingSpeechMark2"
 # Check for colors like: @@color:red   - should be @@.red
-$GREP -e "@@color:" --and --not -e  "@@color:rgb([0-9 ]\+,[0-9 ]\+,[0-9 ]\+)" -- "src/*" | myprint "UseCSSColors"
+$GREP -e "@@color:" --and --not -e "@@color:rgb([0-9 ]\+,[0-9 ]\+,[0-9 ]\+)" -- "src/*" | myprint "UseCSSColors"
 # Check for missing $ in activeSlave or PC
-$GREP "<<[ ]*[^\$><_\[]*\(activeSlave\|PC\)[.]"  -- "src/*" | myprint "MissingDollar"
+$GREP "<<[ ]*[^\$><_\[]*\(activeSlave\|PC\)[.]" -- "src/*" | myprint "MissingDollar"
 # Check for closing bracket without opening bracket.  e.g.:  <<if foo)>>	  (but  <<case "foo")>>   is valid, so ignore those
-$GREP -e "<<[ a-zA-Z]\+\([^()<>]\|[^()<>][<>][^()<>]\)*)" --and --not -e "<< *case"  -- "src/*" | myprint "MissingOpeningBracket"
+$GREP -e "<<[ a-zA-Z]\+\([^()<>]\|[^()<>][<>][^()<>]\)*)" --and --not -e "<< *case" -- "src/*" | myprint "MissingOpeningBracket"
 # Check for opening bracket without closing bracket.  e.g.:  <<if (foo>>
 $GREP -e "<<[ a-zA-Z]\([^<>]\|[^<>][<>][^<>]\)\+(\([^()<>]\|[^<>()][<>][^<>()]\|([^<>()]*])\)*>>" -- "src/*" | myprint "MissingClosingBracket"
 # Check for two closing brackets but one opening bracket.  e.g.:  <<if (foo))>>
-$GREP -e "<<[ a-zA-Z]\+[^()<>]*([^()]*)[^()]*)[^()<>]*>>"  -- "src/*" | myprint "MissingOpeningBracket2"
+$GREP -e "<<[ a-zA-Z]\+[^()<>]*([^()]*)[^()]*)[^()<>]*>>" -- "src/*" | myprint "MissingOpeningBracket2"
 # Check for one closing bracket but two opening brackets.  e.g.:  <<if ((foo)>>
-$GREP -e "<<[ a-zA-Z]\+[^()<>]*([^()]*([^()]*)[^()<>]*>>"  -- "src/*" | myprint "MissingClosingBracket2"
+$GREP -e "<<[ a-zA-Z]\+[^()<>]*([^()]*([^()]*)[^()<>]*>>" -- "src/*" | myprint "MissingClosingBracket2"
 $GREP -e "<<.*[(][^<>)]*[(][^<>)]*)\?[^<>)]*>>" -- "src/*" | myprint "MissingClosingBracket3"
 # Check for too many >>>.  e.g.: <</if>>>
 $GREP "<<[^<>]*[<>]\?[^<>]*>>>" -- "src/*.tw" | myprint "TooManyAngleBrackets"
@@ -50,7 +50,7 @@ $GREP "<<else >\?[^>]" -- 'src/*' | myprint "ShouldBeElseIf"
 # Check, e.g., =to
 $GREP "=to" -- 'src/*' | myprint "EqualAndTo"
 # Check doing  $slaves.foo instead of $slaves[i].foo
-$GREP -e "[$]slaves[.]"  --and --not -e '[$]slaves[.]\(length\|random\|map\|filter\|deleteAt\|push\|find\|includes\|delete\|forEach\)' -- 'src/*' | myprint "MissingSlavesIndex"
+$GREP -e "[$]slaves[.]" --and --not -e '[$]slaves[.]\(length\|random\|map\|filter\|deleteAt\|push\|find\|includes\|delete\|forEach\)' -- 'src/*' | myprint "MissingSlavesIndex"
 # Try to check for accidentally mixing slaves[] and activeSlave.  This can have a lot of false matches, but has caught a lot of bugs so it's worth the pain
 $GREP -e "activeSlave[.]" --and -e "slaves\[..\?\][.]" --and --not -e '[.]ID' --and --not -e 'slaves\[..\?\][.]\(slaveName\|slaveSurname\|actualAge\|relation\|assignment\|age\|devotion\|trust\|vagina\|mother\|father\|training\)' -- 'src/*' | myprint "MaybeAccidentalMixingOfSlavesAndActiveSlave"
 # Check, e.g.  <<set foo == 4>>
@@ -62,7 +62,7 @@ $GREP -e "<<[a-zA-Z]\([^>\"]\|[^>]>[^>]\|\"[^\"]*\"\)* [a-zA-Z]\+ * =" -- src/*.
 # Check for missing command, e.g.  <<foo =
 $GREP -e "<<[a-zA-Z]* = *" -- src/*.tw | myprint "BadCommand"
 # Check for duplicate words, e.g. with with
-$GREP -e  " \(\b[a-zA-Z][a-zA-Z]\+\) \1\b " --and --not -e " her her " --and --not -e " you you " --and --not -e " New New " --and --not -e "Slave Slave " --and --not -e " that that " --and --not -e " in in " --and --not -e " is is " -- 'src/*' | myprint "Duplicate words"
+$GREP -e " \(\b[a-zA-Z][a-zA-Z]\+\) \1\b " --and --not -e " her her " --and --not -e " you you " --and --not -e " New New " --and --not -e "Slave Slave " --and --not -e " that that " --and --not -e " in in " --and --not -e " is is " -- 'src/*' | myprint "Duplicate words"
 # Check for obsolete SugarCube macros
 $GREP -E "<<display |<<click|<<.*\.contains" -- src/*.tw | myprint "ObsoleteMacro"
 # Check for double articles
@@ -73,9 +73,9 @@ $GREP -i -E "\Wan (b|c|d|f|g|j|k|l|m|n|p|q|r|s|t|v|w|x|y|z)\w." -- src/*.tw | gr
 # Check for $ sign mid-word
 $GREP -i "\w$\w" -- src/*.tw | myprint "VarSignMidWord"
 # check for $ sign at beginning of macro
-$GREP '<<\s*\$' -- 'src/*'  | myprint "VarSignAtMacroStart"
+$GREP '<<\s*\$' -- 'src/*' | myprint "VarSignAtMacroStart"
 # check our custom option macro is either options,option,optionlt,optionlte,optiongt,optiongte,optiondefault,optionstyle,optionif
-$GREP -e '<<option' --and --not -e "<<option\(\|s\|lt\|lte\|gt\|gte\|default\|style\|if\)[ >]" -- 'src/*'  | myprint "OptionUnrecognized"
+$GREP -e '<<option' --and --not -e "<<option\(\|s\|lt\|lte\|gt\|gte\|default\|style\|if\)[ >]" -- 'src/*' | myprint "OptionUnrecognized"
 # check our custom option macro is: <<option variable "somestring"
 $GREP -e "<<option[lg]te\? " --and --not -e "<<option[lg]te\?\s\+-\?[0-9]\+\s\+-\?[0-9]\+\s\+[\'\"].*[\'\"]" -- 'src/*' | grep -v src/js | myprint "OptionBadArguments1"
 $GREP -e "<<optiondefault " --and --not -e "<<optiondefault\s\+\(-\?[0-9]\+\|[\'\"].*[\'\"]\|false\|true\)\s\+[\'\"].*[\'\"]" -- 'src/*' | grep -v src/js | myprint "OptionBadArguments2"
@@ -83,8 +83,8 @@ $GREP -e "<<option\([lg]t\?\|default\) *>" -- 'src/*' | grep -v src/js | myprint
 #$GREP -e "<<option " --and --not -e "<<option\s\+\(-\?[0-9]\+\|[\'\"].*[\'\"]\|false\|true\)\s\+[\`\'\"].*[\'\"\`]" -- 'src/*' | grep -v src/js | myprint "OptionBadArguments4" # too many false positives
 $GREP -e "<<if def [^(>]*[&|]" -- 'src/*' | myprint "AddBracketsAroundDef2"
 # check for missing ; before statement
-$GREP 'if $ ' -- 'src/*'  | myprint "Missing ; before statement"
-$GREP 'elseif $ ' -- 'src/*'  | myprint "Missing ; before statement"
+$GREP 'if $ ' -- 'src/*' | myprint "Missing ; before statement"
+$GREP 'elseif $ ' -- 'src/*' | myprint "Missing ; before statement"
 # Check for an unrecognized letter before >>
 $GREP "[^]a-zA-Z0-9 \")}'+-\*\`] *>>" -- 'src/*' | myprint "StrangeCharacterAtEndOfCommand"
 # Check for a . inside a <<>>
@@ -110,10 +110,9 @@ $GREP -i "non.lethal" -- 'src/*' | myprint "ShouldBeNonlethal"
 #$GREP "span class=[^\"]" -- src/*.js --exclude 'src/001-lib/Jquery/Jquery.js' | myprint "MissingQuotes" disabled until I can figure out how to exclude files
 
 (
-cd src/
+	cd src/ || exit
 	$GREP "\$\(PC\|activeSlave\|slaves\|tanks\)[.][^a-zA-Z]" | myprint "UnexpectedCharAfterDot"
 )
 
-
 # Check that all the tags are properly opened and closed & a lot of other stuff
 java -jar devTools/javaSanityCheck/SanityCheck.jar
-- 
GitLab