#!/bin/bash

# TP shell
#
# Question I - 6
# 
# Antoine Min�é
# 26/01/2007

# cette solution est d�ériv�ée de celle de la question 2
# on peut y ajouter les am�éliorations des questions 4 et 5


# nettoyage
rm -f index.html
rm -f thumb-*

# en-t�ête
echo "<html>" >> index.html
echo "<head><title>Album</title></head>" >> index.html
echo "<body><h1>Album</h1>" >> index.html

# images
for i in *
do
    if test -f "$i"
    then
	j=`echo "$i" | sed -e 's/&/\\&amp;/g' -e 's/"/\\&quot;/g' -e 's/</\\&lt;/g' -e 's/>/\\&gt;/g'`
	case `file "$i"` in
	    
	    *image\ data*) # image
	        convert "$i" -quality 35 -resize 120x120 "thumb-$i"
	        echo "<a href=\"$j\"><img src=\"thumb-$j\"></a>" >> index.html
	    ;;
	    
	    *movie* | *video*) # film
		mplayer "$i" -vo jpeg -frames 1 >/dev/null 2>/dev/null
		convert 00000001.jpg -quality 35 -resize 120x120 "thumb-$i.jpg"
		rm 00000001.jpg
		echo "<a href=\"$j\"><img src=\"thumb-$j.jpg\"></a>" >> index.html
		;;

	    *audio* | *MPEG*) # son
		echo "<a href=\"$j\"><img src=\"http://www.di.ens.fr/~mine/sound.png\"></a>" >> index.html
		;;

	    *PostScript* | *PDF*) # texte PostScript ou PDF
		gs -sDEVICE=jpeg -sOutputFile=tmp.jpg -dNOPAUSE  -dQUIET -dBATCH -r120x120 "$i"
		convert tmp.jpg -quality 35 -resize 120x120 "thumb-$i.jpg"
		rm tmp.jpg
		echo "<a href=\"$j\"><img src=\"thumb-$j.jpg\"></a>" >> index.html
		;;

	    *) # �à compl�éter: voir /usr/local/util/etc/magic pour la liste des formats!
		;;
	esac
    fi
done

# fin de page
echo "</body></html>" >> index.html
