#! /usr/bin/perl --

$version= '1.0.1'; # This line modified by Makefile

# Options
$logger = '/usr/bin/logger';
$sendtoscreen = 0;
$log_priority = 'local6';

sub usageversion {
    print(STDERR <<END)
Debian GNU/Linux dpkg-logger $version.  Copyright (C) 1999
Ben Collins.  This is free software; see the GNU General Public Licence
version 2 or later for copying conditions.  There is NO warranty.

Usage:
 dpkg-logger [ --stdout ] <pkgname> <level> message|<message

 --stdout	Also echo message to stdout as well as log

 <pkgname>	The package that is sending this message

 <level>	One of: info, warn, alert, debug
		Please read the policy manual for usage of levels.
		Debug is not for production, but is for testing only.

 message	The message you wish to have logged. This is either on
		the command line or from stdin (command line takes
		precedence)
END
        || &quit("failed to write usage: $!");
}


while (@ARGV) {
    $_= shift(@ARGV);
    last if m/^--$/;
    if (!m/^-/) {
        unshift(@ARGV,$_); last;
    } elsif (m/^--(help|version)$/) {
        &usageversion; exit(0);
    } elsif (m/^--stdout$/) {
        $sendtoscreen = 1;
    } else {
        &badusage("unknown option \`$_'");
    }
}

@ARGV > 1 || &badusage("Missing argument(s)");

$package_name = shift(@ARGV);
$_ = shift(@ARGV);
m/(^debug$|^info$|^warn$|^alert$)/ || &badusage("Unknown log level $_");
$log_level = $_;
$message = "@ARGV";

$message=join('', <>) if ($message eq "");
chomp $message;

if (-x "$logger") {
    print "$message\n" if ($sendtoscreen);
    exec ("$logger -p ${log_priority}.${log_level} -t pkg:$package_name $message");
} else {
    print "$message\n";
}

exit(0);

sub badusage { print STDERR "dpkg-logger: @_\n\n"; &usageversion; exit(2); }
