#!@l_prefix@/lib/openpkg/bash
##
##  audit -- OpenPKG Tool Chain "audit" command
##  Copyright (c) 2000-2005 OpenPKG Foundation e.V. <http://openpkg.net/>
##  Copyright (c) 2000-2005 Ralf S. Engelschall <http://engelschall.com/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   configuration
logfile="@l_prefix@/var/openpkg-audit/openpkg-audit.log"

#   command line parsing
opt_m=""
opt_r=no
opt_h=no
opt_l=no
opt_n=0
opt_f=no
OPTIND=1
while getopts m:rln:fh opt; do
    case ${opt} in
        m ) opt_m=$OPTARG ;;
        r ) opt_r=yes ;;
        l ) opt_l=yes ;;
        n ) opt_n=$OPTARG ;;
        f ) opt_f=yes ;;
        h ) opt_h=yes ;;
        * ) echo "openpkg:audit:ERROR: invalid option \"${opt}\"" 1>&2; exit 1 ;;
    esac
done
shift $(($OPTIND - 1))

#   dispatch into commands
if [ ".${opt_h}" = .yes ]; then
    #   show usage message
    echo "Usage: openpkg audit [-m <message>] [-r] [-l] [-n <lines>] [-f] [-h]" 1>&2
elif [ ".${opt_l}" = .yes ]; then
    #   show logfile
    if [ ".${opt_f}" = .yes ]; then
        tail -f ${logfile}
    elif [ ".${opt_n}" != .0 ]; then
        tail -n ${opt_n} ${logfile}
    else
        cat ${logfile}
    fi
elif [ ".${opt_m}" != . ]; then
    #   add manual administrator entry to logfile
    if [ ! -w $logfile ]; then
        echo "openpkg:audit:ERROR: unsufficient privileges to write to \"$logfile\"" 1>&2
        exit 1
    fi

    #   determine date and user
    date=`date '+%Y-%m-%d %H:%M:%S'`
    user=`@l_prefix@/lib/openpkg/shtool echo -e '%u'`

    #   create logfile entry
    if [ ".${opt_r}" = .yes ]; then
        echo "$date user=$user, $opt_m" >>$logfile
    else
        echo "$date user=$user, message=\"$opt_m\"" >>$logfile
    fi
fi
exit 0

##
##  MANUAL PAGE
##

=pod

=head1 NAME

B<openpkg audit> - OpenPKG Auditing Facility

=head1 SYNOPSIS

B<openpkg audit> [B<-m> I<message>] [B<-r>]

B<openpkg audit> [B<-l>] [B<-n> I<lines>] [B<-f>]

B<openpkg audit> [B<-h>]

=head1 DESCRIPTION

The B<openpkg audit> command is the frontend to the OpenPKG auditing
logfile which is currently written by the B<openpkg rpm>
command in case the RPM database was changed.

=head1 OPTIONS

The following command line options exist:

=over 4

=item B<-m> I<message>

Adds I<message> to the auditing logfile as a C<message="..."> entry.

=item B<-r>

In conjunction with B<-m>, adds I<message> to the auditing logfile as a
raw message.

=item B<-l>

Shows the auditing logfile.

=item B<-n> I<lines>

In conjunction with B<-l>, shows only the last I<lines> number of lines.

=item B<-f>

In conjunction with B<-l>, follows the logfile.

=item B<-h>

Shows a command usage.

=back

=head1 FILES

The following paths are used by B<openpkg audit>:

=over 4

=item F<@l_prefix@/bin/openpkg>

=item F<@l_prefix@/var/openpkg-audit/openpkg-audit.log>

=back

=head1 SEE ALSO

C<openpkg man rpm>.

=head1 AUTHOR

Ralf S. Engelschall E<lt>rse@openpkg.orgE<gt>

=cut

