#!/bin/bash

# La couleur des boutons
COLOR="yellow"
N=15
MAINPAGE="../../"

INDEX="pages/index.html"

#On veut utiliser le programme identify pour savoir si les photos sont en portrait ou paysage
#et le programme convert pour faire des miniatures
IDENTIFY=/usr/bin/identify
CONVERT=/usr/bin/convert
#On verifie l'existence et l'executabilite des programmes convert et identify
test -x $CONVERT || exit 1
test -x $IDENTIFY || exit 1

#On verifie que le programme a bien ete appele avec trois arguments
[ "$#" -ne 3 ] && echo "syntaxe : $0 repertoire_photos titre_page fichier.css" && exit 1


#Et on leur donne des noms plus evocateurs
DIR="$1"
TITLE="$2"
CSS="$3"

cd $DIR
rm -rf "thumbs" "low_quality" "pages" "index"
mkdir "thumbs" "low_quality" "pages" "index"

# Le header pour html qui utilise les variables TITLE et CSS:
HEADER_HTML='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html id="gallery" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"  />
  <title>'"$TITLE"' </title>
  <link rel="stylesheet" type="text/css" href="'"../$CSS"'" media="screen" />
</head>

<body>
'

# Le header de la page principale
HEADER_MAIN='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html id="gallery" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"  />
  <title>'"$TITLE"' </title>
  <link rel="stylesheet" type="text/css" href="'"../$CSS"'" media="screen" />
</head>

<body id="gallery_main">
'



# Le header pour xhtml:
HEADER_XHTML='<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG1.1 Strict//EN"
    "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">
<html id="gallery" xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"  />
  <title>'"$TITLE"' </title>
  <link rel="stylesheet" type="text/css" href="'"../$CSS"'" media="screen" />
</head>

<body>'


# On fabrique des boutons en SVG
POINTS1="20,0 20,20 10,10" 
POINTS2="0,10 20,10, 10,0"
POINTS3="0,0 0,20, 10,10"

button (){
echo "
<li><a href=\"$3\">"' 
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20">
  <style type="text/css"> * { fill : none; }</style>'"
  <polygon style=\"stroke:$COLOR; fill:$COLOR\" points=\"$1\" />
</svg></a></li>
" >> "$2"
}  

#On fabrique des pages en XHTML
pageX (){

    echo "$HEADER_XHTML<ul>" > "$1"
    button "$POINTS1" "$1" "$2"
    button "$POINTS2" "$1" "$3"
    button "$POINTS3" "$1" "$4"

    echo "
</ul><div><a href=\"$5\">
<img class=\"grosse-image$W\" src=\"$5\" /></a></div>

<script type=\"text/javascript\">
function next (){
document.location=\"$4\"
}
setTimeout(next,4200)
</script>

</body></html>
" >> "$1"
}



# Et en HTML

page (){

    echo "$HEADER_HTML<ul>" > "$1"
    echo "
    <li><a href=\"$2\"> PREVIOUS </a></li>
    <li><a href=\"$3\"> UP </a></li>
    <li><a href=\"$4\"> NEXT </a></li>
    " >> "$1"  

    echo "
</ul><div><a href=\"$5\">
<img class=\"grosse-image$W\" src=\"$5\" /></a></div>

<script type=\"text/javascript\">
function next (){
document.location=\"$4\"
}
setTimeout(next,4200)
</script>

</body></html>
" >> "$1"
}

add_thumb (){
echo "<li><a href=\"$1\"><img class=\"dp\" src=\"../$thumbnail\" align=\"center\" /></a></li>
" >> "index/$2"
}

add_thumb3 (){
    add_thumb "$1"hq.xhtml index"$j"hq.xhtml
    add_thumb "$1"mq.xhtml index"$j"mq.xhtml
    add_thumb "$1"ie.html  index"$j"ie.html
}

pages_picture (){
pageX pages/"$1"hq.xhtml "$2"hq.xhtml  index"$j"hq.xhtml "$3"hq.xhtml "../$1"
pageX pages/"$1"mq.xhtml "$2"mq.xhtml  index"$j"mq.xhtml "$3"mq.xhtml "../low_quality/lowq.$1"
page pages/"$1"ie.html "$2"ie.html  index"$j"ie.html "$3"ie.html "../low_quality/lowq.$1"
}

