#!/bin/bash

# costruisce una struttura di file .html per visualizzare 
# le foto contenute nella directory corrente
# bisogna mettere delle icone "up.png" "left.png" e "right.png" nella 
# directory .icons (o in ../[...]/.icons) o in /usr/local/lib/photoindex/ 


if [ "$1" != "" ] ; then
cat <<EOF
photoindex: manu-fatto (2003)

USAGE: photoindex

DESCRIPTION:
    creates a file "index.html" with the list of all images found in
    the current directory. Subdirectories are also listed if they
    contain an "index.html" file.

    The index file contains a small copy of every image to make
    icons. Next to each icon it contains the description of the image
    which is taken either from a file "*.txt" with the same basename
    of the image file or from the description encoded in the image
    itself. Every image icon has a link to an HTML file which contains
    a bigger copy of the image and which contains a link to the
    original image.  

    The program is written to be fast if only a few images have been
    modified: this is achieved by checking modification times and by
    caching important properties of images.

    The program looks for buttons icons in the directories: .icons,
    ../.icons,... and /usr/local/lib/photoindex/. The name of the
    icons are: "up.png", "left.png" and "right.png".

EOF

exit

fi

if [ "$1" == "-r" ] ; then
  recurse=1
fi

allow_null_glob_expansion=1

OUT=index.html
TITLE=`basename "$PWD"`
SMALL=".small"
MEDIUM=".medium"
SMALL_GEOMETRY="80x60"
MEDIUM_GEOMETRY="640x480"

SIGNATURE="photoindexsignature"

ICONS=""

BASEUP="INDIETRO"
UP="INDIETRO"
LEFT="PRECEDENTE"
RIGHT="SUCCESSIVO"

for dir in ./.icons ../.icons ../../.icons ../../../.icons ../../../../.icons ../../../../../.icons /usr/local/lib/photoindex/ ; do
  if [ -f $dir/up.png -a -f $dir/left.png -a -f $dir/right.png ] ; then
    echo Ho trovato le icone nella directory "$dir"
    BASEUP="<img src=\"$dir/up.png\" alt=\"$BASEUP\">"
    if [ "${dir:0:1}" != "/" ] ; then
      dir="../$dir"
    fi
    UP="<img src=\"$dir/up.png\" alt=\"$UP\">"
    LEFT="<img src=\"$dir/left.png\" alt=\"$LEFT\">"
    RIGHT="<img src=\"$dir/right.png\" alt=\"$RIGHT\">"
    break
  fi
done

if [ -f index.htm ] ; then 
  echo Esiste il file index.htm, meglio lasciar perdere $PWD
  exit
fi

if [ -f $OUT ] ; then
    if grep -q "$SIGNATURE" "$OUT" ; then
	true
    else
	echo il file $OUT non l\'ho firmato io... meglio lasciar perdere $PWD
	exit
    fi
fi


echo Creo il file $PWD/$OUT
echo > $OUT

if [ -f index.txt ] ; then
  TITLE=`head -1 index.txt`
fi

cat <<EOF >>$OUT
<html>
<!-- QUESTO FILE E'' STATO GENERATO AUTOMATICAMENTE con photoindex -->
<!-- ***NON MODIFICARE*** -->
<!-- $SIGNATURE -->
<body>
<h1>$TITLE</h1>
EOF

## eventuale link alla directory superiore

for file in ../index.html ../index.htm ; do
  if [ -f "$file" ] ; then
    echo "<a href=\"$file\">$BASEUP</a><br>" >> $OUT
    break
  fi
done


## links alle sottodirectory

for file in * ; do 
if [ -d "$file" ] ; then
  echo DIRECTORY "$file"
  if [ "$recurse" ] ; then
    pushd "$file"
    $0 $1
    popd
  fi
  title="$file"
  if [ -f "$file"/index.txt ] ; then
    title=`head -1 "$file"/index.txt`
  fi
  for try in "$OUT" "index.htm" ; do
    if [ -f "$file"/"$try" ] ; then
      echo "<a href=\"$file/$try\">$title</a><br>" >> $OUT
      break
    fi
  done
fi
done

## crea le directory .small e .medium e cancella i files che 
## non servono piu`

for dir in $SMALL $MEDIUM ; do
  if [ ! -d "$dir" ] ; then
    echo Creo la directory "$dir"
    mkdir "$dir"
  fi
  cd $dir
  for file in * ; do
    if [ "$file" = "*" ] ; then
      continue
    fi
    if [ ! -f ../"${file%.*}" -a ! -f ../"$file" ] ; then
      echo "rimuovo $dir/$file"
      rm "$file"
    fi
  done
  cd ..
done

## crea le foto piccole, le icone e i files secondari per ogni foto

prev="";
file="";
for next in *.{jpg,png,gif} ""; do

if [ "${next:0:1}" == "*" ] ; then continue
fi

if [ "$file" ] ; then

