#!/bin/bash

# -------------------------------------------------
# Vis aktive pakkebrønnar
# -------------------------------------------------

vis_repo() {

    repo_data=()

    while IFS= read -r -d '' felt; do
        repo_data+=("$felt")
    done < <(
        LC_ALL=C zypper lr -d |
        awk -F'|' '
            /^[[:space:]]*[0-9]+[[:space:]]*\|/ {
                for (i=1; i<=NF; i++)
                    gsub(/^[ \t]+|[ \t]+$/, "", $i)

                if ($4 == "Yes") {
                    printf "%s%c", $1, 0
                    printf "%s%c", $2, 0
                    printf "%s%c", $3, 0
                    printf "%s%c", $8, 0
                    printf "%s%c", $9, 0
                    printf "%s%c", $10, 0
                }
            }
        '
    )

    qarma --list \
        --title="Active repositories" \
        --column="Nr." \
        --column="Alias" \
        --column="Name" \
        --column="Priority" \
        --column="Type" \
        --column="Address" \
        --width=1200 \
        --height=650 \
        "${repo_data[@]}"
}


# -------------------------------------------------
# Legg til pakkebrønn
# -------------------------------------------------

legg_til_repo() {

    adresse=$(qarma --entry \
        --title="Add repository" \
        --text="Enter the address of the repository:" \
        --width=500)

    [ -z "$adresse" ] && return

    alias=$(qarma --entry \
        --title="Add repository" \
        --text="Enter an alias for the repository:" \
        --width=500)

    [ -z "$alias" ] && return

    qarma --question \
        --title="Add repository" \
        --text="$(printf 'Do you want to add this repository?\n\nAlias: %s\nAddress: %s' "$alias" "$adresse")"

    [ $? -ne 0 ] && return

    if sudo zypper ar -f "$adresse" "$alias"; then
        qarma --info \
            --title="Finished" \
            --text="Repository added."
    else
        qarma --error \
            --title="Error" \
            --text="Could not add the repository."
    fi
}


# -------------------------------------------------
# Fjern pakkebrønn
# -------------------------------------------------

fjern_repo() {

    repo_data=()

    while IFS= read -r -d '' felt; do
        repo_data+=("$felt")
    done < <(
        LC_ALL=C zypper lr |
        awk -F'|' '
            /^[[:space:]]*[0-9]+[[:space:]]*\|/ {
                for (i=1; i<=NF; i++)
                    gsub(/^[ \t]+|[ \t]+$/, "", $i)

                printf "%s%c", $1, 0
                printf "%s%c", $2, 0
                printf "%s%c", $3, 0
            }
        '
    )

    repo=$(qarma --list \
        --title="Remove repository" \
        --text="Choose a repository to remove:" \
        --column="Nr." \
        --column="Alias" \
        --column="Name" \
        --print-column=2 \
        --width=800 \
        --height=500 \
        "${repo_data[@]}")

    [ -z "$repo" ] && return

    qarma --question \
        --title="Remove repository" \
        --text="$(printf 'Do you really want to remove this repository?\n\nAlias: %s' "$repo")"

    [ $? -ne 0 ] && return

    if sudo zypper rr "$repo"; then
        qarma --info \
            --title="Finished" \
            --text="Repository removed."
    else
        qarma --error \
            --title="Error" \
            --text="Could not remove the repository."
    fi
}


# -------------------------------------------------
# Hovudmeny
# -------------------------------------------------

while true; do

    valg=$(qarma --list \
        --title="Repositories" \
        --text="Choose an action:" \
        --column="Action" \
        --print-column=1 \
        "View active repositories" \
        "Add repository" \
        "Remove repository" \
        "Quit" \
        --width=450 \
        --height=320)

    case "$valg" in

        "View active repositories")
            vis_repo
            ;;

        "Add repository")
            legg_til_repo
            ;;

        "Remove repository")
            fjern_repo
            ;;

        "Quit"|"")
            exit 0
            ;;

    esac

done
