#!/bin/sh
#
# https://wiki.ubuntuusers.de/Nautilus-Actions/
# sudo apt-get install nautilus-actions 
# nautilus-actions-config-tool 
#   --dir "%d" --files "%B"
#   --dir /foo/bar --files "f1 f2" 
# nautilus --quit

LOCK="$0.lock"
LOGFILE="$0.log.txt"
mkdir "$LOCK" || exit 0

log()
{
	local txt="$1"
	local option="$2"
	local message="$0: [$$] $1"

	logger -s "$message $option"
	echo "$(date) $message" >>"$LOGFILE"

	case "$option" in
		'and_exit')
			rm -fR "$LOCK"
			exit 1
		;;
	esac
}

while [ -n "$1" ]; do {
	case "$1" in
		'--dir')
			shift
			eval DIR=$1
			cd "$DIR" || log "[ERR] rc: $? - cd '$DIR'" and_exit
		;;
		'--files')
			shift
			FILE_LIST="$@"	# e.g. 'file1' 'file2' (including single quotes)
			break
		;;
		*)
			shift
		;;
	esac
} done

for FILE in $FILE_LIST; do {
	eval FILE=$FILE

	DEST="$( echo "$FILE" | sed -e 's/.JPG$/-klein.JPG/g' -e 's/.jpg$/-klein.jpg/g' )"
	[ -e "$DEST" ] || {
		log "in '$DIR': convert '$FILE' -resize '1024x768' -quality 70 '$DEST'"
		convert "$FILE" -resize '1024x768' -quality 70 "$DEST" || log "[ERR] rc: $?"
	}
} done

rm -fR "$LOCK"
