#!/bin/sh

set -e

package_list=/etc/update-system-packages
resync=yes
security_only=no
stable_upgrade=no
assume_yes=""

change_sources () {
    cd /etc/apt
    if [ ! -e sources.list.$1 ] ; then
        echo "E: Unknown sources list file: $1"
        exit 1
    fi
    rm sources.list
    ln -s sources.list.$1 sources.list
    cd -
}

resync_lists () {
    [ "$resync" = "no" ] && return
    echo "Resynchronizing package overview lists..."
    apt-get -qq update
    if [ $? -ne 0 ] ; then
        echo "E: Failed to resynchronize package overview files"
        exit 1
    fi
    echo "Resynchronization successful."
}

print_log () {
    egrep -v -e "is already the newest version|Reading Package Lists|Building Dependency Tree" update_system.log
}

while getopts hp:rsSy option ; do
    case $option in
        h)  echo "Usage: system-update [options]"
            echo ""
            echo "system-update is a simple shell script to keep a Debian GNU/Linux system up to"
            echo "date."
            echo ""
            echo "Options:"
            echo "  -h  this help text"
            echo "  -r  do NOT resychronize package overview lists"
            echo "  -p  specify a package list in place of /etc/update-system-packages"
            echo "  -s  upgrade with stable updates instead of the default"
            echo "  -S  upgrade with security updates only"
            echo "  -y  assume yes to all queries"
            echo ""
            echo "The script assumes that the /etc/apt/sources.list file is actually a"
            echo "symbolic link pointing to an actual sources.list file.  The following files"
            echo "must exist: sources.list.security and sources.list.default.  The .security"
            echo "version should only contain one line, similar to the following:"
            echo ""
            echo "  deb http://security.debian.org stable/updates main contrib non-free"
            echo ""
            echo "The .default file should contain whatever sources you desire for your system"
            echo "(unstable, testing, source, etc).  If the -s option was not chosen, the script"
            echo "will also (by default) read /etc/update-system-packages for a list of packages"
            echo "to upgrade to the latest version.  If the -y option is specified, apt-get"
            echo "will not ask for confirmation if additional packages need to be installed."
            echo "However, if any packages must be removed to upgrade, the script will skip that"
            echo "package."
            exit 0
            ;;
        p)  package_list=$OPTARG ;;
        r)  resync=no ;;
        s)  stable_upgrade=yes ;;
        S)  security_only=yes ;;
        y)  assume_yes="--yes --no-remove upgrade" ;;
        ?)  echo "E: Unrecognized option: $option"
            exit 1
            ;;
    esac
done

echo "Upgrading security packages..."
change_sources security
resync_lists
apt-get -q $assume_yes upgrade || (echo "failed to upgrade with security updates." && exit 1)
echo "Upgrade successful."
echo ""

if [ "$security_only" = "yes" ] ; then
    exit
fi

if [ "$stable_upgrade" = "yes" ] ; then
    echo "Upgrading stable packages..."
    change_sources stable
    resync_lists
    apt-get -q $assume_yes upgrade || (echo "failed to upgrade with stable updates." && exit 1)
    echo "Upgrade successful."
else
    echo "Upgrading requested packages from $package_list..."
    change_sources default
    resync_lists
    if [ ! -e $package_list ] ; then
        echo "E: Package list does not exist: $package_list"
        exit 1
    fi
    pkgs=""
    for pkg in `cat $package_list` ; do
        [ -z "$pkg" ] && continue
        if [ "$pkg" = "-" ] ; then
            echo "Upgrading $pkgs..."
            apt-get -q $assume_yes install $pkgs
            pkgs=""
            continue
        fi
        pkgs="$pkgs$pkg "
    done
    echo "Upgrade successful."
fi
