#!/bin/bash
#                                                                    #
# Copyright 2006, Michelle Konzack All rights reserved.              #
#                                                                    #
# Redistribution and use in source and binary forms, with or without #
# modification, are permitted provided that the following conditions #
# are met:                                                           #
#                                                                    #
# 1. Redistributions of source code  must retain the above copyright #
#    notice, this list of conditions and the following disclaimer.   #
#                                                                    #
# 2. Redistributions  in  binary  form   must  reproduce  the  above #
#    copyright  notice, this  list of  conditions and  the following #
#    disclaimer in the documentation and/or other materials provided #
#    with the distribution.                                          #
#                                                                    #
# 3. Neither  the name  of Michelle  Konzack  nor the  names of  its #
#    contributors may be used to endorse or promote products derived #
#    from this software without specific prior written permission.   #
#                                                                    #
# THIS  SOFTWARE IS  PROVIDED BY  MICHELLE KONZACK  AND CONTRIBUTORS #
# ``AS IS'  AND ANY  EXPRESS OR  IMPLIED WARRANTIES,  INCLUDING, BUT #
# NOT  LIMITED TO,  THE  IMPLIED WARRANTIES  OF MERCHANTABILITY  AND #
# FITNESS  FOR A  PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT #
# SHALL  JULIE  HAUGH OR  CONTRIBUTORS  BE  LIABLE FOR  ANY  DIRECT, #
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES #
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR #
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) #
# HOWEVER  CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN #
# CONTRACT,  STRICT  LIABILITY,  OR TORT  (INCLUDING  NEGLIGENCE  OR #
# OTHERWISE) ARISING  IN ANY WAY  OUT OF  THE USE OF  THIS SOFTWARE, #
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                 #
#                                                                    #

VERSION=0.4.5

. gettext.sh
export TEXTDOMAIN=tddebidate
export TEXTDOMAINDIR=/usr/share/locale

let LEN=`echo $VERSION |wc --max-line-length |sed "s/\ //g"`
if [ $LEN -eq 5 ] ; then SPC=" " ; else SPC="" ; fi


####################################################################
#  
#  Definition for the help view
#  
####################################################################
function view_help()
{
  echo    "tddebidate     $VERSION$SPC     Coded by Michelle Konzack <linux4michelle@freenet.de>"
  echo -n "                          "
  gettext "This  BASH  script  is  under  GNU  GPL  version  2.0"
  echo -e "\n"
  gettext "Usage:  tddebidate [ -V | -h ] | [ -n ] [ -v ] [ -x ]"
  echo -e "\n"
  echo -n "-V      "
  gettext         "Print the version and exits."
  echo
  echo -n "-h      "
  gettext         "Print this help."
  echo -e "\n"
  echo -n "-n      "
  gettext         "Do not show the header about the FQDN and how many installed packages."
  echo
  echo -n "        "
  gettext         "(has no effect in combination with -x)"
  echo -e "\n"
  echo -n "-v      "
  gettext         "Output additional version infos but depending on your CPU power"
  echo
  echo -n "        "
  gettext         "heavyly slows down the process."
  echo -e "\n"
  echo -n "-x      "
  gettext          "Force output under X to Xdialog."
  echo
  exit 0
}

####################################################################
#  
#  Definition for the version view
#  
####################################################################
function view_version()
{
  echo "tddebidate     $VERSION$SPC     Coded by Michelle Konzack <linux4michelle@freenet.de>"
  echo -n "                          "
  gettext "This  BASH  script  is  under  GNU  GPL  version  2.0"
  echo
  exit 0
}

####################################################################
#  
#  Select options
#  
####################################################################
NOHEAD=1
DLGW=55
PVER=1
XMSG=1
while getopts "hVnvx" FLAG ; do
  case $FLAG in
    ################################################################
    #  Show the help and exit
    ################################################################
    h) view_help        ;;
    ################################################################
    #  Show the version and exit
    ################################################################
    V) view_version     ;;
    ################################################################
    #  Show no Header
    ################################################################
    n) NOHEAD=0         ;;
    ################################################################
    #  Show additional the version informations
    ################################################################
    v) PVER=0           ;
       DLGW=100         ;;
    ################################################################
    #  Set output und er X to xmessage
    ################################################################
    x) XMSG=0           ;;
  esac
