# openmamba update script
# Copyright (c) 2011 by Silvan Calarco <silvan.calarco@mambasoft.it>
#
# Released under the terms of the GNU GPLv3 License

VERSION=0.2
TEXTDOMAIN=openmamba-update
TEXTDOMAINDIR=/usr/share/locale/
INSTALL_DATE=`date +%Y%m%d-%H%M`
LOG_FILE=/var/log/openmamba-update-$INSTALL_DATE.log
KERNEL_RELEASE=`uname -r`
PROGRESS_POS=0
PKGGROUPS_DB=/usr/share/openmamba/pkggroups.db
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/mamba-tempfile

. $PKGGROUPS_DB

function pkg_parser() {
   while read line; do
      echo $line
   done
}

function pkg_tool() {
   sudo smart $@ | pkg_parser
}

echo $"\
openmamba update script - version $VERSION
Copyright (c) 2011 by Silvan Calarco <silvan.calarco@mambasoft.it>
"

script_ver=1.0
script_name="$(basename $0 2>/dev/null)"

function usage() {

echo $"Usage: $script_name [OPTION][groupname ..]

Options:
  -f, --full
    Perform a full update
  -h, --help
    Display this message.
  -l, --list
    List available groups and exit
  -v, --verbose
    Produce mode verbose output
  -y, --assume-yes
    Assume yes to all answers"
}

OPTS=`LANG=C getopt -o hlvyf \
   --long help,list,verbose,assume-yes,full \
   -n "$script_name" -- "$@"`
[ $? = 0 ] || exit 1

eval set -- "$OPTS"

while :; do
   case "$1" in
   -f|--full) full_update_mode=1 ;;
   -h|--help)
      usage; exit 0 ;;
   -l) list_mode=1 ;;
   -v) verbose_mode=1 ;;
   -y) assume_yes=1 ;;
   --) shift; break ;;
   *) echo "\
(bug) -- $script_name: \`getopt' error: bad command \`$1'" ;;
   esac
   shift
done

while [ "$1" ]; do
   installnames="$installnames $1"
   shift
done

[ "$list_mode" ] && echo $"Available installation groups (*=installed,+=needs update):"
for i in `seq 1 ${#PKG_GROUPS[*]}`; do
   eval instpkgs=(\$`echo ${PKG_GROUPS[$i-1]}`)
   totpkgs=`expr $totpkgs + ${#instpkgs[*]}`
   checkinstalled=(`rpm -q --whatprovides ${instpkgs[*]} | grep -v 'no package provides'`)
   if [ ${#checkinstalled[*]} -eq 0 ]; then
      [ "$list_mode" ] && echo -n "  "
   elif [ ${#checkinstalled[*]} -lt ${#instpkgs[*]} ]; then
      [ "$list_mode" ] && echo -n "+ "
      needupdategroups="${needupdategroups} `expr $i-1`"
   else
      [ "$list_mode" ] && echo -n "* "
   fi
   if [ "$list_mode" ]; then
      echo -n -e "${PKG_NAMES[$i-1]}"
      for j in `seq 30 -1 ${#PKG_NAMES[$i-1]}`; do
         echo -n " "
      done
      echo -e "${PKG_DESC[$i-1]} (${#checkinstalled[*]}/${#instpkgs[*]})"
      if [ "$verbose_mode" ]; then
         for j in `seq 0 ${#instpkgs[*]}`; do
            [ `expr $j % 5` -eq 0 ] && echo -n "                                "
            echo -n "${instpkgs[$j]} "
            [ `expr $j % 5` -eq 4 ] && echo
         done
         echo; echo
      fi
   fi
done
[ "$list_mode" ] && exit 0

#[ ! "$full_update_mode" -a ! "$installnames" ] && installnames="base"

if [ "$installnames" ]; then
   for i in `seq 0 ${#PKG_GROUPS[*]}`; do
      for n in $installnames; do
         [ "${PKG_NAMES[$i]}" = "$n" ] && installgroups="$installgroups $i"
      done
   done
elif [ "$needupdategroups" ]; then
   echo -n $"The following groups need to be updated: "
   for n in $needupdategroups; do 
      echo -n "${PKG_NAMES[$n]} "
      installgroups="$installgroups $n"
   done
   echo
   [ "$assume_yes" ] || {
      echo -n $"Proceed with update [Y/n]?"
      read ans
      [ "$ans" = "n" -o "$ans" = "N" ] && exit 0
   }
fi

for i in $installgroups; do
   eval instpkgs=\$`echo ${PKG_GROUPS[$i]}`
   for i in $instpkgs; do
      rpm -q --whatprovides $i >/dev/null || {
         finalinstalllist=(${finalinstalllist[*]} $i)
      }
   done
done

if [ "${finalinstalllist[*]}" ]; then
   [ "$verbose_mode" ] && echo $"Installing: ${finalinstalllist[*]}"
   pkg_tool install -y ${finalinstalllist[*]}
   exit $?
fi
exit 0
