#!/bin/bash


patchlist=""
packagelist=""
type="patch"

while [ -n "$1" ]; do

	case "$1" in
		"--patch")
		type="patch"
		;;

		"--package")
		type="package"
		;;

	*)
		if [ $type == "patch" ]; then
			patchlist="$patchlist \"$1\""
		fi

		if [ $type == "package" ]; then
			packagelist="$packagelist \"$1\""
		fi

		;;
	esac

	shift
done


patchcommand="zypper -q --terse --non-interactive in -l -t patch --force --force-resolution --name $patchlist"
packagecommand="zypper -q --terse --non-interactive in -l -t package --force --force-resolution --name $packagelist"

tempdir=`mktemp -d /tmp/opensuseupdater.XXXXXXXXXX`
if [ $? != 0 ];
then	
	echo "Unable to create temp dir"
	exit 1
fi

out=${tempdir}/opensuseupdater_out
err=${tempdir}/opensuseupdater_err


mkfifo $out || { echo "can't mkfifo $out" >&2 ; exit 1 ; }
mkfifo $err || { echo "can't mkfifo $err" >&2 ; exit 1 ; }


retval=0

cat $err >&2 &
cat $out &

command="true"

if [ -n "$patchlist" ]; then
  command="$command ; $patchcommand "
fi

if [ -n "$packagelist" ]; then
  command="$command ; $packagecommand "
fi

kdesu --noignorebutton -d -c "( $command )"
retval=$?

rm -rf ${tempdir}

exit $retval
