#!/usr/bin/perl

use strict;
use warnings FATAL => 'all';
use LWP::UserAgent;

# Upstream version
my $version = '1.0';

# check we are root
if ($> != 0) {
    print "You must run $0 as root.\n";
    exit 2;
}

# Check our destination directory exists (or create) and is sane
# (i.e. avoid symlink attacks - CVE-2008-5379[0])
# 
# We cannot use mktemp as this path should be available to
# netdisco-mibs-install, invoked independently.
my $destdir = '/tmp/netdisco-mibs';
if (-e $destdir) {
    my @stat = stat($destdir);
    my $mode = sprintf('%04o', $stat[2] & 07777);
    my $maxmode = 0755;
    if (! -d $destdir or $stat[4] != 0 or $maxmode - $mode) {
	print "$destdir exists and is not a root-owned directory with " .
	    "permissions set to 0755 (or less)\n";
	exit 3;
    }
} else {
    unless (mkdir($destdir)) {
	print "Could not create destination directory $destdir\n";
	exit 3;
    }
}

my $site  = "http://downloads.sourceforge.net/project/netdisco/netdisco-mibs/$version";
my $target = "netdisco-mibs-$version.tar.gz";
my $destfile = "$destdir/$target";
my $homepage = 'http://sourceforge.net/projects/netdisco/files/netdisco-mibs/';

unlink($destfile) if -e $destfile;

# try to download $site/$target
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
my $request = HTTP::Request->new(GET => "$site/$target");
my $response = $ua->request($request, $destfile);

if ($response->is_success) {
    print "Downloaded ok, please now run netdisco-mibs-install.\n";
    exit 0;
}
else {
    print $response->status_line, "\n" if $ENV{MIBS_DEBUG};
}

print "\nSorry, it has not been possible to download the Netdisco MIB bundle.\n";
print "Please go to the Netdisco Sourceforge page, and download $target:\n";
print "    $homepage\n";
print "\nSave this file to $destfile and then run netdisco-mibs-install.\n";

exit 1;
__END__

=head1 NAME

netdisco-mibs-download - Downloader for the Netdisco MIB bundle

=head1 VERSION

This document refers to version 1.0700 of netdisco-mibs-download

=head1 DESCRIPTION

Run this program to download the Netdisco MIB files bundle from Sourceforge.
If the download is successful, you can run the netdisco-mibs-install program
to unpack and install them.

If the download fails, you can visit the Netdisco Sourceforge downloads page,
details of which are given by this program on failure, and get the bundle
yourself.

=head1 ENVIRONMENT

Setting the environment variable C<MIBS_DEBUG> to a true value will display
information about failed mirror site downloads.

=head1 EXIT CODES

=over 4

=item 0 - Successful download

=item 1 - Failed download, possibly no available mirror

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

=item 3 - Error regarding the download directory

=back

=head1 AUTHOR

Oliver Gorwits C<< <oliver@cpan.org> >>

=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

