#!/bin/sh

if [ "$1" == "" ]; then
    xdg-su -c "zypp-up --user $USER"
elif [ "$1" == "--user" ]; then
    xterm -T "Available Updates" \
            -fa "Monospace" -fs 10 \
            -rightbar \
            $(grep "$2" ~/.zypp-up | sed -e "s/$2/-geometry/") \
            -e "zypp-up --update $2"
elif [ "$1" == "--update" ]; then
    zypper --no-refresh update &
    ZYPP_PID=$!
    function cleanup
    {
        if [ "$(cat /run/zypp.pid)" == "$ZYPP_PID" ]; then
            echo -n >/run/zypp.pid;
        fi
    }
    trap cleanup EXIT
    wait
    read -p "Press any key to continue ..." -n1 -s
    if grep "$2" ~/.zypp-up;then
        cat ~/.zypp-up | sed -e "s/$2.*/$2 $(tput cols)x$(tput lines)/" >~/.zypp-up
    else
        echo "$2" $(tput cols)x$(tput lines) >>~/.zypp-up
    fi
    echo
fi;