if [ -L "$file" ] ; then
  realfile=`ls -l "$file" | cut -f2 -d">" | cut -c2-`
  echo "($file -> $realfile)"
else
  realfile="$file"
  echo "($file)"
fi

txt=""

for try in "${file}.txt" "${file%.*}.txt" "${realfile}.txt" "${realfile%.*}.txt" ; do
  if [ -e "$try" ] ; then
    txt="$try"
    break
  fi
done

name="${file//.*}"

if [ "$file" -nt .medium/"$file".cache -o "$txt" -nt .medium/"$file".cache ] ; then 

TEMP="/tmp/photoindex-$$"

identify -format "ZZSKIPZZSIZE=%wx%h\nZZSKIPZZTITLE= %c" "$file" > $TEMP

SIZE=`grep SIZE= $TEMP | cut -f2- -d=`
title=`grep TITLE= $TEMP | cut -f2- -d=`
description=`grep -v ZZSKIPZZ $TEMP`

#description=`(echo -n ZZSKIPZZ ; identify -format "%c" "$file" ) |grep -v ZZSKIPZZ`
#title=`identify -format "%c" "$file"|head -1`

if [ "$title" = "" ] ; then
    title="$name"
fi

if [ "$txt" ] ; then
  title=`head -1 "$txt"`
  description=`(echo -n ZZSKIPZZ ; cat "$txt") | grep -v ZZSKIPZZ`
fi

echo title="\"$title\"" > .medium/"$file".cache
echo description="\"$description\"" >> .medium/"$file".cache

else
 . .medium/"$file".cache
 echo '(cached)'
fi

medium=`dirname "$realfile"`/$MEDIUM/`basename "$realfile"`

if [ "$realfile" != "$file" -a -e "$medium" ] ; then
  echo "$medium -> $MEDIUM/$file"
  if [ "${medium:0:1}" == '/' ] ; then
    ln -sf "$medium" $MEDIUM/"$file"
  else
    ln -sf ../"$medium" $MEDIUM/"$file"
  fi
elif [ "$file" -nt $MEDIUM/"$file" ] ; then
  echo Creo il file $MEDIUM/"$file"
  convert -geometry "$MEDIUM_GEOMETRY" $file $MEDIUM/"$file"
fi

small=`dirname "$realfile"`/$SMALL/`basename "$realfile"`

if [ "$realfile" != "$file" -a -e "$small" ] ; then
    echo "$small -> $SMALL/$file"
  if [ "${small:0:1}" == '/' ] ; then
    ln -sf "$small" $SMALL/"$file"
  else
    ln -sf ../"$small" $SMALL/"$file"
  fi
elif [ "$file" -nt $SMALL/"$file" ] ; then
  echo Creo il file $SMALL/"$file"
  convert -geometry "$SMALL_GEOMETRY" $file $SMALL/"$file"
fi

OUTOUT="$MEDIUM"/"$file".html

echo > "$OUTOUT"
cat <<EOF >> "$OUTOUT"
<html>
<body>
<h1>$title</h1>
EOF

echo "<p>" >> "$OUTOUT"
echo "<a href=\"../$OUT\">$UP</a> " >> "$OUTOUT"
if [ "$prev" ] ; then
  echo "<a href=\"../$MEDIUM/${prev}.html\">$LEFT</a> " >> "$OUTOUT"
else
  echo "$LEFT" >> "$OUTOUT"
fi

if [ "$next" ] ; then
  echo "<a href=\"../$MEDIUM/${next}.html\">$RIGHT</a> " >> "$OUTOUT"
else
    echo "$RIGHT" >> "$OUTOUT"
fi

echo "</p>" >> "$OUTOUT"

echo "<img src=\"../$MEDIUM/$file\"><br>" >> "$OUTOUT"

echo "<pre> $description </pre>" >> $OUTOUT

echo "<p>(file: <a href=\"../${realfile}\">$file</a>, size=$SIZE)</p>" >> $OUTOUT


cat <<EOF >> "$OUTOUT"
</body>
</html>
EOF

#SIZE=`identify -format "%wx%h" "$SMALL/$file"`
#echo "<a href=\"$OUTOUT\"><img width=${SIZE//x*} height=${SIZE//*x} src=\"$SMALL/$file\"></a>&nbsp;<strong>$title</strong><br>" >> $OUT
echo "<a href=\"$OUTOUT\"><img src=\"$SMALL/$file\"></a>&nbsp;<strong>$title</strong><br>" >> $OUT

fi


prev="$file"
file="$next"

done

if [ -f index.txt ] ; then
  echo "<pre>" >> $OUT
  (echo -n ZIZZUZ; cat index.txt) | grep -v ZIZZUZ >> $OUT
  echo "</pre>" >> $OUT
fi



cat <<EOF >>$OUT
<p>(directory: ${PWD##*/})</p>
</body>
</html>
EOF
