#!/bin/sh
#
# Script pour générer des pages HTML en fonction des fiches de bandes dessinées


# PARAMÊTRES
#
DEBUG=0
if [ "$1" = "-d" -o "$1" = "-debug" ]; then
  DEBUG=1
  shift
fi

PRETS=0
if [ "$1" = "prets" ]; then
  PRETS=1
  shift
fi

PUBLIC=0
if [ "$1" = "all" ]; then
  PUBLIC=1
  shift
fi

if [ "$1" != "" ]; then
  echo "update [-debug] [prets] [all]"
  exit
fi

# DÉFINITIONS
#
TODAY=`date '+%A %d %B %Y'`
FREP=fiches
NREP=prochains
PREP=emprunts
DREP=html
TMP=/tmp/parties-bds
serie=""
genre=""
auteurs=""
editeur=""
numeros=""
destfiche=""
index=""
fichegenre=""
nbalbums=0
totalalbums=0
totalseries=0

coultitre="CFCFCF"
coulligne1="EBEBEB"
coulligne2="E5E5E5"


# CRÉATION DE LA FICHE HTML POUR LA SÉRIE
#
do_file () {
  if [ $DEBUG = 1 ]; then
    echo "===> Traitement de $fiche :"
    echo "  Série   : $serie"
    echo "  Genre   : $genre"
    echo "  Auteurs : $auteurs"
    echo "  Éditeur : $editeur"
    echo "  Numéros : $numeros"
  fi
    cat << EOF > $destfiche
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15">
    <meta name="Author" content="Thomas Nemeth">
    <meta name="keywords" content="fiche BD $auteurs $serie">
    <title>BD - $serie</title>
  </head>
  <body text="#000000" bgcolor="#FFFFFF" link="#0000EF" vlink="#51188E" alink="#FF0000">
    <font face="Verdana,Arial,Helvetica">
      <div align="center">
        <h1><b>$serie</b></h1>
      </div>
    </font>
    <hr>
    <font face="Verdana,Arial,Helvetica" size="-1">
      <div align="left">
        <table width="90%" cellspacing="1" cellpadding="2" border="0">
          <tr>
            <td width="10%"><b>Auteur(s)</b></td>
            <td width="1%"><b>:</b></td>
            <td>$auteurs</td>
          </tr>
          <tr>
            <td width="10%"><b>Éditeur</b></td>
            <td width="1%"><b>:</b></td>
            <td>$editeur</td>
          </tr>
          <tr>
            <td width="10%"><b>Numéros</b></td>
            <td width="1%"><b>:</b></td>
            <td>$numeros</td>
          </tr>
          <tr>
            <td width="10%"><b>Genre</b></td>
            <td width="1%"><b>:</b></td>
            <td>$genre</td>
          </tr>
          <tr>
            <td width="10%"><b>Fiche</b></td>
            <td width="1%"><b>:</b></td>
            <td>$fiche</td>
          </tr>
        </table>
        <br>
      </div>
      <hr width="36%">
      <p>
        <br>
        <b>TITRES DISPONIBLES&nbsp;:</b>
        <br>
      </p>
      <div align="left">
        <table width="90%" cellspacing="1" cellpadding="2" border="0">
          <tr bgcolor="$coultitre">
            <td><div align="center">NUMÉRO</div></td>
            <td>TITRE</td>
          </tr>
EOF
  nbalbums=0
  fond="$coulligne2"
  for num in $numeros; do
    nbalbums=`expr $nbalbums + 1`
    album=`grep "^$num : " $fiche | awk -F " : " '{print $2}'`
    if [ "$fond" = "$coulligne2" ]; then
      fond="$coulligne1"
    else
      fond="$coulligne2"
    fi
    echo "          <tr bgcolor=\"$fond\">" >> $destfiche
    echo "            <td width=\"10%\">" >> $destfiche
    echo "              <div align=\"center\">$num</div>" >> $destfiche
    echo "            </td>" >> $destfiche
    echo "            <td>" >> $destfiche
    echo "              $album" >> $destfiche
    echo "            </td>" >> $destfiche
    echo "          </tr>" >> $destfiche
  done
  echo "        </table>" >> $destfiche
  echo "      </div>" >> $destfiche
  echo "    <hr>" >> $destfiche
  if [ "$fichealt" != "" ]; then
    echo "      <p>" >> $destfiche
    if [ "$rep" = "$NREP" ]; then
      echo "        <a href=\"../$fichealt\">Albums déjà achetés</a>." >> $destfiche
    else
      echo "        <a href=\"../$fichealt\">Albums prévus à l'achat</a>." >> $destfiche
    fi
    echo "      </p>" >> $destfiche
  fi
  echo "    <br>" >> $destfiche
  echo "    <table width=\"98%\">" >> $destfiche
  echo "      <tr>" >> $destfiche
  echo "        <td>" >> $destfiche
  echo "          <font size=\"-1\">" >> $destfiche
  echo "            Fiche mise à jour le $TODAY." >> $destfiche
  echo "          </font>" >> $destfiche
  echo "        </td>" >> $destfiche
  echo "        <td>" >> $destfiche
  echo "          <div align=\"right\">" >> $destfiche
  echo "            <a href=\"index.html\">Index</a>" >> $destfiche
  echo "          </div>" >> $destfiche
  echo "        </td>" >> $destfiche
  echo "      </tr>" >> $destfiche
  echo "    </table>" >> $destfiche
  echo "    </font>" >> $destfiche
  echo "  </body>" >> $destfiche
  echo "</html>" >> $destfiche
}


