#!/bin/sh

# Par Julien MOREAU	( jmoreau@bigfoot.com )

nom_ind=index.html		# Nom du fichier à créer
nbps=2				# Nombre de paramètres souhaités (sans option)
nom_cmde=`basename $0`		# Nom de la commande
usage="Usage: $nom_cmde <repertoire> [<titre> <selection_de_fichiers>]"
usage=$usage"\n\tCréer l'$nom_ind du répertoire donné."	# Message d'aide

if test `uname` != "HP-UX" ; then e="-e" ; fi   # HP-UX echo case

if [ $# -lt 1 -o "$1" = '-h' ] ; then	# Vérification du nombre de paramètres
	echo $e $usage 1>&2 ; exit 1	# Message d'aide sur sortie erreurs
fi

rep=`basename $1`

if test ! -d $1 ; then		# Si le paramètre n'est pas un répertoire,
	echo "$1 n'est pas un répertoire !" 1>&2	# Message d'erreur
	exit 2						# et sortie
fi

if test ! -w $1 ; then	# Si le répertoire n'est pas modifiable,
	echo $e "Vous ne pouvez modifier le répertoire \"$1\" !" 1>&2
	exit 3			# et sortie
fi

cd $1 ; if [ $? -ne 0 ] ; then echo $e "Répertoire \"$1\" inaccessible !" ; fi
rep=`basename $PWD`

if [ $# -ge 2 ] ; then titre=$2 ; shift
else	case $rep in
		#bin)	titre="Scripts shell à action distante" ;;
		bin_pub|scripts)
			tar cvf 1_PIKSH.tar *.txt \
			  `find . -perm 755|grep -v -e '\./\.' -e ^\.$|sort`
			gzip *.tar
			titre="Shell script utilities" ;;
		News)	titre="PixEye's shell scripts News Letters" ;;
		*) echo $e "Quel en sera le titre ? \c" ; read titre
		   if test -z "$titre" ; then exit 1 ; fi
	esac
fi

shift

rm -f $nom_ind
user=`whoami`
exec >> $nom_ind

echo $e "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\""
echo $e "  \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"
echo $e "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"fr\" >"
echo $e "<head>"
echo $e "   <meta http-equiv=\"Content-Type\" content=\"text/html;\c"
echo $e " charset=iso-8859-15\" />"
echo $e "   <title>$titre</title>"
echo $e "   <meta name=\"AUTHOR\" content=\"`l2n $user`\" />"
echo $e "   <meta name=\"description\""
echo $e "\t content=\"Utilitaires ksh ( Korn-Shell ) pour UNIX ou LINUX\" />"
echo $e "   <meta name=\"keywords\" content=\"ksh, utilitaires, utilitaire,\c"
echo $e " Korn-Shell, korn, shell, UNIX, unix, LINUX, linux\" />"
echo $e "   <meta http-equiv=\"KEYWORDS\" content=\"ksh;utilitaires;utilitaire;\c"
echo $e "Korn-Shell;korn;shell;UNIX;unix;LINUX;linux\" />"
echo $e "  <style type=\"text/css\">"
echo $e "    BODY { background:#000; color:#fff; }"
echo $e "    H1, P { font-family:sans-serif; }"
echo $e "    P, DIV { font-size:12px; }"
echo $e "    TT { font-size:11px; }"
echo $e "    STRONG { background:#000; color:#0f8; }"
echo $e "    SMALL { background:#000; color:#aaa; }"
echo $e "    A:link { background:#000; color:#88f; }"
echo $e "    A:hover { background:#000; color:#ff8; }"
echo $e "    IMG { border:0; }"
echo $e "    .center { text-align:center; }"
echo $e "    .right { text-align:right; }"
echo $e "  </style>"
echo $e "</head>"
echo $e "<body>\n"

echo $e "<div class=\"center\"><h1>$titre</h1></div>\n"

echo $e "<p>C'est comme si vous y &eacute;tiez&nbsp;! &nbsp; Voici le contenu"
echo $e " d&eacute;taill&eacute; du r&eacute;pertoire\c"
echo $e "&nbsp;<strong\n >\"$rep\"</strong>&nbsp;:</p>\n"

echo $e "<!-- wmltable filter=\"*,9\" -->"
echo $e "<table>"
ls -lAF $* | tail -n+2 | grep -v @ | while read ligne ; do
	echo $ligne 1>&2		# Pour voir l'évolution du programme
	prog=`echo $ligne|sed -e 's/.* //'`
	echo $prog|grep -qie ^'index.htm' -e "ls" -e '\.old'$ -e '\.bak'$
	if [ $? -eq 1 ] ; then
		prefix=`echo $ligne | cut -d' ' -f-8`
		set x $prefix ; shift ; echo $e " <tr>\c"
		while [ $# -ge 1 ] ; do
			mess=$1
			if test $1 = total ; then shift ; mess="total $1" ; fi
			echo $e "\t<td><tt>$mess </tt></td>" ; shift
		done
		echo $e "\t<td><tt><big> &nbsp;\c"
		echo $prog|grep -q -e ^'\.mailist' -e ^'\.pathAdded' \
			-e ^'\.badusers'		# Filtres
		if [ $? -eq 0 ] ; then echo $e "$prog\c"
		elif test -n "$prog" ; then
			echo $e "<a href=\"$prog\c"	# Le lien
			if test $prog = "News/" ; then echo $e "$nom_ind\c" ; fi
			echo $e "$prog"|grep -qi -e tar\.gz$ -e zip$ -e txt$
			if [ $? -eq 0 ] ; then
				echo $e "\"\n\t\t><b>$prog</b></a>\c"
			else
				echo $e "\"\n\t\t>$prog</a>\c"
			fi
		fi
		echo "</big></tt></td></tr>"
	fi
done
echo "</table>"
echo "<!-- wmltable -->"

echo $e "\n<hr />\n"

echo $e "<div><a href=\"http://validator.w3.org/check/referer\"><img"
echo $e "  src=\"http://www.w3.org/Icons/valid-xhtml11\""
echo $e "  alt=\"Valid XHTML 1.1!\" height=\"31\" width=\"88\" /></a>"
echo $e "  <a href=\"http://jigsaw.w3.org/css-validator/\">"
echo $e "  <img style=\"width:88px;height:31px\""
echo $e "  src=\"http://jigsaw.w3.org/css-validator/images/vcss\""
echo $e "  alt=\"Valid CSS!\" /></a></div>\n"

echo $e "<div class=\"center\"><big>&lt; Retour au <a href=\"../\c"
if test $rep = "News" ; then echo $e "../\c" ; fi
echo $e "index.html\">niveau"
echo $e "pr&eacute;c&eacute;dent</a> &gt;</big></div>\n"

echo "<p class=\"right\"><small><i>"
echo $e "<script type=\"text/javascript\">"
echo $e " document.write(\"Derni&egrave;re modification de cette page&nbsp;:\c"
echo $e " \"+\n\tdocument.lastModified+\".\")"
echo $e "</script></i></small></p>\n"

echo $e "</body>\n</html>\n"

chmod a+r $nom_ind

exit 0		# Sortie sans erreur
