#!/bin/sh
#
# Script de création de la liste des fichiers mp3 en HTML.

VERT="33FF33"
ROUGE="FF1111"
today=`date '+%A %d %B %Y'`

DEBUG=0
if [ "$1" = "-d" ]; then
  DEBUG=1
  shift
fi

if [ "$1" = "-o" ]; then
  FORMAT="ogg"
else
  FORMAT="mp3"
fi

TITRE=`echo "$FORMAT" | tr [a-z] [A-Z]`

cat << EOF
<!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 fichiers $TITRE</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 FICHIERS $TITRE</h1>
        <br>
      </div>
    </font>

    <font face="Verdana,Arial,Helvetica" size="-1">
      <div align="center">
        <table width="90%" cellspacing="1" cellpadding="2" border="0">
          <tr bgcolor="CFCFCF">
            <td><b>ARTISTE - TITRE</b></td>
            <td><b><div align="center">DURÉE</div></b></td>
            <td><b><div align="center">BITS</div></b></td>
            <td><b><div align="right">TAILLE</div></b></td>
          </tr>
EOF

tf=`ls *.$FORMAT | wc -l`
echo "Nombre de fichiers à traiter : $tf" >> /dev/stderr
n=0
t=0
totals=0
totalm=0
totalt=0
fond="E5E5E5"
for i in *.$FORMAT; do
  n=`expr $n + 1`
  if [ $DEBUG = 1 ]; then
    echo "-+- DEBUG MODE ($DEBUG) -+-" >> /dev/stderr
    echo "Fichier : [$i], num = $n" >> /dev/stderr
  else
    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
      t=`echo "$t + $n" | bc`
      n=0
      echo " $t" >> /dev/stderr
    fi
  fi
  nom=`basename "$i" .mp3`
  if [ "$FORMAT" = "mp3" ]; then
    min=`mp3info -f "%m" "$i"`
    sec=`mp3info -f "%s" "$i"`
    bits=`mp3info -f "%b" "$i"`
  else
    bits=`ogginfo "$i" | grep nominal | cut -d "=" -f 2`
    bits=`echo "$bits / 1000" | bc`
    duree=`ogginfo "$i" | grep playtime | cut -d "=" -f 2`
    min=`echo "$duree" | cut -d ":" -f 1`
    sec=`echo "$duree" | cut -d ":" -f 2`
  fi
  if [ $bits -gt 128 ]; then
    couleur="<font color=\"$VERT\">"
    fincouleur="</font>"
  elif [ $bits -lt 128 ]; then
    couleur="<font color=\"$ROUGE\">"
    fincouleur="</font>"
  else
    couleur=""
    fincouleur=""
  fi
  totals=`echo "$totals + $sec" | bc`
  totalm=`echo "$totalm + $min" | bc`
  if [ $min -lt 10 ]; then
    min="0$min"
  fi
  if [ $sec -lt 10 ]; then
    sec="0$sec"
  fi
  taille=`ls -lh "$i" | awk '{print $5}' | sed -e 's/M//'`
  point=`echo $taille | grep "\."`
  if [ "$point" = "" ]; then
    taille="$taille.0"
  fi
  totalt=`echo "$totalt + $taille" | bc`
  if [ "$fond" = "E5E5E5" ]; then
    fond="EBEBEB"
  else
    fond="E5E5E5"
  fi
  echo "       <tr bgcolor=\"$fond\">"
  echo "         <td>$nom</td>"
  echo "         <td><div align=\"center\">$min:$sec</div></td>"
  echo "         <td><div align=\"center\">$couleur$bits$fincouleur</div></td>"
  echo "         <td><div align=\"right\">$taille Mo</div></td>"
  echo "       </tr>"
done

echo "" >> /dev/stderr
mis=`echo "$totals / 60" | bc`
totals=`echo "$totals - $mis * 60" | bc`
totalm=`echo "$totalm + $mis " | bc`
totalh=`echo "$totalm / 60" | bc`
totalm=`echo "$totalm - $totalh * 60" | bc`
if [ $totalm -lt 10 ]; then
  totalm="0$totalm"
fi
if [ $totals -lt 10 ]; then
  totals="0$totals"
fi
echo "Durée totale  = $totalh:$totalm:$totals" >> /dev/stderr
echo "Taille totale = $totalt Mo" >> /dev/stderr

cat << EOF
       <tr>
         <td>&nbsp;</td>
       </tr>
       <tr>
         <td><b>TOTAL</b></td>
         <td><b><div align="center">$totalh:$totalm:$totals</div></b></td>
         <td>&nbsp;</td>
         <td><b><div align="right">$totalt Mo</div></b></td>
       </tr>
     </table>
     Mis à jour le $today
    </font>
  </body>
</html>
EOF
