#!/bin/bash
#
# netsrpm-install.sh - Build srpms that download sources from network
#
# Copyright (C) 2003-2014 by Silvan Calarco <silvan.calarco@mambasoft.it>
# Copyright (C) 2005 by Massimo Pintore <massimo.pintore@qilinux.it>
# Copyright (C) 2005-2007 by Davide Madrisan <davide.madrisan@qilinux.it>

TEXTDOMAINDIR=/usr/share/locale/
TEXTDOMAIN=openmamba-update
me="${0##*/}"
DISTRO_SRPMS_DIR=/usr/share/openmamba/SRPMS
rpmdir=$(rpm --eval=%{_rpmdir} 2>/dev/null)

while [ "$1" ]; do
   case $1 in
      -c) CHECK_MODE=1 ;;
      -f) FORCE_MODE=1 ;;
      -u) UPGRADE_MODE=1 ;;
      -h|--help) PACKAGES= ; break ;;
      -*) echo $"Error: invalid option $1; aborting."; exit 1 ;;
      *) PACKAGES="$PACKAGES $1"
   esac
   shift
done

if [ -z "$PACKAGES" ]; then
   echo $"Usage:
$me [options...] pkgname ...

Options:
-c: only check and exit (returns: 0: up-to-date; 1: needs update; 2: not installed)
-f: force reinstallation if already installed
-u: only upgrade if already installed
"
   exit 255
fi

function check_if_already_installed() {
   rpm -q $1 &>/dev/null
   if [ $? -eq 0 ]; then
      notify-send -a $"Network software installation" -i $icon "$pckname" "$pckname: "$"already installed."
   fi
}

declare -A SRPMVERSION SRPMRELEASE
. /etc/sysconfig/netsrpminstall || exit 1

for pckname in $PACKAGES; do

   SRPM_PATH=$DISTRO_SRPMS_DIR/${pckname}
   SPEC_PATHNAME=$SRPM_PATH/${pckname}.spec

   [ -r "$SPEC_PATHNAME" ] || {
      echo $"$me: $pckname is missing in $DISTRO_SRPMS_DIR; aborting."
      exit 255
   }

   icon=`grep Icon= /usr/share/applications/openmamba-$pckname.desktop | sed "s|Icon=||"`

   SRPM_VERSION=${SRPMVERSION[${pckname}]}-${SRPMRELEASE[${pckname}]}

   RPM_INSTALLED=`rpm -q $pckname`
   [ $? -eq 0 ] || RPM_INSTALLED=

   RPM_INSTALLED_VERSION=`\
      echo $RPM_INSTALLED | \
      sed "s|${pckname//+/\\+}-\([^-]*-[^-]*\)\..*|\1|" 2>/dev/null`

   echo $"Package name:      $pckname"
   echo $"Installed version: $RPM_INSTALLED_VERSION"
   echo $"Last version:      $SRPM_VERSION"
   echo

   if [ "$RPM_INSTALLED_VERSION" != "$SRPM_VERSION" ]; then
      if [ "$RPM_INSTALLED_VERSION" ]; then
         UPDATE_NEEDED=1
      elif [ "$UPGRADE_MODE" ]; then
         continue
      fi
   elif [ ! "$FORCE_MODE" ]; then
      continue
   fi

   if [ ! "$CHECK_MODE" ]; then
      if [ -f ${SRPM_PATH}/LICENSE ]; then
         if [ "$DISPLAY" ]; then
            license-dialog ${SRPM_PATH}/LICENSE
            [ $? -ne 0 ] && exit 1
         else
            echo $"You must accept the following license terms:"
            echo
            more ${SRPM_PATH}/LICENSE
            echo -n $"Do you agree with the above license terms [y/N]? "
            read ans
            [ "$ans" = "Y" -o "$ans" = "y" -o "$ans" = "s" -o "$ans" = "S" ] || exit 1
         fi
      fi

      notify-send -a $"Network software installation" -i $icon "$pckname" $"$pckname installation in progress..."

      # build rpm
      tmpfile=`mktemp -q -t $me.XXXXXXXX` || {
         echo "$me: "$"error: cannot create temporary files; aborting."
         exit 255
      }

      tmpdir=`mktemp -d -q -t $me.XXXXXXXX` || {
         echo "$me: "$"error: cannot create temporary directory; aborting."
         exit 255
       }

      echo "$me: "$"rebuilding package \`$pckname'..."

      sed "s|@SRPMVERSION@|${SRPMVERSION[${pckname}]}|g;s|@SRPMRELEASE@|${SRPMRELEASE[${pckname}]}|g" \
         ${SPEC_PATHNAME} >> $tmpfile

      rpmbuild --ba \
         --define="%_rpmdir $tmpdir" \
         --define="%_sourcedir ${SRPM_PATH}" \
         $tmpfile

      if [ $? -gt 0 ]; then
         echo "$me: "$"error rebuilding \`$pckname'."" "$"Aborting..." >&2
         notify-send -a $"Network software installation" -i $icon "$pckname" $"There was an error installing"" $pckname!"
         exit 255
      fi
      RPM_PATH=`find $tmpdir -name \*.rpm`

      # install rpm
      if [ "$FORCE_MODE" = "1" ]; then
         sudo smart remove -y $pckname
      fi
      sudo smart install -y $RPM_PATH
      if [ $? -gt 0 ]; then
         echo "$me: "$"error installing \`$RPM_PATH'."" "$"Aborting..." >&2
         check_if_already_installed $pckname
         exit 255
      fi

      rpm -q $pckname 2>/dev/null
      if [ $? -eq 0 ]; then
         notify-send -a $"Network software installation" -i $icon "$pckname" $"Installation of $pckname successfully completed."
      else
         notify-send -a $"Network software installation" -i $icon "$pckname" $"There was an error installing $pckname!"
      fi
      rm -rf $tmpdir $tmpfile

   fi
done

if [ "$CHECK_MODE" ]; then
   if [ "$UPDATE_NEEDED" ]; then
      echo $"Update needed."
      exit 1
   fi
   rpm -q $pckname > /dev/null
   if [ $? -eq 1 ]; then
      echo $"Not installed."
      exit 2
   fi
fi

exit 0