# La fonction qui sera utilisee dans la boucle principale
itere (){

    echo "processing $pic ..."
    #On cree des miniatures et des images de taille moyenne
    thumbnail="thumbs/thumbnail.$2"
    $CONVERT -sample 200x200 "$2" "$thumbnail"
    lowq="low_quality/lowq.$2"
    $CONVERT -sample 600x600 "$2" "$lowq"
 
    # W=1 si la photo est en paysage
    ID=`$IDENTIFY "$2" | cut -d ' ' -f 3`
    WIDTH="${ID%x*}"
    HEIGHT="${ID#*x}"
    [[ $WIDTH -gt $HEIGHT ]] && W=1 || W=2

    #Et on remplit les pages qu'il faut
    add_thumb3 "$2"
    pages_picture "$2" "$1" "$3"

}

start () {


if [ $i == 0 ]; then 
echo "$HEADER_MAIN
<h1> $TITLE </h1>

<p> Revenir à <a href=\"$MAINPAGE\"> la page principale</a>.

<p>Je vous recommande d'utiliser Firefox (ou un autre navigateur capable d'afficher
du SVG) pour regarder les photos. Vous pouvez le télécharger 
<a href=\"http://www.mozilla.com/en-US/firefox/all.html#fr \"> ici </a></p>

<p>Lancer le diaporama:
<a href=\""$1"hq.xhtml\"> Haute qualité  </a>/
<a href=\""$1"mq.xhtml\"> Moyenne qualité  </a>/
<a href=\""$1"ie.html\"> Internet Explorer  </a>
</p><p>" > "$INDEX"

else
 echo " Photos $b - $i :
<a href=\"index"$a"hq.xhtml\"> Haute qualité </a>/
<a href=\"index"$a"mq.xhtml\"> Moyenne qualité</a>/
<a href=\"index"$a"ie.html\"> Internet Explorer</a> <br />" >> "$INDEX"

 echo "$HEADER_XHTML<h1> Photos $b - $i </h1><ul>" > pages/index"$a"hq.xhtml 
 echo "$HEADER_XHTML<h1> Photos $b - $i </h1><ul>" > pages/index"$a"mq.xhtml 
 echo "$HEADER_HTML <h1> Photos $b - $i </h1><ul>" > pages/index"$a"ie.html

fi
a=$j
b=$(( i + 1))
}

# Et on lance la boucle principale
i=-1
previous=index0


for next in *.jpg *.JPG ; do

    test -r "$next" || continue
    j=$(($i / $N))
    if test -n "$pic"; then
	itere "$previous" "$pic" "$next"
	[ $(($i % $N)) == 0 ] && start "$pic" 	
	previous="$pic"
    fi
    
    pic="$next"
    i=`expr $i + 1`
done

i=`expr $i + 1`
itere "$previous" "$pic" "index$j"
start "$pic"


page_indX (){
echo "processing $1 ..."
temp=`cat $5`

    button "$POINTS1" "$1" "$2"
    button "$POINTS2" "$1" "$3"
    button "$POINTS3" "$1" "$4"

    echo "</ul>

<ul>
$temp
</ul></body></html>
" >> "$1"

}

cd index

make_index (){
previous=index.html
pic=debut
i=0

for next in *"$1" ; do
    test -r "$next" || continue
    if [ "$pic" != "debut" ] ; then
	page_indX "../pages/$pic" "$previous" "index.html" "$next" "$pic"
	previous="$pic"
    fi
    pic="$next"
done
page_indX "../pages/$pic" "$previous" "index.html" "index.html" "$pic"

}

make_index hq.xhtml
make_index mq.xhtml

page_ind (){
echo "processing $1 ..."
temp=`cat $5`


    echo "$HEADER_HTML<ul>" > "$1"
    echo "
    <li><a href=\"$2\"> PREVIOUS </a></li>
    <li><a href=\"$3\"> UP </a></li>
    <li><a href=\"$4\"> NEXT </a></li>
    " >> "$1"  
 
    echo "</ul><ul>
$temp
</ul></body></html>
" >> "$1"

}

previous=index.html
pic=debut
i=-1
for next in *ie.html ; do
    test -r "$next" || continue
    if [ "$pic" != "debut" ] ; then
	page_ind "../pages/$pic" "$previous" "index.html" "$next" "$pic"
	previous="$pic"
    fi
    pic="$next"
    i=`expr $i + 1`
done
page_ind "../pages/$pic" "$previous" "index.html" "index.html" "$pic"


echo "</p></body></html>
" >> "../$INDEX"



