#!/bin/sh
set -e

TARBALL="netdisco-mibs-0.6.tar.gz"
UNPACK_DIR="netdisco-mibs-0.6"
SHAREDIR="/usr/share/netdisco/mibs"
CONTRIBDIR="/usr/share/doc/netdisco-mibs-installer/contrib"

# Check if we are root
if [ "$UID" -ne "0" ]; then
    echo >&2 "$0: error: you must be root to run this program"
    exit 1
fi

rm -rf /tmp/$UNPACK_DIR

if [ ! -s /tmp/$TARBALL ]; then
    echo >&2 "$0: error: /tmp/$TARBALL must exist"
    echo >&2 "Have you run netdisco-mibs-download ?"
    exit 2
fi

if [ "`ls -l /tmp/$TARBALL | awk '{print $3}'`" != root ]; then
    echo >&2 "$0: error: file not owned by root: /tmp/$TARBALL"
    exit 3
fi

cd /tmp
tar -x -z -f $TARBALL
chmod -R og-w $UNPACK_DIR
chown -R root:root $UNPACK_DIR
cd $UNPACK_DIR

if [ ! -d $SHAREDIR ]; then
    echo >&2 "$0: error: $SHAREDIR must exist"
    exit 4
fi

for d in `find -mindepth 1 -type d`; do
    cp -fpR $d $SHAREDIR/
done

if [ ! -d $CONTRIBDIR ]; then
    echo >&2 "$0: error: $CONTRIBDIR must exist"
    exit 5
fi

for f in `find -maxdepth 1 -type f`; do
    cp -fp $f $CONTRIBDIR/
done

rm -rf /tmp/$UNPACK_DIR

echo "The MIB files in $SHAREDIR have now been updated."
exit 0

# trick to embed POD in shell
: <<=cut

=pod

=head1 NAME

netdisco-mibs-install - Installer for the Netdisco MIB bundle

=head1 VERSION

This document refers to version 1.0000 of netdisco-mibs-install

=head1 DESCRIPTION

Run this program after you have run netdisco-mibs-download.

The downloaded MIB bundle is unpacked and MIB files are installed to
/usr/share/netdisco/mibs. Remaining files from the bundle are installed to
/usr/share/doc/netdisco-mibs-installer/contrib.

The Debian NetDisco package is pre-configured to use /usr/share/netdisco/mibs.
You could also set your local snmp.conf file to reference the vendor-specific
directories of this location.

=head1 EXIT CODES

=over 4

=item 0 - Successful install

=item 1 - Program must be run as root, and you are not root

=item 2 - /tmp/netdisco-mibs-0.6.tar.gz is missing - have you run netdisco-mibs-download ?

=item 3 - /tmp/netdisco-mibs-0.6.tar.gz is not owned by root

=item 4 - Directory /usr/share/netdisco/mibs does not exist

=item 5 - Directory /usr/share/doc/netdisco-mibs-installer/contrib does not exist

=back

=head1 AUTHOR

Oliver Gorwits C<< <oliver.gorwits@oucs.ox.ac.uk> >>

=head1 COPYRIGHT & LICENSE

Copyright (c) The University of Oxford 2007. All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published by the
Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
St, Fifth Floor, Boston, MA 02110-1301 USA

=cut

