#!/bin/bash
set -e
set -x
set -u

APP=$1

PACKAGE=$(apt-cache search -n '^'"$APP"'$' |cut -f1 -d' ')

if [ -z "$PACKAGE" ];
then
  PACKAGE=$(apt-file search -F -l "/usr/bin/$APP")
fi

if [ -z "$PACKAGE" ];
then
  PACKAGE=$(apt-file search -F -l "/bin/$APP")
fi

if [ -z "$PACKAGE" ];
then
  PACKAGE=$(apt-file search -F -l "/sbin/$APP")
fi

if [ -z "$PACKAGE" ];
then
  PACKAGE=$(apt-file search -F -l "/usr/sbin/$APP")
fi

if [ -z "$PACKAGE" ];
then
  echo "Paquet non-trouvé"
  exit 1
fi

SECTION=$(apt-cache show $PACKAGE | sed -n '/^Section/s/^Section: //p')

declare -A paths

paths["unknown"]="doc:autres"
paths["virtual"]="doc:autres"
paths["admin"]="doc:systeme"
paths["alien"]="doc:systeme"
paths["base"]="doc:programmation"
paths["cli-mono"]="doc:programmation"
paths["comm"]="doc:reseau"
paths["database"]="doc:programmation"
paths["debug"]="doc:systeme"
paths["devel"]="doc:programmation"
paths["editors"]="doc:editeurs"
paths["electronics"]="doc:electronique"
paths["embedded"]="doc:electronique"
paths["fonts"]="doc:media"
paths["games"]="doc:jeux"
paths["gnome"]="doc:environnements:gnome"
paths["gnu-r"]="doc:sciences"
paths["gnustep"]="doc:programmation"
paths["graphics"]="doc:media"
paths["hamradio"]="doc:media"
paths["haskell"]="doc:programmation"
paths["httpd"]="doc:reseau"
paths["interpreters"]="doc:programmation"
paths["java"]="doc:programmation"
paths["kernel"]="doc:systeme"
paths["kde"]="doc:environnements:kde"
paths["libdevel"]="doc:programmation"
paths["libs"]="doc:programmation"
paths["lisp"]="doc:programmation"
paths["localization"]="doc:systeme"
paths["mail"]="doc:reseau"
paths["math"]="doc:sciences"
paths["misc"]="doc:autres"
paths["net"]="doc:reseau"
paths["news"]="doc:reseau"
paths["ocaml"]="doc:programmation"
paths["oldlibs"]="doc:systeme"
paths["otherosfs"]="doc:systeme"
paths["perl"]="doc:programmation"
paths["php"]="doc:programmation"
paths["python"]="doc:programmation"
paths["ruby"]="doc:programmation"
paths["science"]="doc:sciences"
paths["shells"]="doc:programmation"
paths["sound"]="doc:media"
paths["tex"]="doc:sciences"
paths["text"]="doc:editeurs"
paths["utils"]="doc:systeme"
paths["video"]="doc:media"
paths["vcs"]="doc:systeme"
paths["web"]="doc:reseau"
paths["x11"]="doc:environnements:x11"
paths["xfce"]="doc:environnements:xfce"
paths["zope"]="doc:programmation"



NEWPATH=${paths[${SECTION}]}":"$APP

echo "Page=$APP Paquet=$PACKAGE Section=$SECTION NouveauNom=$NEWPATH"