#! /bin/bash
# Last edited on 2010-11-12 23:22:10 by stolfi

# Should create files "p.sizes" in each directory, and use them...

export PATH="${STOLFIHOME}/EXPORT/images/tools/bin:${PATH}"

# === MOVING/COPYING THE ORIGINAL IMAGES TO THEIR CANONICAL DIRECTORIES ==================
# *** IMPORTANT PARAMETERS *******************************************************
today="2010-11-12" # Date when this script was executed.
junkthem=0         # Set this to 1 to move all images to JUNK
testonly=1         # If 1 just prints the commands without executing.

function cop() {
  sdir="$1"; shift  # Directory of source image name.
  snum="$1"; shift  # Variable part of source image name.
  pdir="$1"; shift  # Parent diretory of destination image directory.
  dnum="$1"; shift  # Variable part of destination image directory.
  when="$1"; shift  # Date and time image was taken.
  who="$1"; shift   # Who took the photo.
  cam="$1"; shift   # Device used to produce the image.
  keep=$1; shift    # 1 to keep image, 0 to junk it.
  siz="$1"; shift   # Resize parameter for "convert". 
  rot="$1"; shift   # Rotation parameter for "convert".
  ops="$1"; shift   # Image quality codes (e.g. "(ZCT)"; see below).
  cmt="$1"; shift   # Contents to place in "p.comments". 
  
  sfile="${sdir}/${snum}"
  ddir="${pdir}/${dnum}"
  dfile="${ddir}/orig.png"
  cmtfile="${ddir}/p.comments"
  
  printf "%s\n" "--- ${sfile} --> ${dfile} ---"
  
  aut="Photo taken ${when} {by ${who}} with ${cam}"

  if [[ testonly -ne 0 ]]; then
    echo "convert ${sfile} -resize "${siz}" -rotate "${rot}" ${dfile}"  >&2;
    echo "${cmt}." | fmt --split-only  >&2;
    echo "${aut}." | fmt --split-only  >&2;
    return
  fi
  
  if [[ ${junkthem} -eq 0 ]]; then
    if [[ ${keep} -eq 0 ]]; then
      echo '!! image skipped'  >&2;
      return
    fi
    # Create a directory for image:
    if [[ ! -d ${ddir} ]] ; then mkdir -p ${ddir} ; fi
    # Create the comments file:
    echo "${cmt}." | fmt --split-only > ${cmtfile}
    echo "${aut}." | fmt --split-only >> ${cmtfile}
    cat ${cmtfile}  >&2;
    # Create the "orig" image file:
    convert ${sfile} -resize "${siz}" -rotate "${rot}" ${dfile}
    ls -l ${dfile}
    display -title "${dnum}/%f" ${dfile}
  else
    if [[ ! -d ${jdir} ]] ; then mkdir -p ${jdir} ; fi
    mv -vi ${sfile} ${jdir}
  fi
}

# Source templates

temp="${STOLFIHOME}/EXPORT/images/temp"
ptmp="${STOLFIHOME}/EXPORT/images/photos/TEMP"

# Destination templates

dtri="${STOLFIHOME}/EXPORT/images/photos/animals"
drsa="${STOLFIHOME}/EXPORT/images/photos/events/2008-04-sand-phd"
dkyo="${STOLFIHOME}/EXPORT/images/photos/events/2007-06-kyotocggt"
dblp="${STOLFIHOME}/EXPORT/images/photos/events/1997-sblp"

dmug="${STOLFIHOME}/EXPORT/images/photos/people"
even="${STOLFIHOME}/EXPORT/images/photos/events"
misc="${STOLFIHOME}/EXPORT/images/photos/misc"
land="${STOLFIHOME}/EXPORT/images/photos/landscapes"
capa="${STOLFIHOME}/EXPORT/images/photos/figures/covers"
toon="${STOLFIHOME}/EXPORT/images/photos/figures/cartoons"
plan="${STOLFIHOME}/EXPORT/images/photos/plants"

nada="/dev/null"

export jdir="${STOLFIHOME}/EXPORT/images/JUNK/processed/${today}"

  # Temporary tags:

  # (J) Junk it.
  # (K) Keep it

  # (N) needs no rotation.
  # (R) needs rotation by 90 degree ccw
  # (Z) needs a lot of cropping 
  # (C) needs non-trivial color correction

  # (S) sharp and good enough to remain as JPEG original size.
  # (P) sharp, convert to PNG with reduction to 2/3 of original size.
  # (H) lightly blurred, needs reduction to about 1/2(???) size.
  # (T) lightly blurred, needs reduction to about 1/3(???) size.
  # (Q) blurred, needs reduction to about 1/4(???) size.
  # (U) too blurry, unusable.
  
# === CONVERTING ORIGINAL IMAGES TO THE RAW PPM FILES ==================
export cvt=( tools/bin/convert-and-crop-original-images -show )

${cvt[@]} -crop   2  2  864  756   -size  800  700 figures/technical/reservoir-1/orig.png

exit 0
${cvt[@]} -crop  13  44 1800 3200  -size  450  800 figures/cartoons/justicell-1/orig.png