#! /bin/sh -e

TITLE="Debian Reference"
PACKAGE="reference" # Yes this is not debian-reference
HTMLROOT="/usr/share/doc/Debian/reference"
QHTMLROOT="/usr/share/doc/Debian/quick-reference"
README="/usr/share/doc/debian-reference-common/README.gz"

# Echo Language name
echolang () {
  case "$1" in
    "en") echo "<dt>English</dt>";;
    "fr") echo "<dt>French</dt>";;
    "it") echo "<dt>Italian</dt>";;
    "es") echo "<dt>Spanish</dt>";;
    "de") echo "<dt>German</dt>";;
    "pl") echo "<dt>Polish</dt>";;
    "pt-br") echo "<dt>Portuguese (Brazil)</dt>";;
    "zh-tw") echo "<dt>Chinese (Traditional)</dt>";;
    "zh-cn") echo "<dt>Chinese (Simplified)</dt>";;
    "fi") echo "<dt>Finnish</dt>";;
    "ja") echo "<dt>Japanese</dt>";;
    "ko") echo "<dt>Korean</dt>";;
    "ru") echo "<dt>Russian</dt>";;
    "nl") echo "<dt>Nederlands</dt>";;
     *) echo "<dt>$1</dt>";;
  esac
}

# Echo index page
echoindex () {
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">" 
echo "<html>" 
echo "<head>" 
echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">"
echo "<title>${TITLE}</title>" 
echo "</head>" 
echo "<body>" 
echo "<hr>" 
echo "<h1>${TITLE}</h1>" 
if [ -n "${LANGS}" ]; then
  echo "<hr>" 
  echo "<p>" 
  echo "<dl>" 
  for lang in ${LANGS}; do
    echolang "$lang"
    echo "<dd>"
    echo "<a href=\"index.$lang.html\">HTML</a>"
    echo ", <a href=\"$PACKAGE.$lang.txt.gz\">plain text</a>"
    echo ", <a href=\"$PACKAGE.$lang.ps.gz\">PS</a>"
    echo ", <a href=\"$PACKAGE.$lang.pdf.gz\">PDF</a>"
    echo "</dd>"
  done
  echo "</dl>"
  echo "</p>"
fi
echo "<hr>" 
echo "<p>If your favorite language is not found in this list,"
echo "please install the corresponding <tt>debian-reference-*</tt> package."
echo "Currently, English (original), French, German, Italian, Spanish,"
echo "Polish, Portuguese (Brazil), Chinese (Traditional), and "
echo "Chinese (Simplified) are supported.</p>"
echo "<p>Some browsers may not be setup to read compressed plain text, "
echo "Postscript, and PDF links above. Use appropriate file viewer system"
echo "to read them directly from $HTMLROOT.</p>"
echo "<p>For your handy short hard copy reference, excerpt versions "
echo "are available as <tt>quick-reference-*</tt> package and"
echo "files are installed into $QHTMLROOT.</p>"
echo "<p>See <a href=\"$README\">README.gz<a> for more help and information.</p>"
echo "</body>" 
echo "</html>"
}

# Always remove index page and move to the $HTMLROOT directory
if [ -d $HTMLROOT ]; then
  cd $HTMLROOT
  # index.html can be symlink or file
  rm -f index.html
else
  # You may have removed the $HTMLROOT directory
  exit 0
fi
# Always sort language with English as top.
LANGS="$(ls -1 --color=never index.*.html 2> /dev/null | \
	sed 's/index\.//g;s/\.html//g;s/en/00/' | \
	sort | sed 's/00/en/')"

HTML_INSTALLED=$(echo "${LANGS}" | wc -w)

# Create single index page for menu:
if [ $HTML_INSTALLED = 0 ]; then
  echoindex > index.html
elif [ $HTML_INSTALLED = 1 ]; then
  # one file only, link it
  ln -sf index.*.html index.html
else
  echoindex > index.html
fi

exit 0
