#!/bin/bash

qarma_gui() {
    qarma "$@" 2>/dev/null
}
# -------------------------------------------------
# Be om sudo-passord éin gong
# -------------------------------------------------

sudo -v || exit 1


# -------------------------------------------------
# Verifisera pakkesystemet?
# -------------------------------------------------

qarma_gui --question \
    --title="System update" \
    --text="Do you want to verify installed packages?" \
    --ok-label="yes" \
    --cancel-label="no"

svar=$?

if [ "$svar" -eq 0 ]; then

    tmpfile=$(mktemp)

    sudo /usr/bin/zypper --non-interactive verify > "$tmpfile" 2>&1 &
    pid=$!

    (
        while kill -0 "$pid" 2>/dev/null; do
            echo "# Verifying packagaes ..."
            sleep 0.5
        done
    ) | qarma_gui --progress \
        --title="System update" \
        --text="Verifying packages ..." \
        --pulsate \
        --auto-close \
        --no-cancel \
        --width=450

    wait "$pid"
    resultat=$?

    if [ "$resultat" -eq 0 ]; then

        qarma_gui --text-info \
            --title="Result from zypper verify" \
            --filename="$tmpfile" \
            --width=800 \
            --height=500 \
            --ok-label="Close"

    else

        qarma_gui --text-info \
            --title="Error during zypper verify" \
            --filename="$tmpfile" \
            --width=900 \
            --height=600 \
            --ok-label="Close"
    fi

    rm -f "$tmpfile"
fi


# -------------------------------------------------
# Oppdater pakkebrønnar
# -------------------------------------------------

tmpfile=$(mktemp)

sudo /usr/bin/zypper --non-interactive refresh > "$tmpfile" 2>&1 &
pid=$!

(
    while kill -0 "$pid" 2>/dev/null; do
        echo "# Updating repositories ..."
        sleep 0.5
    done
) | qarma_gui --progress \
    --title="System update" \
    --text="repositories are refreshing ..." \
    --pulsate \
    --auto-close \
    --no-cancel \
    --width=450

wait "$pid"
resultat=$?

if [ "$resultat" -eq 0 ]; then

    qarma_gui --info \
        --title="System update" \
        --text="Repositories are updated." \
        --ok-label="OK"

else

    qarma_gui --text-info \
        --title="Error during zypper refresh" \
        --filename="$tmpfile" \
        --width=900 \
        --height=600 \
        --ok-label="Close"

    rm -f "$tmpfile"
    exit 1
fi

rm -f "$tmpfile"


# -------------------------------------------------
# Zypper dup eller Myrlyn?
# -------------------------------------------------

qarma_gui --question \
    --title="System update" \
    --text="Do you want to update with zypper dup?, If not Myrlyn will be opened" \
    --ok-label="Yes, update" \
    --cancel-label="No, use Myrlyn"

svar=$?


# -------------------------------------------------
# Oppdater med zypper dup
# -------------------------------------------------

if [ "$svar" -eq 0 ]; then

    logdir="${XDG_CACHE_HOME:-$HOME/.cache}/sysupdate"
mkdir -p "$logdir"

tmpfile=$(mktemp "$logdir/zypper-dup.XXXXXX")

    expect <<EOF
set timeout -1

log_file -noappend "$tmpfile"

spawn pkexec /usr/bin/env LC_ALL=C LANG=C /usr/bin/zypper dup

expect {
    -re {Continue\?.*} {

        set rc [catch {
            exec qarma --question \
                --title="System update" \
                --text="Zypper asks whether you want to continue." \
                --ok-label="Yes" \
                --cancel-label="No"
        } resultat]

        if {\$rc == 0} {
            send "y\r"
        } else {
            send "n\r"
        }

        exp_continue
    }

    eof
}

catch wait result
exit [lindex \$result 3]
EOF

    resultat=$?

    if [ "$resultat" -eq 0 ]; then

        qarma_gui --text-info \
            --title="Results from zypper dup" \
            --filename="$tmpfile" \
            --width=900 \
            --height=600 \
            --ok-label="Close"

        qarma_gui --info \
            --title="System update" \
            --text="System has been updated." \
            --ok-label="OK"

    else

        qarma_gui --text-info \
            --title="Error during zypper dup" \
            --filename="$tmpfile" \
            --width=900 \
            --height=600 \
            --ok-label="Close"
    fi

    rm -f "$tmpfile"

# -------------------------------------------------
# Opna Myrlyn
# -------------------------------------------------

else

    qarma_gui --info \
        --title="System update" \
        --text="Opens Myrlyn ..." \
        --timeout=2 \
        --no-wrap

    /usr/bin/myrlyn-sudo &
    sleep 3
    disown

    exit 0
fi
