From 2fa00047ccebcc1740b17b09f8c5dc394f3d0dc7 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Wed, 8 Jul 2020 20:24:57 +0100
Subject: [PATCH] url-diff: commpact the settings definitions

---
 url-diff.sh | 45 ++++++++++++++++++++++++++++++---------------
 1 file changed, 30 insertions(+), 15 deletions(-)

diff --git a/url-diff.sh b/url-diff.sh
index a36d344..1b9ac1e 100755
--- a/url-diff.sh
+++ b/url-diff.sh
@@ -6,16 +6,21 @@ if [[ ! -z "${URLDIFF_STORAGE_DIR}" ]]; then loc_storage="${URLDIFF_STORAGE_DIR}
 loc_firefox_profile="${loc_storage}/firefox-profile";
 loc_prev_images="${loc_storage}/previous-images";
 
-highlight_colour="#ff000044";
-if [[ ! -z "${URLDIFF_HIGHLIGHT_COLOUR}" ]]; then highlight_colour="${URLDIFF_HIGHLIGHT_COLOUR}"; fi
+highlight_colour="${URLDIFF_HIGHLIGHT_COLOUR:-#ff000044}";
 
-quiet="false";
-if [[ ! -z "${URLDIFF_QUIET}" ]]; then quiet="${URLDIFF_QUIET}"; fi
+quiet="${URLDIFF_QUIET:-false}";
 
 window_width="${URLDIFF_WINDOW_WIDTH}";
 
-change_threshold="2";
-if [[ ! -z "${URLDIFF_CHANGE_THRESHOLD}" ]]; then change_threshold="${URLDIFF_CHANGE_THRESHOLD}"; fi
+change_threshold="${URLDIFF_CHANGE_THRESHOLD:-2}";
+
+###############################################################################
+
+if [[ -z "$(type -p calc)" ]]; then
+	echo "calc command not detected - it would appear it is not installed" >&2;
+	echo "Install it like this on debian systems:" >&2;
+	echo "	sudo apt install apcalc" >&2;
+fi
 
 ###############################################################################
 
@@ -41,7 +46,9 @@ calculate_percentage_colour() {
 	image_height="$(identify -format "%[fx:h]" "${loc_image}")";
 	pixel_count_colour="$(convert "${loc_image}" -fill black +opaque "${colour}" -fill white -opaque "${colour}" -format "%[fx:w*h*mean]" info:)";
 	
-	echo "(${pixel_count_colour} / (${image_width}*${image_height})) * 100" | bc -l
+	# echo "image_width: ${image_width}, image_height: ${image_height}, pixel_count_colour: ${pixel_count_colour}" >&2;
+	
+	calc -p "(${pixel_count_colour} / (${image_width}*${image_height})) * 100" | tr -d '~';
 }
 
 ###############################################################################
@@ -58,17 +65,19 @@ Usage:
 
 Subcommands:
 	check {url} {output_diff_image} [{output_apng}]
-		Checks the given URL for differences, and saves a diff image to a
-		specified location. Exits with code 0 if differences are found, or code
-		1 if no differences are found.
-		Optionally generates an animated PNG that alternates between the 2 screenshots.
+		Checks the given URL for differences, and saves a diff
+		image to a specified location. Exits with code 0 if
+		differences are found, or code 1 if no differences are
+		found.
+		Optionally generates an animated PNG that alternates
+		between the 2 screenshots.
 
 Environment Variables:
 	Variable                    Default variable
 	----------------------------------------------------------------------
 	URLDIFF_STORAGE_DIR         ~/.config/url-diff
-		The directory in which previous images are stored to diff against.
-		The firefox profile is also stored in here.
+		The directory in which previous images are stored to diff
+		against. The firefox profile is also stored in here.
 	
 	URLDIFF_HIGHLIGHT_COLOUR    #ffffffcc
 		The colour to highlight any changes in.
@@ -77,7 +86,8 @@ Environment Variables:
 		The window width to tell firefox to screenshot with.
 	
 	URLDIFF_CHANGE_THRESHOLD	2
-		The percentage of the screenshot that should have changed for it to count.
+		The percentage of the screenshot that should have
+		changed for it to count.
 		Allows for ignoring random minor rendering artifacts.
 " >&2;
 fi
@@ -129,10 +139,15 @@ case "${mode}" in
 		# Work out how much of the image has changed
 		percentage_changed="$(calculate_percentage_colour "${tmpdir}/transp.png" "${highlight_colour}")";
 		
+		if [[ -z "${percentage_changed}" ]]; then
+			echo "Something went wrong when calculating the percentage changed - no value was returned";
+			exit 1;
+		fi
+		
 		log_msg "Changed: ${percentage_changed}%";
 		
 		# if [[ "${img_hash_new}" == "${img_hash_old}" ]]; then
-		if [[ "$(echo "${percentage_changed} > ${change_threshold}" | bc -l)" -eq 0 ]]; then
+		if [[ "$(calc -p "${percentage_changed} > ${change_threshold}")" -eq 0 ]]; then
 			log_msg "No changes detected.";
 			exit 1;
 		fi