done


####################################################################
#  
#  
#  
####################################################################
if [ "$XMSG" == "0" ] ; then
  DP=`w -h $USER |tr -s ' ' |cut -d " " -f3 |egrep ":[0-9]" |head -n 1`
  if [ $? -ne 0 ] ; then
    echo -n "tddebidate: "
    gettext "error 10: it seems you are not running the x-window-system."
    echo
    exit 10
  else
    which Xdialog >/dev/null 2>&1
    if [ $? -eq 1 ] ; then
      echo -n "tddebidate: "
      gettext "error 20: xdialog is required if you want to use the option -x."
      echo
      exit 20
    fi
  fi
  if [ "$TERM" == "linux" ] ; then
    echo -n "tddebidate: "
    gettext "error 30: You are running 'tddebidate' with the option -x on a virtual terminal."
    exit 30
  fi
fi


####################################################################
#  
#  
#  
####################################################################
if [ "$PVER" == "0" ] || [ "$XMSG" == "0" ] ; then
  TMPFILE1=`mktemp -t tddebidate.XXXXXX`
fi

####################################################################
#  
#  
#  
####################################################################
function get_packages()
{
  LC_TIME=C \
  ls -l --reverse -g --no-group --sort=time --time=ctime --full-time \
     /var/lib/dpkg/info/ |grep "\.list" |\
                          sed 's/\.list//;s/\.000000000.*\ / /' |\
                          sed --regexp-extended 's/^.{22}//'
}


####################################################################
#  
#  
#  
####################################################################
let PACKS=`ls /var/lib/dpkg/info/*.list |wc --lines |sed s/\ //g`
COMPUTER=`hostname --fqdn`


####################################################################
#  
#  
#  
####################################################################

if [ "$NOHEAD" == "1" ] ; then
  if [ "$XMSG" == "1" ] ; then
    eval_gettext "The Computer '\$COMPUTER' has \$PACKS packages installed."
    echo
  fi
fi


####################################################################
#  
#  
#  
####################################################################
if [ "$PVER" == "1" ] ; then
  if [ "$XMSG" == "1" ] ; then
    get_packages
  else
    get_packages >>$TMPFILE1
  fi

else
  if [ "$XMSG" == "0" ] ; then
    let PERC=10000/$PACKS
    let WIDTH=`get_packages |wc --max-line-length |sed "s/\ //g"`+1
  else
    let WIDTH=45
  fi

  function makelist()
  {
  let Z=0
  (get_packages) |
  while read OUT ; do
    if [ "$XMSG" == "0" ] ; then
      let Z=$Z+$PERC
      let Y=$Z/100
      echo $Y
    fi
    PACK=`echo "$OUT" |sed s/^.*\ //`
    VERS=`apt-cache policy $PACK |grep "\*\*\*" |cut -d " " -f 3`
    PLEN=`echo "$OUT" |wc --max-line-length |sed s,\ ,,g`
    for X in `seq $PLEN $WIDTH` ; do
      OUT="$OUT "
    done
    if [ "$XMSG" == "1" ] ; then
      echo "$OUT $VERS" 
    else
      echo "$OUT $VERS" >>$TMPFILE1
    fi
  done
  }

  if [ "$XMSG" == "1" ] ; then
    makelist
  else
    TEXTGAUGE=`eval_gettext "Please wait.\n\nYou have installed \$PACKS packages and 'tddebidate' is looking for the package versions which take some time depending on the CPU speed of your computer."`
    makelist |DISPLAY=$DP Xdialog --title "tddebidate" \
                                  --screen-center --wrap \
                                  --gauge "$TEXTGAUGE" 10 55
    if [ "$?" != "0" ] ; then
      rm -f $TMPFILE1
      exit 99
    fi
  fi
fi

####################################################################
#  
#  
#  
####################################################################
if [ "$XMSG" == "0" ] ; then
  TEXT=`eval_gettext "The Computer '\$COMPUTER' has \$PACKS packages installed."`
  DISPLAY=$DP Xdialog --title "tddebidate" --backtitle "$TEXT" \
                      --screen-center --wrap --no-cancel --fixed-font \
                      --textbox $TMPFILE1 30 $DLGW &
  disown
  sleep 2
fi

rm -f $TMPFILE1

exit 0
