#!/bin/bash
# snapshrink by Andrew Oakley 2014-09 aoakley.com Public Domain

versiondate="2014-09-11"
maxpixels=1200
quality=75
append=""
force=""
quiet=""
verbose=""

while [[ "${1:0:1}" == "-" ]]; do
  case "$1" in
    ("-h"|"--help")
      echo "Usage: snapshrink [ OPTIONS ] INPUTDIR OUTPUTDIR"
      echo "       snapshrink [ OPTIONS ] -f | -a DIR"
      echo "Requires imagemagick v=>6.7 with adaptive-resize support"
      echo
      echo "Mandatory arguments to long options are mandatory for short options too."
      echo "--version        Version and authorship information (for bug reporting)"
      echo "-h, --help       Show this help text then exit"
      echo "-a, --append     Append last part of the input directory, to the output path"
      echo "                 If no output specified, create dir in current working dir"
      echo "-f, --force      Force overwriting of existing files (dangerous)"
      echo "-m, --max NNNN   Max pixels of either dimension, default 1200 (roughly 1Mpx)"
      echo "-j, --quality NN JPEG quality, default 75"
      echo "-q, --quiet      Quiet output"
      echo "-v, --verbose    Verbose output"
      echo
      echo "Takes a directory of JPEG photos, downsamples the larger ones to approx"
      echo "1Mpx and then outputs them to a new directory. Ideal for archiving holiday"
      echo "snaps where they have sentimental value, but you don't want, or have space"
      echo "for, full resolution from a high megapixel camera."
      echo "1 megapixel is 200dpi on a 6x4\"/10x15cm standard photo."
      exit 1
    ;;
    ("--version")
      echo "snapshrink by Andrew Oakley http://www.aoakley.com"
      echo "This version dated $versiondate"
      exit 1
    ;;
    ("-a"|"--append")
      append="true"
      shift
    ;;
    ("-f"|"--force")
      force="true"
      shift
    ;;
    ("-m"|"--max")
      shift
      maxpixels=$1
      shift
    ;;
    ("-j"|"--quality")
      shift
      quality=$1
      shift
    ;;
    ("-q"|"--quiet")
      quiet="true"
      shift
    ;;
    ("-v"|"--verbose")
      verbose="-verbose"
      shift
    ;;
    (*)
      echo "Invalid parameter: $1"
      exit -2
  esac
done
indir=$1
if [[ "$indir" == "" ]]; then
  echo "No input directory specified."
  exit -1
fi
if [[ ${#indir} -gt 1 && "${indir:(-1)}" == "/" ]]; then
  indir=$(echo $indir | sed -e "s/\/$//")
fi
if [[ !(-d $indir) ]]; then
  echo "$indir is not a directory"
  exit -1
fi
if [[ ( ${#indir} -gt 1 && "${indir:(-1)}" == "/" ) || $indir == "/" ]]; then
  indir=$(echo $indir | sed -e "s/\/$//")
fi
if [ $verbose ]; then
  echo "indir  =\"$indir\""
fi
outdir=$2
if [ $append ]; then
  if [[ "$outdir" == "" ]]; then
    outdir=$PWD/`basename "$indir"`
  else
    outdir=$outdir/`basename "$indir"`
  fi
fi
if [ $force ]; then
  if [[ "$outdir" == "" ]]; then
    outdir=$indir
  else
    echo "You cannot have both -f (--force) and an output directory."
    exit -1
  fi
else
  if [[ "$outdir" == "" ]]; then
    echo "No output directory specified."
  exit -1
  fi
  if [[ ${#outdir} -gt 1 && "${outdir:(-1)}" == "/" ]]; then
    outdir=$(echo $outdir | sed -e "s/\/$//")
  fi
  mkdir -p "$outdir" 2>/dev/null
  if [[ !(-d "$outdir") ]]; then
    echo "$outdir exists but is not a directory"
    exit -1
  fi
  if [[ ( ${#outdir} -gt 1 && "${outdir:(-1)}" == "/" ) || $outdir == "/" ]]; then
    outdir=$(echo $outdir | sed -e "s/\/$//")
  fi
fi
if [ $verbose ]; then
  echo "outdir =\"$outdir\""
fi

cd "$indir"
find -maxdepth 1 -type f -iregex '.*\.jpe?g$' -printf '%f\n' | xargs -i convert $verbose "{}" -adaptive-resize ${maxpixels}x${maxpixels}\> -quality $quality "$outdir/{}"

