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

VERSION=0.3
TEXTDOMAIN=mambabase
TEXTDOMAINDIR=/opt/kde3/share/locale/
INSTALL_DATE=`date +%Y%m%d-%H%M`
INPUT_FILE=~/.mambabase.conf
LOG_FILE=/var/log/install-mamba-$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 dcop_write() {
   [ "$DCOPID" ] && {
      [ $2 = -1 ] && 
         PROGRESS_POS=$TOTAL_PROGRESS ||
	 PROGRESS_POS=`expr $PROGRESS_POS + $2`
      dcop $DCOPID KommanderIf setText progressText "$1"
      dcop $DCOPID KommanderIf setText progressBar $PROGRESS_POS
   }
}

function apt_parser() {
   while read line; do
      [ "$DCOPID" ] && {
         echo $line
         dcop $DCOPID KommanderIf setText progressText2 "$line"
      }
   done
}

[ "$1" = "-y" ] || {
   echo $"Warning: this script will perform an automatic installation erasing all the"
   echo $"         contents of device $INSTALL_DEVICE; use $0 -y to actually start the"
   echo $"         operation."
   exit 0
}

function apt_get_parsed() {
   sudo smart $@ | apt_parser
}

DCOPID=$2

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

[ "$DCOPID" ] && dcop $DCOPID KommanderIf setChecked NextBox false
dcop_write $"Starting installation..." 0

[ -r $INPUT_FILE ] || {
   echo $"Error: cannot read input file $INPUT_FILE; aborting." >&2
   abort
}

. $INPUT_FILE

TOTAL_PROGRESS=10

[ "$INSTALL_BASE" = "1" ] && TOTAL_PROGRESS=`expr $TOTAL_PROGRESS + 35`
[ "$INSTALL_UPDATE" = "1" ] && TOTAL_PROGRESS=`expr $TOTAL_PROGRESS + 15`
[ "$INSTALL_OFFICE" = "1" ] && TOTAL_PROGRESS=`expr $TOTAL_PROGRESS + 15`
[ "$INSTALL_PLAYERS" = "1" ] && TOTAL_PROGRESS=`expr $TOTAL_PROGRESS + 15`
[ "$INSTALL_MULTIMEDIA_EDITING" = "1" ] && TOTAL_PROGRESS=`expr $TOTAL_PROGRESS + 15`
[ "$INSTALL_INTERNET" = "1" ] && TOTAL_PROGRESS=`expr $TOTAL_PROGRESS + 15`
[ "$INSTALL_GRAPHICS" = "1" ] && TOTAL_PROGRESS=`expr $TOTAL_PROGRESS + 15`
[ "$INSTALL_GAMES" = "1" ] && TOTAL_PROGRESS=`expr $TOTAL_PROGRESS + 15`
[ "$INSTALL_VIRTUALIZATION" = "1" ] && TOTAL_PROGRESS=`expr $TOTAL_PROGRESS + 15`
[ "$INSTALL_SERVER" = "1" ] && TOTAL_PROGRESS=`expr $TOTAL_PROGRESS + 15`
[ "$INSTALL_DEVEL" = "1" ] && TOTAL_PROGRESS=`expr $TOTAL_PROGRESS + 15`
[ "$INSTALL_FLASH" = "1" ] && TOTAL_PROGRESS=`expr $TOTAL_PROGRESS + 10`
[ "$INSTALL_SKYPE" = "1" ] && TOTAL_PROGRESS=`expr $TOTAL_PROGRESS + 10`
[ "$INSTALL_CODECS" = "1" ] && TOTAL_PROGRESS=`expr $TOTAL_PROGRESS + 10`
[ "$INSTALL_MSTTCF" = "1" ] && TOTAL_PROGRESS=`expr $TOTAL_PROGRESS + 10`
[ "$INSTALL_JRE" = "1" -o "$INSTALL_JDK" = "1" ] && TOTAL_PROGRESS=`expr $TOTAL_PROGRESS + 10`

[ "$DCOPID" ] && dcop $DCOPID KommanderIf setMaximum progressBar $TOTAL_PROGRESS

if [ "$INSTALL_BASE" = "1" ]; then
   echo $"Installing openmamba base packages"
   dcop_write $"Updating packages list..." 10
   apt_get_parsed update

   dcop_write $"Removing packages that are no longer needed..." 10
   rpm -q msysklogd >/dev/null && apt_get_parsed remove -y msysklogd

   dcop_write $"Installing/updating openmamba base packages..." 15
   rpm -q --whatprovides $BASE_PKGS >/dev/null || apt_get_parsed install -y $BASE_PKGS

   # first time: remove mambabase from autostart
   [ -e /home/$USER/.kde4/Autostart/mambabase.desktop ] && \
      rm -f /home/$USER/.kde4/Autostart/mambabase.desktop
   # ugly workaround: don't have permissions to rm file in /etc/xdg/autostart
   [ -e /etc/xdg/autostart/mambabase.desktop ] && \
      > /etc/xdg/autostart/mambabase.desktop
