#!/bin/bash

usage() {
cat 1>&2 <<EOF
usage: $0

       checks whether there are new packages available, and if so, prompts for updating them
       after that, it suggests to push the new changes in /etc

EOF
}

error() {
        echo "$*" 1>&2
}

APT=$(which apt)
APTGET=$(which apt-get)
APTITUDE=$(which aptitude)
DF=$(which df)


should_use_sources() {
        if dpkg --compare-versions "$(apt-get --version | head -1 | awk '{print $2}')" ge 2.9.28; then
                # Debian>=trixie: should use .sources
                if find /etc/apt/sources.list /etc/apt/sources.list.d/ -name "*.list" 2>/dev/null | grep -q .; then
                        # but apparently still using .list
                        cat 1>&2 <<EOF
You are using old-style apt sources.
Consider upgrading them with
~~~
apt modernize-sources
~~~
EOF
                        return 1
                fi
                return 0
        else
                # old Debian; use .list
                return 1
        fi
}

check_apt_list() {
    find /etc/apt/sources.list.d/ -name "*.list" -exec sed -e 's|#.*||' -e '/^[[:space:]]*$/d' -n -e '/apt.iem.at/p' {} + 2>/dev/null | grep -q "://apt.iem.at/"
}
check_key_list() {
    find /etc/apt/sources.list.d/ -name "*.list" -exec sed -e 's|#.*||' -e '/^[[:space:]]*$/d' -n -e '/apt.iem.at/p' {} + 2>/dev/null | grep -q "signed-by=${1}"
}
show_apt_list() {
        cat <<EOF
# /etc/apt/sources.list.d/iem.list
deb [signed-by=$1] https://apt.iem.at/debian iem main
EOF
}
check_apt_sources() {
    find /etc/apt/sources.list.d/ -name "*.sources" -exec grep -q "^URIs: .*//apt.iem.at" {} +
}
check_key_sources() {
    find /etc/apt/sources.list.d/ -name "*.sources" -exec grep -q "^Signed-By: ${1}$" {} +
}
show_apt_sources() {
        cat <<EOF
# /etc/apt/sources.list.d/iem.sources
Types: deb
URIs: https://apt.iem.at/debian/
Suites: iem
Components: main
Signed-By: $1
EOF
}

if should_use_sources; then
        check_iemapt() {
                check_apt_sources "$@"
        }
        check_iemkey() {
                check_key_sources "$@"
        }
        show_iemapt() {
                show_apt_sources "$@"
        }
else
        check_iemapt() {
                check_apt_list "$@"
        }
        check_iemkey() {
                check_key_list "$@"
        }
        show_iemapt() {
                show_apt_list "$@"
        }
fi


do_keycheck() {
    keyfile=/usr/local/share/keyrings/iem-archive-keyring.asc

    if check_iemapt; then
        if check_iemkey "${keyfile}"; then
            :
        else
            error "keyfile '${keyfile}' is not used to sign 'apt.iem.at' archives."
            error "use something like the following in your apt-sources:"
            error
            show_iemapt "${keyfile}"
            error
        fi
    else
        error "cannot find entry for 'apt.iem.at' in your apt-sources. continue anyway"
    fi
}

do_checkupdate() {
    if [ -x "${APT}" ]; then
        "${APT}" update
    elif [ -x "${APTITUDE}" ]; then
        "${APTITUDE}" update
    elif [  -x "${APTGET}" ]; then
        "${APTGET}" update
    fi
    if [  -x "${APTITUDE}" ]; then
        "${APTITUDE}"  forget-new
    fi
    if [ -x "${APT}" ]; then
        "${APT}" list --upgradable
        "${APT}" list --upgradable 2>/dev/null  | grep upgradable >/dev/null
    elif [ -x "${APTGET}" ]; then
        if "${APTGET}" upgrade --no-upgrade --assume-no; then
            false
        else
            true
        fi
    fi
}

show_freespace() {
    if [ -x "${DF}" ]; then
        echo "=================================="
        echo "available space on local harddisks"
        echo
        "${DF}" -hl | egrep "^/"
        echo "=================================="
    fi
}

do_update() {
    if [ -x "${APT}" ]; then
        "${APT}" upgrade
    elif [ -x "${APTITUDE}" ]; then
        "${APTITUDE}" upgrade
    elif [ -x "${APTGET}" ]; then
        "${APTGET}" upgrade
    fi
}

do_push() {
    virtshaus_pushetc
}

do_keycheck

if do_checkupdate; then
    show_freespace
    do_update && virtshaus-pushetc
fi
