#!/bin/sh

rotate() {
	degree="$1"
	file="$2"
	case "$(file -b -i "$file")" in
	image/jpeg*) jpegtran -rotate "$degree" -copy all -outfile "$file" "$file" ;;
	*)           mogrify  -rotate "$degree" "$file" ;;
	esac
}

while read file; do
  case "$1" in
    "C-a" | "C-1")
      setbg "$file"
      exit ;;
    "C-2")
      setbg "$file" "2"
      exit ;;
    "C-s")
      setbglblur "$file"
      exit ;;
    "C-d")
      setbglsimple "$file"
      exit ;;
    "C-c")
      wl-copy < "$file"
      exit ;;
    "C-f")
      wl-copy "$file"
      exit ;;
    "C-r")
      # Move file to 'trash' directory, if the current directory's name is
      # JPEG, remove the NEF file as well
      dir="$(dirname "$file")"
      name="$(basename "$file")"
      base="${name%.*}"
      mv "$file" ~/.local/trash/
      if [ "JPEG" = "$(basename $dir)" ]; then
        mv $(dirname "$dir")/"$base".NEF ~/.local/trash/
      fi
      exit ;;
	"C-comma")  rotate 270 $file ;;
	"C-period") rotate  90 $file ;;
	"C-slash")  rotate 180 $file ;;
  esac
done