#!/usr/bin/python


#nice apt-get -s -o Debug::NoLocking=true upgrade | grep ^Inst

import apt_pkg
import apt
import os
import sys
from optparse import OptionParser
import gettext
import traceback

SYNAPTIC_PINFILE = "/var/lib/synaptic/preferences"

def _(msg):
    return gettext.dgettext("update-notifier", msg)

def _handleException(type, value, tb):
    sys.stderr.write("E: "+ _("Unkown Error: '%s' (%s)\n") % (type,value))
    sys.stderr.write("Traceback:\n")
    traceback.print_tb(tb)
    sys.exit(-1)


def run(options=None):
    os.nice(19)
    apt_cache = apt.Cache(None,None,True)
    apt_cache.upgrade(True)

    if options and options.show_package_names:
        for pkg in apt_cache:
            if pkg.marked_upgrade and not pkg.marked_install:
                sys.stdout.write("u;%s;%s;%s;%s\n" % (pkg.fullname, pkg.installed.version, pkg.candidate.version, pkg.candidate.origins[0].codename))
            elif pkg.marked_delete:
                sys.stdout.write("d;%s;%s;;\n" % (pkg.fullname, pkg.installed.version))
            elif pkg.marked_install:
                sys.stdout.write("i;%s;;%s;%s\n" % (pkg.fullname, pkg.candidate.version, pkg.candidate.origins[0].codename))
    if options and options.show_size:
        if options.readable_output:
             sys.stdout.write(_("Need to get %d kB of archives.\n") % (float(apt_cache.required_download) / 1000 + 0.5));
             if apt_cache.required_space < 0:
                 sys.stdout.write(_("After this operation, %d kB disk space will be freed.\n") % -(float(apt_cache.required_space) / 1000 - 0.5));
             else:
                 sys.stdout.write(_("After this operation, %d kB of additional disk space will be used.\n")
                                    % (float(apt_cache.required_space) / 1000 + 0.5));
        else:
             sys.stdout.write("%d;%d\n" % (apt_cache.required_download, apt_cache.required_space))
    else:
        upgrade_count = 0
        delete_count = 0
        install_count = 0

        for pkg in apt_cache:
            if pkg.marked_upgrade and not pkg.marked_install:
                upgrade_count += 1
            elif pkg.marked_delete:
                delete_count += 1
            elif pkg.marked_install:
                install_count += 1

        if options and options.readable_output:
            print _("%i upgraded, %i newly installed, %i to remove") % (upgrade_count, install_count, delete_count)
        else:
            sys.stdout.write("%i;%i;%i\n" % (upgrade_count, install_count, delete_count))

if __name__ == "__main__":
    # setup a exception handler to make sure that uncaught stuff goes
    # to the notifier
    sys.excepthook = _handleException

    # gettext
    APP="update-notifier"
    DIR="/usr/share/locale"
    gettext.bindtextdomain(APP, DIR)
    gettext.textdomain(APP)

    # check arguments
    parser = OptionParser()
    parser.add_option("-p",
                      "--package-names",
                      action="store_true",
                      dest="show_package_names",
                      help=_("Show the packages that are going to be installed/upgraded"))
    parser.add_option("-r",
                      "--human-readable",
                      action="store_true",
                      dest="readable_output",
                      help=_("Show human readable output on stdout"))
    parser.add_option("-s",
                      "--size",
                      action="store_true",
                      dest="show_size",
                      help=_("Show required download/install size"))
    (options, args) = parser.parse_args()

    # run it
    run(options)
