#!/bin/sh
#
# PicIndex produces an HTML file with index pictures, names and sizes
#          of all web-pictures (jpg, gif) in the directory.
#
# author: Peter_Hanssen@writeme.com
# last change: 31 Oct 2000
#
# note: no PNG support, coz my machine's ImageMagick doesn't support
#       it... ;) edit the script if you need it.

# You may change this default settings
#
# Directory name for the small index-pictures
DIR=.picindex
# Name of the output HTML-file
OF=.picindex.html
# Number of columns for each row:
COL=3
# Height of small index-picture im pixel
MAX_HEIGHT=50
# Text size
TZ=-1

# Check if there are web-pictures (jpg, gif) in this directory and if
# so produce the directory for the small index-pictures if it doesn't
# exists.
if [ ! -r *.jpg ] && [ ! -r *.gif ]
then
 echo "No images (jpg, gif) found!"
 echo "... abort."
 echo " " 
 exit 2
else
 # Make directory for small index-pictures if not exists
 if [ -d ${DIR} ]; then
  echo "\n!!! ${DIR} exists already. "
  echo "!!! I might delete some old index-pictures..."
 else 
  mkdir ${DIR}
  echo "\nDirectory ${DIR} created for index-pictures..."
 fi
fi
echo " " 

echo "<HTML><HEAD><TITLE>PicIndex - " > ${OF}
pwd >> ${OF}
echo "</TITLE></HEAD><BODY BGCOLOR=\"#FFFFFF\"" >> ${OF}
echo "TEXT=\"#000000\" LINK=\"#000000\" VLINK=\"000000\">" >> ${OF}
echo "<H1>Picture-Index</H1>" >> ${OF}
echo "<P><B>directory:</B> " >> ${OF}
pwd >> ${OF}
echo "<BR><B>last update:</B> " >> ${OF}
date >> ${OF}
echo "</P>" >> ${OF}
echo " " >> ${OF}

index_pic () {
N=0
 echo "INDEXING *.${SUF}..."
 echo " " >> ${OF}
for FILE in *.${SUF}
do
 echo "  Working on ${FILE}..."
 N=`expr $N + 1`
 FILENAME=`basename ${FILE} .${SUF}`
 echo "" >> ${OF}

 if [ $N -eq 1 ]; then
  echo "<TR>" >> ${OF}
 fi

 ORG_HEIGHT=`identify ${FILE} | head -1 | cut -f2 -d" " | \
             cut -dx -f2 | cut -d"+" -f1`
 if [ ${ORG_HEIGHT} -gt ${MAX_HEIGHT} ]; then
  convert -blur 70 -geometry x${MAX_HEIGHT} -sharpen 20 ${FILE} \
          ${DIR}/sm_${FILENAME}.${SUF}
  HEIGHT=${MAX_HEIGHT}
 else
  cp ${FILE} ${DIR}/sm_${FILENAME}.${SUF} 
  HEIGHT=${ORG_HEIGHT}
 fi

 WIDTH=`identify ${DIR}/sm_${FILENAME}.${SUF} | head -1 | \
       cut -f2 -d" " | cut -dx -f1`
 if [ ${WIDTH} -gt 110 ]; then
  XOFFSET=`expr ${WIDTH} / 2 - 55`
  convert -crop 110x${HEIGHT}+${XOFFSET} \
          ${DIR}/sm_${FILENAME}.${SUF} ${DIR}/sm_${FILENAME}.jpg
  WIDTH=110
 fi
 echo " <TD ALIGN=RIGHT>" >> ${OF}
 echo " <TABLE BORDER=0 ALIGN=LEFT><TR><TD>" >> ${OF}
 echo "     <A HREF=\"${FILE}\"><IMG" >> ${OF}
 echo "     SRC=\"${DIR}/sm_${FILENAME}.${SUF}\"" >> ${OF}
 echo "     WIDTH=${WIDTH} HEIGHT=${HEIGHT}" >> ${OF}
 echo "     BORDER=0 ALT=\"${FILE}\"></A></TD>" >> ${OF}
 if [ ${SUF} = "jpg" ]; then
  XY=`identify ${FILE} | head -1 | cut -f2 -d" "`
  BYTE=`identify ${FILE} | head -1 | cut -f4 -d" " | sed s/b//`
 fi
 if [ ${SUF} = "gif" ]; then
  XY=`identify ${FILE} | head -1 | cut -f2 -d" " | cut -f1 -d"+"`
  BYTE=`identify -verbose ${FILE} | grep bytes | \
       head -1 | cut -f2 -d:`
 fi

 echo "     <TD VALIGN=TOP><FONT SIZE=\"${TZ}\">" >> ${OF}
 echo "     <A HREF=\"${FILE}\">${FILE}</A><BR>" >> ${OF}
 echo "     ${XY}<BR>" >> ${OF}
 if [ ${BYTE} -ge 10000000 ]; then
  SIZE=`echo "scale=1;${BYTE} / 1000000"|bc -l`
  echo "     <FONT COLOR=\"#FF0000\">${SIZE} MB</FONT>" >> ${OF}
 elif [ ${BYTE} -ge 1000000 ]; then
  SIZE=`echo "scale=2;${BYTE} / 1000000"|bc -l`
  echo "     <FONT COLOR=\"#FF0000\">${SIZE} MB</FONT>" >> ${OF}
 elif [ ${BYTE} -ge 100000 ]; then
  SIZE=`echo "scale=0;${BYTE} / 1000"|bc -l`
  echo "     ${SIZE} KB" >> ${OF}
 elif [ ${BYTE} -ge 10000 ]; then
  SIZE=`echo "scale=1;${BYTE} / 1000"|bc -l`
  echo "     ${SIZE} KB" >> ${OF}
 elif [ ${BYTE} -ge 1000 ]; then
  SIZE=`echo "scale=2;${BYTE} / 1000"|bc -l`
  echo "     ${SIZE} KB" >> ${OF}
 else
  echo "     ${BYTE} Byte" >> ${OF}
 fi
 echo "     </FONT></TD></TR></TABLE></TD>" >> ${OF}

 if [ $N -eq ${COL} ]; then
  echo "</TR>" >> ${OF}
  N=0
 fi

done

if [ $N -ne 0 ]; then
 echo "</TR>" >> ${OF}
fi

}

if [ -r *.jpg ]; then
 N=0
 SUF=jpg
 echo "<H2>${SUF} - images:</H2>" >> ${OF}
 echo "<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0>" >> ${OF}
 index_pic
 echo "</TABLE>" >> ${OF}
fi

if [ -r *.gif ]; then
 N=0
 SUF=gif
 echo "<H2>${SUF} - images:</H2>" >> ${OF}
 echo "<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0>" >> ${OF}
 index_pic
 echo "</TABLE>" >> ${OF}
fi

echo "" >> ${OF}
echo "<P>made by <A HREF=\"http://I.am/Pete\"" >> ${OF}
echo "TARGET=\"_new\">Pete's picindex.sh</A></P>" >> ${OF}
echo "" >> ${OF}
echo "</BODY></HTML>" >> ${OF}

echo ""
echo "*** PicIndex finished"


