#!/bin/bash

# converts uncompressed screenshots (or any other images, i guess) into (hopefully) smaller sizes using imagemagick (convert)
# this version will also cut the top and bottom border (80px)

# usage: convert [scan_dir=$pwd] [output_dir=screens]

function __colortext(){ echo -e "\e[1;$2m$1\e[0m";};
function echogreen(){ echo $(__colortext "$1" "32");};
function echored(){ echo $(__colortext "$1" "31");};
function echoblue(){ echo $(__colortext "$1" "34");};
function echopurple(){ echo $(__colortext "$1" "35");};
function echoyellow(){ echo $(__colortext "$1" "33");};
function echocyan(){ echo $(__colortext "$1" "36");}

OUTPUT="screens"
CURRENT="$PWD"

if [[ $# -gt 0 ]] ; then
	CURRENT="$1"
fi

if [[ $# -gt 1 ]] ; then
	OUTPUT="$2"
fi

echo "$(echoyellow "$CURRENT") .." && cd "$CURRENT"

sleep 1

for FILE in "$CURRENT/"*; do 
	if [ -f "$FILE" ]; then
		EXTENSION="${FILE##*.}"

		case "${EXTENSION,,}" in
			"png"|"jpg"|"jpeg")
				if [ ! -d "$OUTPUT" ]; then
					mkdir -p "$OUTPUT"
				fi

				FILENAME=$(basename "$FILE")
				DESTINATION="$OUTPUT/$(basename "${FILENAME%.*}").jpg"

				printf "$FILENAME .. "

				convert -shave 0x80 "$FILENAME" "$DESTINATION"

				if [ -f "$DESTINATION" ]; then
					printf "$(echogreen "Done!")\n"
				else
					printf "$(echored "Error!")\n"
				fi
			;;
		esac
	fi
done