# TRAITEMENT DE LA LISTE DES FICHES ET DES ÉLÉMENTS DE L'INDEX
#
do_list () {
    nomfiche=`basename $fiche`
    if [ -f $ALT/$nomfiche ]; then
      fichealt=$ALT/$nomfiche.html
    else
      fichealt=""
    fi
    serie=`grep SÉRIE $fiche | awk -F" : " '{print $2}'`
    genre=`grep GENRE $fiche | awk -F" : " '{print $2}'`
    auteurs=`grep AUTEURS $fiche | awk -F" : " '{print $2}'`
    editeur=`grep ÉDITEUR $fiche | awk -F" : " '{print $2}'`
    numeros=`grep NUMÉROS $fiche | awk -F" : " '{print $2}'`
    destfiche=$DREP/$fiche.html
    nomdest=$nomfiche.html
    fichegenre=$TMP/$genre.html
    touch $fichegenre
    x=`wc -l $fichegenre | awk '{print $1}' | sed -e 's/.*\(.\)$/\1/'`
    do_file
    totalalbums=`echo "$totalalbums + $nbalbums" | bc`
    if [ "$x" = "0" -o "$x" = "2" -o "$x" = "4" -o "$x" = "6" -o "$x" = "8" ]; then
      fond="$coulligne1"
    else
      fond="$coulligne2"
    fi
    echo "          <tr bgcolor=\"$fond\"><td><a href=\"$nomdest\">$serie</a> <i>($nbalbums)</i></td><td>$auteurs</td><td>$editeur</td></tr>" >> $fichegenre
}


