#!/usr/bin/perl -w
#
# SUMMARY:
#   This script checks for available debian packages, and lists packages by
#   apt-source.  It also lists packages which were held back.  If all packages
#   are up to date, it prints a message stating so.
#--------------------------------------------------------------------------
# COPYRIGHT INFORMATION - DO NOT REMOVE
# "Portions Copyright (c) 2000-2001 LinuxMagic Inc. All Rights Reserved.
#
# This file contains Original Code and/or Modifications of Original Code as
# defined in and that are subject to the Free Source Code License Version
# 1.0 (the 'License'). You may not use this file except in compliance with
# the License. Please obtain a copy of the License at:
#
# http://www.linuxmagic.com/opensource/licensing/
#
# and read it before using this file.
#
# The Original Code and all software distributed under the License are
# distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
# EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
# INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see
# the License for the specific language governing rights and limitations
# under the License."
#
# Please read the terms of this license carefully. By using or downloading
# this software or file, you are accepting and agreeing to the terms of this
# license with LinuxMagic Inc. If you are agreeing to this license on behalf
# of a company, you represent that you are authorized to bind the company to
# such a license. If you do not meet this criterion or you do not agree to
# any of the terms of this license, do NOT download, distribute, use or alter
# this software or file in any way.
#
#  Authors: John Bratlien <john@wizard.ca>
#           Josh Wilsdon <josh@wizard.ca>
#
#  DO NOT MODIFY WITHOUT CONSULTING THE LICENSE
#
#-------------------------------------------------------------------------
# Version: $Id: lm-apt-check.pl,v 1.12 2003/09/10 02:05:34 michael Exp $
#-----------------------------------------------------------------------
# Prep Section
# Set AutoFlush
#$|;

use strict;
use vars qw{    $advertizement_head
                $advertizement_foot
                $hostname
                $output_nothing
                $serverinfo
                $APT_PROG
		$PROXY_SERVER_ADDRESS
};
#------------------------------------------------------------------------
# Set Globals and Configurations
# 
$APT_PROG="/usr/bin/apt-get";
#$PROXY_SERVER_ADDRESS="http://192.100.1.1:3128/";

$output_nothing = 0;            # Set this to 0 if you don't want mails when nothing new is available

# End Configuration, do not edit below
#------------------------------------------------------------------------
if (defined($PROXY_SERVER_ADDRESS)) {
        # Set Environment for Proxy Server
        $ENV{'http_proxy'}=$PROXY_SERVER_ADDRESS;
};

$hostname = `hostname`;
chomp($hostname);
$serverinfo = `uname -a`;
chomp($serverinfo);

# header advertizement
$advertizement_head  = "LinuxMagic System Update Notification Alert\n\n";
$advertizement_head .= "If you have received this Email, then you have software packages\n";
$advertizement_head .= "installed on this server, $hostname, that have been updated, upgraded,\n";
$advertizement_head .= "or have had a security update release provided by the package maintainer.\n\n";
$advertizement_head .= "Normally, you would want to upgrade the packages indicated when this occurs.\n\n";
$advertizement_head .= "Server Information: ($serverinfo)\n\n";

# footer advertizement
$advertizement_foot  = "Please contact support\@linuxmagic.com if you have any questions, or\n";
$advertizement_foot .= "to have us upgrade these for you. http://www.linuxmagic.com (604) 682-0300\n";

sub init
{
    # Used to make sure that everything is ready to run
    # First check to see if we are running as root
    if ($> ne 0) {
        print STDERR "You MUST be root to run this program! Aborting!\n";
        return 1;
    } else {
        # We are running as root, now check that apt-get update runs
        system ("$APT_PROG -qq update");
        my $exit_value = $? >> 8;
        if ( $exit_value ne 0 ) { 
            print STDERR "Cannot run apt-get update: $|";
            return 1;
        } else {
            return 0;
        }
    }
}

sub main
{
    my @pkgs = ();
    my @pkgs_held_back = ();
    my %hash = ();
    my ($did_something, $key, $line, $pkg, $pkgs_held_back);
     
    if (init() != 0) {
        exit (1);
    }

    # Initialization
    $did_something = 0;

    open(FILE_H, "$APT_PROG -qs upgrade|");
    while ($line=<FILE_H>) {

        if ($line =~ m/^The following packages have been kept back/) {
            while ($line=<FILE_H>) {
                if ($line =~ /^  (.{0,})/) {
                    # packages held back all begin with whitespace, and occur after
                    # the line that begins "The following packages have been..."
                    @pkgs = split(' ', $1);
                    foreach $pkg (@pkgs) {
                        chomp($pkg);
                        push(@pkgs_held_back, $pkg);
                    }
                } else {
                    last; # while()
                }
            }
        } 
    
        # get all lines that begin with INST remember package name and type
        #if ($line =~ /^Inst ([^ ]{0,}).{0,}\(.{0,}(Debian:[^\)]{0,})/) {
        if ($line =~ /^Inst ([^ ]+).+\([^ ]+.([^\)]+)/) {
            #strip out the bit after Inst <pkg name>
	    #strip out the distribution
    
            #list by package type: security, unstable ...
            push(@{$hash{$2}}, $1);
        }
    }
    
    if (($output_nothing != 0) || (scalar(@pkgs_held_back) > 0) || (scalar(keys %hash) > 0)) {
        # print this only if we are going to have other output
        print $advertizement_head;
    }

    if (scalar(@pkgs_held_back) > 0) {
        $did_something=1;
        print "Packages held back:\n\n";
        foreach $pkgs_held_back(sort @pkgs_held_back) {
            print "\t$pkgs_held_back\n";
        }
        print "\n";
    }
    
    foreach $key (keys %hash) {
        $did_something=1;
	my $printable_key = $key;
	$printable_key =~ s/:/\ \(/;
	$printable_key =~ s/$/\)/;
        print "Installable packages in $printable_key:\n\n";
        foreach $pkg (sort @{$hash{$key}}) {
            print "\t$pkg\n";
        }
        print "\n";
    }
    
    if (($did_something == 0) && ($output_nothing != 0)) {
        print "All packages up to date.\n\n";
    }

    if (($output_nothing != 0) || (scalar(@pkgs_held_back) > 0) || (scalar(keys %hash) > 0)) {
        # print this only if we are going to have other output
        print $advertizement_foot;
    }

    # success
    exit(0);
}


# Call main
main();
