#!/bin/bash

# script to upgrade packages with user's preferred upgrade type
# this script is part of apt-notifier package

# translations are defined within apt-notifier python modules
# define a function to make xgettext ignore translations used here
mygettext() { gettext "$@"; }

# check privs
if [ $(id -u) != 0 ]; then
    MSG='This command needs %s privileges to be executed.\n'
    printf "$(mygettext -d su-to-root "$MSG")" 'root'
    exit 1
fi

#-----------------------------------------------------------------------
# set less-prompt
#
export TEXTDOMAINDIR="/usr/share/locale"
export LESS="-Ps"$(gettext -d util-linux "[Press space to continue, 'q' to quit.]")


if [ ! -e /etc/apt/sources.list ]; then
    cat <<EOF > /etc/apt/sources.list
#This sources.list is empty by default and is only included
#to avoid error messages from some package install scripts that expect
#to find it.
#Sources are under /etc/apt/sources.list.d
EOF
fi

# use nala if needed
#
APT_NOTIFIERRC="/home/$(logname)/.config/apt-notifierrc"

UpgradeType=$(     grep -oP '^UpgradeType=\K.*'      "$APT_NOTIFIERRC" 2>/dev/null)
UpgradeAssumeYes=$(grep -oP '^UpgradeAssumeYes=\K.*' "$APT_NOTIFIERRC" 2>/dev/null)
UpgradeAutoClose=$(grep -oP '^UpgradeAutoClose=\K.*' "$APT_NOTIFIERRC" 2>/dev/null)

# upgrade type dist-upgrade or upgrade
export TEXTDOMAIN=apt-notifier
APTGET_UPGRADETYPE='upgrade'
if [ "$UpgradeType" = "upgrade" ]
  then
    :
    #mygettext "basic upgrade"; echo
  else
    #mygettext "full upgrade" ; echo
    UpgradeType='full-upgrade'
    APTGET_UPGRADETYPE='dist-upgrade'
fi

# assume yes handling
if [ "$UpgradeAssumeYes" = "true" ]; then
    AssumeYes="--assume-yes"
else
    AssumeYes=""
fi

# apt preference handling
UPDATER_APTPREF=/usr/lib/apt-notifier/shlib/updater_aptpref
AptPref_Opts=""
if [ -f "$UPDATER_APTPREF" ]; then
      . "$UPDATER_APTPREF"
fi

unset APT_NOTIFIER_CONFIG_ITEMS
declare -A APT_NOTIFIER_CONFIG_ITEMS
UPDATER_CONFIG="/usr/lib/apt-notifier/bin/updater_config"
if [ -x "${UPDATER_CONFIG}" ]; then
    eval "APT_NOTIFIER_CONFIG_ITEMS=$(${UPDATER_CONFIG} --shell)"
fi

# use nala if desired and available

: ${APT_NOTIFIER_USE_NALA:=false}
export APT_NOTIFIER_USE_NALA
if [ x"${APT_NOTIFIER_CONFIG_ITEMS[use_nala]}" = x"true" ]; then
    APT_NOTIFIER_USE_NALA=true
    if ! which nala >/dev/null; then
       APT_NOTIFIER_USE_NALA=false
    fi
fi

# check if the last apt update run was less than 3 minutes ago
update_stamp=$(find /var/lib/apt/periodic/update-stamp -mmin -3 2>/dev/null)

if [ x"$APT_NOTIFIER_USE_NALA" = x"true" ]; then
    Nala_Pref="--no-autoremove"
    if [ -z "$update_stamp" ]; then
        Nala_Update_Pref="--update"
        nala update
    else
        Nala_Update_Pref="--no-update"
    fi
    Nala_Update_Pref="--no-update"

    #[ -z "$update_stamp" ] && Nala_Update_Pref="--update" || Nala_Update_Pref="--no-update"
    if [ "$UpgradeType" = "upgrade" ]; then
        Nala_UpgradeType="--no-full"
    else
        Nala_UpgradeType="--full" # default
    fi

    Nala_Update_Pref_Raw=""
    LINUX_DKMS_COUNT=$(
        LANGUAGE= LC_ALL= LANG=C
        apt-get $AptPref_Opts -o Debug::NoLocking=true --trivial-only -V $APTGET_UPGRADETYPE 2>/dev/null |
        sed -nr '/^The following.*will be (installed|upgraded):/,${
                 s/^\s+//;
                 /^linux-image-|^[^\s]*-dkms/p};
                ' | wc -l )

    if (( LINUX_DKMS_COUNT != 0 )); then
        Nala_Update_Pref_Raw=" --raw-dpkg"
    fi

    [ y"${DEBUG_APT_NOTIFIER/true/es}" = "yes" ] &&  echo nala upgrade $AptPref_Opts $Nala_UpgradeType $Nala_Pref $AssumeYes
    echo "nala upgrade $Nala_UpgradeType $Nala_Pref $Nala_Update_Pref_Raw"
    nala upgrade $AptPref_Opts $Nala_UpgradeType $Nala_Pref $Nala_Update_Pref $Nala_Update_Pref_Raw $AssumeYes
    if [ "$Nala_Update_Pref" = "--update" ]; then
       [ -d /var/lib/apt/periodic ] || mkdir -p /var/lib/apt/periodic
       touch /var/lib/apt/periodic/update-stamp
    fi

else
    if [ -z "$update_stamp" ]; then
       echo apt update
       apt update --option 'APT::Cmd::Show-Update-Stats=0'
       [ -d /var/lib/apt/periodic ] || mkdir -p /var/lib/apt/periodic
       touch /var/lib/apt/periodic/update-stamp
    fi
    echo apt $UpgradeType
	if [ "$UpgradeType" = "upgrade" ]
	  then
	    AptPref_Opts+=" -o APT::Get::Upgrade-Allow-New=false"
	    AptPref_Opts+=" -o Binary::apt::APT::Get::Upgrade-Allow-New=false"
	  else
	    AptPref_Opts+=" -o APT::Get::Upgrade-Allow-New=true"
	    AptPref_Opts+=" -o Binary::apt::APT::Get::Upgrade-Allow-New=true"
	fi
    apt --verbose-versions $AssumeYes $AptPref_Opts $UpgradeType
fi

echo ""

if [ "$UpgradeType" = "upgrade" ]; then
    mygettext "basic upgrade complete (or was canceled)"; echo
else
    mygettext "full upgrade complete (or was canceled)"; echo
fi

# auto close timeout
AUTO_CLOSE_TIMEOUT="${APT_NOTIFIER_CONFIG_ITEMS[upgrade_auto_close_timeout]}"
default_timeout=10
if [ -z "${AUTO_CLOSE_TIMEOUT}" ]; then
    AUTO_CLOSE_TIMEOUT=$default_timeout
fi
if [ -n "${AUTO_CLOSE_TIMEOUT//[0-9]/}" ]; then
    AUTO_CLOSE_TIMEOUT=$default_timeout
fi

UPDATER_SHLIB=/usr/lib/apt-notifier/shlib/updater_shlib
if [ -f "$UPDATER_SHLIB" ]; then
      . "$UPDATER_SHLIB"
fi
echo
mygettext "this terminal window can now be closed"
echo
echo
if [ "$UpgradeAutoClose" = "true" ]; then
    press_any_key_stop_auto_close && exit
fi
press_any_key_to_close && exit

exit