# TRAITEMENT DES INDEX
#
do_idx () {
  totalseries=0
  totalalbums=0
  n=0
  mkdir $DREP/$rep
  index=$DREP/$rep/index.html
  if [ "$rep" = "$FREP" ]; then
    ALT=$NREP
  else
    ALT=$FREP
  fi
  cat << EOF > $index
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15">
    <meta name="Author" content="Thomas Nemeth">
    <title>Liste des bandes dessinées</title>
  </head>
  <body text="#000000" bgcolor="#FFFFFF" link="#0000EF" vlink="#51188E" alink="#FF0000">
    <font face="Verdana, Arial,Helvetica">
      <div align="center">
        <br>
EOF
  if [ "$rep" = "$FREP" ]; then
    echo "        <h1>LISTE DES BANDES DESSINÉES</h1>" >> $index
  else
    echo "        <h1>LISTE DES BANDES DESSINÉES PRÉVUES À L'ACHAT</h1>" >> $index
  fi
  cat << EOF >> $index
        <br>
      </div>
    </font>

    <font face="Verdana, Arial,Helvetica" size="-1">
      <div align="center">
        <br>
        <table width="98%" cellspacing="1" cellpadding="2" border="0">
EOF
  # Création des fiches et des éléments de l'index
  for fiche in $rep/*; do
    n=`expr $n + 1`
    echo -n "." >> /dev/stderr
    if [ $n = 10 -o $n = 20 -o $n = 30 -o $n = 40 ]; then
      echo -n " " >> /dev/stderr
    fi
    if [ $n = 50 ]; then
      totalseries=`echo "$totalseries + $n" | bc`
      n=0
      echo " $totalseries" >> /dev/stderr
    fi
    do_list
  done
  # Insertion des genres.
  for genre in $TMP/*.html; do
    nomgenre=`basename $genre .html`
    totalgenre=`wc -l $genre | awk '{print $1}'`
    if [ $PUBLIC = 1 -a "$genre" != "$TMP/Érotique.html" -o $PUBLIC = 0 ]; then
      cat << EOF >> $index
        <tr><td>&nbsp;</td></tr>
        <tr><td>&nbsp;</td></tr>
        <tr>
          <td bgcolor="$coultitre">
            <div align="center">$nomgenre (<i>$totalgenre</i>)</div>
          </td>
        </tr>
        <tr><td>&nbsp;</td></tr>
        <tr bgcolor="$coultitre">
          <td>SÉRIE <i>(nb albums)</i></td>
          <td>AUTEUR(S)</td>
          <td>ÉDITEUR</td>
        </tr>
EOF
      cat $genre >> $index
    fi
  done
  echo "" >> /dev/stderr
  if [ $n != 0 ]; then
    totalseries=`echo "$totalseries + $n" | bc`
  fi
  echo "Nombre de séries   ($rep) : $totalseries" >> /dev/stderr
  echo "Nombre de d'albums ($rep) : $totalalbums" >> /dev/stderr
  ts=`echo "$ts + $totalseries" | bc`
  ta=`echo "$ta + $totalalbums" | bc`
  cat << EOF >> $index
        </table>
      </div>
      <p>
        <br>&nbsp;
        <br>&nbsp;
      </p>

      <blockquote>
        <div align="left">
          NOMBRE TOTAL DE SÉRIES&nbsp;: <b>$totalseries</b><br>
          NOMBRE TOTAL DE BDS&nbsp;: <b>$totalalbums</b><br>
        </div>
      </blockquote>
    </font>
    <hr>
    <br>
    <table width="98%">
      <tr>
        <td>
          <font size="-1">
            Fiche mise à jour le $TODAY.
          </font>
        </td>
        <td>
          <div align="right">
            <a href="../index.html">Page précédente</a>
          </div>
        </td>
      </tr>
    </table>
  </body>
</html>
EOF
  rm -rf $TMP/*
}


# TRAITEMENTS DES LISTES DE BANDES DESSINÉES & SÉRIES
#
do_all () {
  ts=0
  ta=0
  rm -rf html/*
  for rep in $FREP $NREP; do
    do_idx
  done
}


# TRAITEMENT DES PRÊTS
#
do_prets () {
  mkdir $DREP/$PREP 2> /dev/null
  nbemprunts=`ls $PREP | wc -l | awk '{print $1}'`
  index=$DREP/$PREP/index.html
  cat << EOF > $index
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15">
    <meta name="Author" content="Thomas Nemeth">
    <title>Liste des bandes dessinées empruntées</title>
  </head>
  <body text="#000000" bgcolor="#FFFFFF" link="#0000EF" vlink="#51188E" alink="#FF0000">
    <font face="Verdana, Arial,Helvetica">
      <div align="center">
        <br>
        <h1>LISTE DES BANDES DESSINÉES EMPRUNTÉES</h1>
        <br>
      </div>

      <blockquote>
        <p>
EOF
  if [ $nbemprunts -gt 0 ]; then
    for pret in $PREP/*; do
      nompret=`basename $pret`
      destpret=$DREP/$pret.html
      cat << EOF > $destpret
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="Author" content="Thomas Nemeth">
    <meta name="keywords" content="Prêts de bandes dessinées">
    <title>$nompret</title>
  </head>
  <body text="#000000" bgcolor="#FFFFFF" link="#0000EF" vlink="#51188E" alink="#FF0000">

    <div align="center"><br><h1>$nompret</h1><br></div>

    <p>
      &nbsp;<br>
      &nbsp;<br>
    </p>

    <blockquote>
      <p>
        <table width="98%">
          <tr bgcolor="$coultitre">
            <td width="50%">SÉRIE</td>
            <td width="50%">NUMÉROS</td>
          </tr>
EOF
      fond="$coullinge2"
      cat $pret | while read ligne; do
        if [ "$fond" = "$coulligne2" ]; then
          fond="$coulligne1"
        else
          fond="$coulligne2"
        fi
        serie=`echo "$ligne" | sed -e 's/^\(.*\)§.*/\1/'`
        numeros=`echo "$ligne" | sed -e 's/^.*§\(.*\)/\1/'`
        echo "          <tr bgcolor="$fond">" >> $destpret
        echo "            <td width=\"50%\">$serie</td>" >> $destpret
        echo "            <td width=\"50%\"><i>$numeros</i></td>" >> $destpret
        echo "          </tr>" >> $destpret
      done
      cat << EOF >> $destpret
        </table>
      </p>
    </blockquote>
    <hr>
    <br>
    <table width="98%">
      <tr>
        <td>
          <font size="-1">
            Fiche mise à jour le $TODAY.
          </font>
        </td>
        <td>
          <div align="right">
            <a href="index.html">Index</a>
          </div>
        </td>
      </tr>
    </table>
  </body>
</html>
EOF
      echo "          <a href=\"$nompret.html\">$nompret</a><br>" >> $index
    done
  else
      echo "          AUCUN EMPRUNT EN COURS...<br>" >> $index
  fi
  cat << EOF >> $index
        </p>
      </blockquote>
    </font>
    <hr width="100%">
    <br>
    <table width="98%">
      <tr>
        <td>
          <font size="-1">
            Fiche mise à jour le $TODAY.
          </font>
        </td>
        <td>
          <div align="right">
            <a href="../index.html">Page précédente</a>
          </div>
        </td>
      </tr>
    </table>
  </body>
</html>
EOF
}


