the_earnest/dotfiles

#!/bin/bash

function downscale() {

	total_before=0
	total_after=0
	total_savings=0

	while read f
	do
		before=$(stat -c "%s" "$f")
		(( total_before+=before ))

		magick mogrify -define preserve-timestamp=true -scale 50% "$f"

		after=$(stat -c "%s" "$f")
		(( total_after+=after ))
		(( savings=before-after ))
		(( total_savings+=savings ))

		percent=$(echo "scale=2;100*$savings/$before" | bc)
		#printf "%s before:%d after:%d saved:%d (%.0f%%)\n" "$f" "$before" "$after" "$savings" "$percent"
		printf "%s - saved %.0f%%\n" "$f" "$percent"

	done

	# report total savings
	percent=$(echo "scale=2;100*$total_savings/$total_before" | bc)
	printf "\ntotal size reduction: from %d to %d (%.0f%%), for %d bytes\n" $total_before $total_after $percent $total_savings
}

INPUTFILE=large_jpegs.txt
if [[ ! -e "$INPUTFILE" ]]
then
	echo "error: input file \"$INPUTFILE\" not found in this directory"
	exit
fi

cat $INPUTFILE | downscale

if [[ $? -eq 0 ]]
then
	echo removing $INPUTFILE
	rm $INPUTFILE
else
	echo "function did not return zero; presumable error; not removing $INPUTFILE"
fi