#!/bin/sh

maxniv=0
niveau=0
branche="$1"
maxbranche=""
tab=""

ndep() {
   echo "${tab}$1 ($niveau [$branche])"
   tab="  $tab"
   for package in `apt-cache depends $1 | awk '/  Depends: / {print $2}'`; 
do
     cycle="false"
     for i in $branche; do
       if [ "$i" == "$package" ]; then
         echo "${tab}$1 (cycle detected))"
         cycle="true"
         break
       fi
     done
     if [ $cycle = "false" ]; then
       branche="$branche ${package}"
       niveau=$((niveau+1))
       if [ $niveau -gt $maxniv ]; then
         maxniv=$niveau
         maxbranche="$branche"
       fi
       ndep $package
     fi
   done
   tab=`echo "$tab" | cut -c 3-`
   branche=`echo "$branche" | cut -d" " -f-$niveau`
   niveau=$((niveau-1))
}

ndep $1
echo -e "\nn($1)=${maxniv} ($maxbranche)"
exit 0