fi

if [ "$INSTALL_UPDATE" = "1" ]; then
   echo $"Updating installed packages"
   dcop_write $"Updating installed packages..." 15
   apt_get_parsed upgrade -y
fi

if [ "$INSTALL_OFFICE" = "1" ]; then
   echo $"Installing Office packages"
   dcop_write $"Installing/updating Office packages..." 15
   rpm -q --whatprovides $OFFICE_PKGS >/dev/null || apt_get_parsed install -y $OFFICE_PKGS
fi

if [ "$INSTALL_PLAYERS" = "1" ]; then
   echo $"Installing Audio and Video players"
   dcop_write $"Installing/updating Audio and Video players..." 15
   rpm -q --whatprovides $MULTIMEDIA_PLAYERS_PKGS >/dev/null || apt_get_parsed install -y $MULTIMEDIA_PLAYERS_PKGS
fi

if [ "$INSTALL_MULTIMEDIA_EDITING" = "1" ]; then
   echo $"Installing multimedia editing and production packages"
   dcop_write $"Installing/updating multimedia editing and production packages..." 15
   rpm -q --whatprovides $MULTIMEDIA_EDITING_PKGS >/dev/null || apt_get_parsed install -y $MULTIMEDIA_EDITING_PKGS
fi

if [ "$INSTALL_INTERNET" = "1" ]; then
   echo $"Installing Internet packages"
   dcop_write $"Installing/updating Internet packages..." 15
   rpm -q --whatprovides $INTERNET_PKGS >/dev/null || apt_get_parsed install -y $INTERNET_PKGS
fi

if [ "$INSTALL_GRAPHICS" = "1" ]; then
   echo $"Installing Graphics packages"
   dcop_write $"Installing/updating Graphics packages..." 15
   rpm -q --whatprovides $GRAPHICS_PKGS >/dev/null || apt_get_parsed install -y $GRAPHICS_PKGS
fi

if [ "$INSTALL_GAMES" = "1" ]; then
   echo $"Installing Games packages"
   dcop_write $"Installing/updating Games packages..." 15
   rpm -q --whatprovides $GAMES_PKGS >/dev/null || apt_get_parsed install -y $GAMES_PKGS
fi

if [ "$INSTALL_VIRTUALIZATION" = "1" ]; then
   echo $"Installing Virtualization packages"
   dcop_write $"Installing/updating Virtualization packages..." 15
   rpm -q --whatprovides $VIRTUALIZATION_PKGS >/dev/null || apt_get_parsed install -y $VIRTUALIZATION_PKGS
fi

if [ "$INSTALL_SERVER" = "1" ]; then
   echo $"Installing Server packages"
   dcop_write $"Installing/updating Server packages..." 15
   rpm -q --whatprovides $SERVER_PKGS >/dev/null || apt_get_parsed install -y $SERVER_PKGS
fi

if [ "$INSTALL_DEVEL" = "1" ]; then
   echo $"Installing openmamba packages"
   dcop_write $"Installing/updating Development packages..." 15
   rpm -q --whatprovides $DEVEL_PKGS >/dev/null || apt_get_parsed install -y $DEVEL_PKGS
fi

if [ "$INSTALL_FLASH" = "1" ]; then
   echo $"Installing flash plugin"
   dcop_write $"Installing Flash plugin..." 10
   /usr/bin/openmamba-netsrpms flashplugin
   echo $"Executing nspluginscan"
   nspluginscan
   rpm -q gnash && {
      echo $"Removing gnash"
      sudo rpm -e gnash      
   }
fi

if [ "$INSTALL_CODECS" = "1" ]; then
   echo $"Installing win32 codecs"
   dcop_write $"Installing Win32 codecs..." 10
   /usr/bin/openmamba-netsrpms win32codecs
fi

if [ "$INSTALL_MSTTCF" = "1" ]; then
   dcop_write $"Installing MS TrueType core fonts..." 10
   /usr/bin/openmamba-netsrpms msttcorefonts
fi

if [ "$INSTALL_SKYPE" = "1" ]; then
   rpm -q libqt4 >/dev/null || apt_get_parsed install -y libqt4
   dcop_write $"Installing Skype software..." 10
   /usr/bin/openmamba-netsrpms skype
fi

if [ "$INSTALL_JDK" = "1" ]; then
   dcop_write $"Installing Sun Java Developers Kit..." 10
   apt_get_parsed install -y $SUN_JAVA_PKGS

elif [ "$INSTALL_JRE" = "1" ]; then
   dcop_write $"Installing Sun Java Runtime Environment..." 10
   apt_get_parsed install -y $SUN_JAVA_RUNTIME_PKGS
fi

dcop_write $"Installation completed." -1
[ "$DCOPID" ] && {
   dcop $DCOPID KommanderIf setText progressText2 $"Press next to see the installation log and exit."
   dcop $DCOPID KommanderIf setChecked NextBox true
}
echo $"Installation completed."

exit 0
