#! /bin/csh -f 
# Last edited on 2008-02-04 18:24:03 by stolfi

set usage = "$0 [ -reusetiff ] [ -reusepnm ]"

# Enumerates all "*.tiff" and "p-raw.ppm*' files under the current
# directory, then extracts their sizes.  Merges the two lists,
# printing the output for DIR/p.tiff and that of DIR/p-raw.tiff on the
# same line; the result is the file .images.csz.
#
# Also writes to a separate file .images-similar.csz the TIFF/rawPPM
# pairs where the former is not significantly larger than the second.

# Set program paths: 

set tooldir = "$0"; set tooldir = "${tooldir:h}"
source ${tooldir}/../lib/${PLATFORM}/setpaths.csh 

set reusetiff = 0
set reusepnm = 0
while ( ( $#argv > 0 ) && ( "/$1" =~ /-* ) ) 
  if ( ( $#argv >= 1 ) && ( "/$1" == "/-reusetiff" ) ) then
    set reusetiff = 1; shift
  else if ( ( $#argv >= 1 ) && ( "/$1" == "/-reusepnm" ) ) then
    set reusepnm = 1; shift
  else
    echo 'unknown option "'"$1"'"'
    echo "usage: ${usage}"; exit 1
  endif
end

if ( $#argv != 0 ) then
    echo "usage: ${usage}"; exit 1
endif

set libdir = "${STOLFIHOME}/EXPORT/images/tools/lib"
set bindir = "${STOLFIHOME}/EXPORT/images/tools/lib"

set tifsiz = ".tiff.siz"
set pnmsiz = ".pnm.siz"
set tmp = "/tmp/$$"

if ( ${reusetiff} ) then 
  echo "using existing ${tifsiz}"
else
  echo "locating and measuring TIFF files..."
  find ./ -name '*.tiff' -print \
    | egrep -v '[~]' \
    > .tiff-files.dir
  list-tiff-sizes `cat .tiff-files.dir` | sort -b +2 -3 > ${tifsiz}
endif

if ( ${reusepnm} ) then
  echo "using existing ${pnmsiz}"
else
  echo "locating and measuring raw PPM files..."
  find ./ -name 'p-raw.p[bpg]m*' -print \
    | egrep -v '[~]' \
    > .pnm-raw-files.dir
    
  list-pnm-sizes `cat .pnm-raw-files.dir` | sort -b +2 -3 > ${pnmsiz}
endif

foreach f ( ${tifsiz} ${pnmsiz} )
  cat ${f} \
    | sed -e 's:/p[-.].*$::g' \
    | sort -b +2 -3 \
    | uniq \
    > ${tmp}${f}
end

join \
    -a1 -a2 -e '0' -j1 3 -j2 3 -o1.1,1.2,2.1,2.2,0 \
    ${tmp}${tifsiz} ${tmp}${pnmsiz} \
  | gawk '//{printf "%4d %4d  %4d %4d  %s\n", $1,$2,$3,$4,$5;}' \
  > .images.csz
  
cat .images.csz \
  | gawk -f ${libdir}/check-superfluous-tiffs.gawk \
  > .images-similar.csz

/bin/rm -f ${tmp}${tifsiz} ${tmp}${pnmsiz}