#!/bin/sh -eu
# Last edited on 2009-12-27 18:54:49 by stolfi

# Uses "p.sizes" in each directory

echo "script = ${0}"

imgbin="${STOLFIHOME}/EXPORT/images/tools/bin"
makepngs="${imgbin}/make-scaled-png-images"; # echo "makepngs = ${makepngs}"
getdims="${STOLFIHOME}/bin/get-pnm-dimensions"; # echo "getdims = ${getdims}"

#  -test

makepsizes() {
  # Make sure that the file "p.sizes" exists:
  if [[ ! -s $1/p.sizes ]] ; then
    echo "$1/p.sizes not found" 1>&2;
    # Get the raw image size ${nx},${ny}:
    if [[ -s $1/p-raw.ppm.gz ]] ; then
      fxy=( `zcat $1/p-raw.ppm.gz | ${getdims}` )
    elif [[ -s $1/p-raw.ppm ]] ; then
      fxy=( `cat $1/p-raw.ppm | ${getdims}` )
    elif [[ -s $1/p.ppm.gz ]] ; then
      fxy=( `zcat $1/p.ppm.gz | ${getdims}` )
    elif [[ -s $1/p.ppm ]] ; then
      fxy=( `cat $1/p.ppm | ${getdims}` )
    else
      echo "$1/p-raw.ppm(.gz) not found" 1>&2;
      return
    fi
    nx=${fxy[0]}; ny=${fxy[1]}
    if [[ ( -z "${nx}" ) ||  ( -z "${ny}" ) ]]; then
      echo "** could not get the image dimensions" 1>&2;
      return
    fi
    echo "image size = ${nx}x${ny}" 1>&2
    # Reduce ${nx}, ${ny} to relatively prime ratio:
    tx=${nx}; 
    ty=${ny};
    while [[ ${ty} -gt 0 ]]; do
      tr=$(( ${tx} - ( ( ${tx} / ${ty} ) * ${ty} ) ))
      tx=${ty}
      ty=${tr}
    done
    gcd=${tx}
    echo "gcd(${nx},${ny}) = ${gcd}" 1>&2
    aspx=$(( ${nx} / ${gcd} ))
    aspy=$(( ${ny} / ${gcd} ))
    echo "aspect ratio ${aspx}:${aspy}" 1>&2
    # Pick a multiple of the aspect ratio that is about the right icon size:
    nt=$(( ${aspx} + ${aspy} ))
    sc=$(( ( 179 + ${nt} ) / ${nt} ))
    mx=$(( ${aspx} * ${sc} ))
    my=$(( ${aspy} * ${sc} ))
    echo "icon size = ${mx}x${my}" 1>&2
    # Creating a "p.sizes" with icon size and full size:
    echo "${mx}x${my}" > $1/p.sizes
    echo "${nx}x${ny}" >> $1/p.sizes
  fi
}

mpng() {
  if [[ $# -ne 1 ]]; then
    echo "invalid args mpng($@)" 1>&2;
    return
  fi
  echo "=== making scaled pngs for $1 ====================================="
  
  if [[ ! -d $1 ]]; then
    echo "** directory $1 does not exist" 1>&2;
    return
  fi

  # Make sure that some source file "p-raw.ppm(.gz)" "p.ppm(.gz)"  exists:
  hasppmraw=0;
  if [[ -s $1/p-raw.ppm ]] ; then hasppmraw=1; fi
  if [[ -s $1/p-raw.ppm.gz ]] ; then hasppmraw=1; fi
  if [[ ( -s $1/p-0-raw.ppm ) && ( -s $1/p-1-raw.ppm ) ]] ; then hasppmraw=1; fi
  if [[ ( -s $1/p-0-raw.ppm.gz ) && ( -s $1/p-1-raw.ppm.gz ) ]] ; then hasppmraw=1; fi
  
  hasppm=0;
  if [[ -s $1/p.ppm ]] ; then hasppm=1; fi
  if [[ -s $1/p.ppm.gz ]] ; then hasppm=1; fi
  if [[ ( -s $1/p-0.ppm ) && ( -s $1/p-1.ppm ) ]] ; then hasppm=1; fi
  if [[ ( -s $1/p-0.ppm.gz ) && ( -s $1/p-1.ppm.gz ) ]] ; then hasppm=1; fi
  
  if [[ ( ${hasppm} -eq 0 ) && ( ${hasppmraw} -eq 0 ) ]]; then
    echo "** $1 PPM source files not found" 1>&2;
    return
  fi

  # Make sure that the file "p.sizes" exists:
  if [[ ! -s $1/p.sizes ]] ; then
    echo "** $1/p.sizes not found" 1>&2;
    return
  fi

  if [[ ${hasppmraw} -ne 0 ]]; then
    # Make sure that the file "p.parms" exists:
    if [[ ! -s $1/p.parms ]] ; then
      echo "$1/p.parms not found, getting a standard one" 1>&2;
      if [[ ( "$1" =~ '^photos/') && ( -s photos/MODEL.parms ) ]]; then
        cp -p photos/MODEL.parms $1/p.parms
      else
        echo "** could not fake $1/p.parms" 1>&2;
        return
      fi
    fi
  fi

  ${makepngs} -show $1 `cat $1/p.sizes`
}

for dir in `cat do-ppm-to-png-todo.list | sed -e 's/#.*$//' -e '/^ *$/d' -e '/^END/,$d'` ; do 
  mpng $dir
done