#!/bin/bash
#
# Scales pictures to either web-size or thumb-size using
# ImageMagick's convert-command.
#
# Håvard Berland
# http://www.pvv.ntnu.no/~berland/
#
# Scaling is skipped if scaled file already exists and is newer
# than original file.
#
# This script is designed for this example picture hierarchy:
#  2005--+- 01 
#        `- 01-trip
#  2006--+- 01 
#        +- 02 
#        `- 03 
# and will generate the structure
#  web--+-2005--+- 01 
#       |       `- 01-trip
#       `-2006--+- 01 
#               +- 02 
#               `- 03 
# although nothing of this is enforced or difficult to change.
#
# When launching the script, ensure your working directory is at the top
# level of your hierarchy.

# To force scaling (if you have changed options), do 
# $ find scaled/web -type f -exec touch -d "19800101" {} \;
# (this is preferred over 'rm -rf web')
#
# Current option-sets for ImageMagick's convert requires
# ImageMagick version 6 and upwards.

# If no directories are supplied, use this wildcard pattern:
allsubdirspattern="*20??"

ulimit -d 300000

usage () {
    echo "Scales pictures for use on web, thumbs, etc."
    echo "\$ scale -web       # Scales all pictures to web-resolution"
    echo "\$ scale -thumb     # Scales all pictures to thumbs-resolution"
    echo "\$ scale -web 2004  # Scales all pictures in directory 2004 to web"
}


# Add more option sets here if needed.
if [ "$1" == "-web" ] ; then
    dir='scaled/web'
    size="800x600"
    convertoptions="-quality 90 -strip"
    unsharpen="-unsharp 0.3x0.6+1.3"
    shift
elif [ "$1" == "-thumb" ] ; then
    dir='scaled/thumbs'
    size="500x85"
    convertoptions="-quality 70 -strip"
    unsharpen="-unsharp 0.3x0.6+1.3"
    shift
elif [ "$1" == "-tv" ] ; then
	dir='scaled/tv'
	size="1366x768"
	convertoptions="-quality 85 -strip"
	unsharpen="-unsharp 0.3x1+2"
	shift
elif [ "$1" == "-nokia" ] ; then
	dir='scaled/nokia'
	size="300x300"
	convertoptions="-quality 92 -strip"
	unsharpen="-unsharp 0.3x0.6+1.3"
	shift
else
    usage
    exit 1
fi

mkdir -p $dir

#######################################
# Function to process one subdirectory:
#
# Converts all jpg-files in chosen subdir,
# preserves arbitrary directory structures.
dosubdir () {
    subdir=$1
   
    if [ "Darwin" == `uname -s` ] ; then
	XARGS="xargs -I % mkdir -p $dir/%"
    else
	XARGS="xargs --replace mkdir -p $dir/{}"
    fi
 
    # Duplicate directory structure with exceptions for safety:
    # (there is a risk of infinite recursion here)
    find $subdir -type d \
	| grep -v -e $dir -e .qiv-trash -e .xvpics \
	| $XARGS ; 

    
    files=`find $subdir -name *.jpg  | grep -v .qiv- | wc -l`
    filecounter=0
    echo "Doing subdir $subdir"
    for filename in `find $subdir -name *.jpg | grep -v qiv-trash | grep -v xvpics` ; do
	filecounter=$((filecounter+1))
	echo -n " $filecounter / $files   $filename                "

	if [ ! -f "$dir/$filename" -o "$filename" -nt "$dir/$filename" ] ; then
	    convert -geometry $size $convertoptions $unsharpen \
		"$filename" "$dir/$filename" || rm -f "$dir/$filename"
	fi
	echo -e "\r\c"
    done
    echo
}

########################################
# Entry point of script


# If no arguments, process all subdirs
if [ $# == 0 ]; then
    for subdir in `ls -d $allsubdirspattern`
      do
      dosubdir $subdir
    done
else
    # If any (remaining) shell arguments, assume it is subdirectories
    for subdir in $@
      do
      if [ -d $subdir ] ; then
	  dosubdir $subdir
      else
	  echo "$subdir not found"
      fi
    done
fi