# TRAITEMENT DES LISTES
#
mkdir $TMP
if [ $PRETS = 0 ]; then
  do_all
fi
do_prets
index=html/index.html
cat << EOF > $index
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15">
    <meta name="Author" content="Thomas Nemeth">
    <title>Liste des bandes dessinées</title>
  </head>
  <body text="#000000" bgcolor="#FFFFFF" link="#0000EF" vlink="#51188E" alink="#FF0000">
    <font face="Verdana, Arial,Helvetica">
      <div align="center">
        <br>
        <h1>LISTE DES BANDES DESSINÉES</h1>
        <br>
      </div>

      <blockquote>
        <p>
          <ol>
          <li><a href="fiches/index.html">Albums déjà achetés</a>.</li>
          <li><a href="prochains/index.html">Achats prévus</a>.</li>
          <li><a href="emprunts/index.html">Albums empruntés</a>.</li>
        </p>
      </blockquote>
    </font>
    <hr>
    <br>
    <table width="98%">
      <tr>
        <td>
          <font size="-1">
            Fiche mise à jour le $TODAY.
          </font>
        </td>
        <td>
          <div align="right">
            <a href="../index.html">Page précédente</a>
          </div>
        </td>
      </tr>
    </table>
  </body>
</html>
EOF
rm -rf $TMP
