#!/bin/sh

# randomquote - tant donn un fichier de donnes avec une entre par
#   ligne, ce script choisit une ligne au hasard et l'affiche. Le mieux
#   est de l'employer en tant qu'appel SSI depuis une page Web.

awkscript="/tmp/randomquote.awk.$$"

if [ $# -ne 1 ] ; then
  echo "Syntaxe: $0 <nom du fichier de donnes>" >&2
  exit 1
elif [ ! -r "$1" ] ; then
  echo "Erreur: le fichier de citations $1 manque ou n'est pas lisible" >&2
  exit 1
fi

trap "/bin/rm -f $awkscript" 0

cat << "EOF" > $awkscript
BEGIN { srand(); }
      { s[NR] = $0 } 
END   { print s[randint(NR)] } 
function randint(n) { return int (n * rand() ) + 1 }
EOF

awk -f $awkscript < "$1"

exit 0
