#!/usr/bin/perl -w

sub webget($$)
{
    my ($src, $dest) = @_;

    print "Get $src -> $dest\n";
    system ("cd $dest ; $WGET $src") != 0 && die "Failed fetch";
}

sub usage {
    print STDERR "\ndownload\n";
    print STDERR "Syntax:	download    [--help] \n\n";
    print STDERR "  download's behavior is coded by your configure options eg.\n";
    print STDERR "  if you configure with --with-system-gcc it will not download\n";
    print STDERR "  gcc & binutils\n";
};


%SRC_URLS = (
    'binutils-.*'			 => '@MIRROR@/support',
    'gcc-.*'			 	 => '@MIRROR@/support',
    'src680-m.*'			 => '@MIRROR@/SRC680',
    'ooa680-m.*'			 => '@MIRROR@/OOA680',
    'oob680-m.*'			 => '@MIRROR@/OOB680',
    'ooc680-m.*'			 => '@MIRROR@/OOC680',
    'ood680-m.*'			 => '@MIRROR@/OOD680',
    'ooo680-m.*'			 => '@MIRROR@/OOO680',
    'OOO_2_0_2.*'			 => '@MIRROR@/OOB680',
    'OOO_2_0_3.*'			 => '@MIRROR@/OOC680',
    'OOO_2_0_4.*'			 => '@MIRROR@/OOD680',
    'images_gnome-.*'                    => '@MIRROR@/SRC680',
    'images_kde-.*'                      => '@MIRROR@/SRC680',
    'extras-.*'				 => '@MIRROR@/SRC680',
    'ooo_custom_images.*'                => '@MIRROR@/SRC680',
    'ooo_crystal_images.*'               => '@MIRROR@/SRC680',
    'cli_types.*'                        => '@MIRROR@/OOD680',
    'mdbtools.*'                         => '@MIRROR@/SRC680',
    'libpixman-.*'			 => '@MIRROR@/SRC680',
    'cairo-.*'			         => '@MIRROR@/SRC680',
    'glitz-.*'			         => '@MIRROR@/SRC680',
    'hunspell.*'			 => '@MIRROR@/SRC680',
    'biblio.*'				 => '@MIRROR@/SRC680',
    'xt-.*'                              => '@MIRROR@/xt',
# CalcSolver bits
    'lp_solve_*'			 => '@MIRROR@/SRC680',
# Graphite rendering bits
    'silgraphite-.*'                     => '@MIRROR@/SRC680',
# Win32 bits:
    'unicows\.exe'			 => 'http://download.microsoft.com/download/b/7/5/b75eace3-00e2-4aa0-9a6f-0b6882c71642',
    # from http://www.microsoft.com/downloads/release.asp?releaseid=30682'
    'dbghinst\.EXE'			 => 'http://download.microsoft.com/download/platformsdk/Redist/5.0.2195.1/W9XNT4/EN-US',
    '5_11_98Odma20\.zip'		 => '@MIRROR@/support',
    'openclipart-.*'			 => 'http://www.openclipart.org/downloads/@OPENCLIPART_VER@',
    'dejavu-ttf-.*'			 => 'http://heanet.dl.sf.net/sourceforge/dejavu/',
# Win32 ant binaries ...
    'apache-ant-.*'                      => 'http://apache.mirror.positive-internet.com/ant/binaries',
# Mozilla source and libraries
    'mozilla-source-1\.7\.5.*'		 => 'http://ftp.mozilla.org/pub/mozilla.org/mozilla/releases/mozilla1.7.5/source/',
    'glib-.*'                            => 'http://ftp.gtk.org/pub/gtk/v1.2/',
    'libIDL-.*'                          => 'http://ftp.mozilla.org/pub/mozilla.org/mozilla/libraries/source/',
    'wintools\.zip'                      => 'http://ftp.mozilla.org/pub/mozilla.org/mozilla/source/',
# SDK bits
    'unowinreg\.dll'                      => 'http://tools.openoffice.org/unowinreg_prebuild/680/'
);

$WGET='/usr/bin/wget';

sub download_files($$$)
{
    my ($files, $locations, $dest) = @_;

    for my $file ( @{$files} ) {
        if ( $file ne '' ) {
            if (-f "$dest/$file") {
                print "Skipping $file\n";
            } else {
                print "No file $dest/$file\n";
		my $location = '';
		for $regexp (keys %{$locations}) {
		    if ($file =~ m/^$regexp/) {
			$location = $locations->{$regexp};
			last;
		    }
		}
		$location eq '' && die "Can't find '$file' to download";
                webget( $location."/$file", $dest );
            }
        }
    }
}

if (!-d "@SRCDIR@") {
    `mkdir -p @SRCDIR@`;
}

# Files to download
@files = ();

while ($arg = shift @ARGV) {
    if ( $arg eq '--help' ) {
        &usage and exit(0);
    }
}

# Defaults
$tag = '@CVSTAG@';
print "Downloading files for $tag\n";

-x $WGET || die "Can't find wget - install it and try again\n";

if ('@SYSTEM_GCC@' eq '') {
    push @files, ( 'gcc-3.4.1.tar.bz2', 'gcc-3.4.1-Enums.diff', 'gcc-3.4.1-VisibilityPatch2.diff' );
}

# Misc. bits for Win32
if ('@BUILD_WIN32@' ne '') 
{
    push @files, ( 'unicows.exe', 'dbghinst.EXE' );
    push @files, ( "$tag-system.tar.bz2" );
#    push @files, ( '5_11_98Odma20.zip' ); # - ODMA - for when it works.
    push @files, ( "apache-ant-1.6.5-bin.tar.gz" );
    push @files, ( '@BUILD_MOZILLA_SOURCE@' );
    push @files, ( 'glib-1.2.10.tar.gz' );
    push @files, ( 'libIDL-0.6.8.tar.gz' );
# this contains binaries and source, we just use the source
    push @files, ( 'wintools.zip' );
}
else
{
    if ('@ENABLE_MONO@' ne 'FALSE') {
	push @files, ( 'cli_types.dll' );
	push @files, ( 'cli_types_bridgetest.dll' );
    }
}
if ('@CAIRO_ENABLED@' eq 'TRUE' && '@SYSTEM_CAIRO@' ne 'TRUE') {
    push @files, ( 'cairo-@CAIRO_VER@.tar.gz' );
    if ('@BUILD_WIN32@' eq '' ) {
        push @files, ( 'glitz-@GLITZ_VER@.tar.gz' );
    }
}

if ('@OPENCLIPART_VER@' ne '') {
    push @files, ( 'openclipart-@OPENCLIPART_VER@.tar.bz2' );
}

if ('@DEJAVU_FONTS_VER@' ne '') {
    push @files, ( 'dejavu-ttf-@DEJAVU_FONTS_VER@.tar.gz' );
}

if ('@MDBTOOLS_SRC@' ne '') {
    push @files, ( '@MDBTOOLS_SRC@' );
}

push @files, ( "$tag-core.tar.bz2" );
push @files, ( '@OOO_CUSTOM_IMAGES@') if '@OOO_CUSTOM_IMAGES@';
push @files, ( '@OOO_EXTRA_ARTWORK@' ) if '@OOO_EXTRA_ARTWORK@';
push @files, ( "xt-20051206-src-only.zip" );
# Temporary utf-8ization of bibliograpy bits
push @files, ( "biblio.tar.bz2" );

if ('@ENABLE_BINFILTER@' eq 'TRUE') {
    push @files, ( "$tag-binfilter.tar.bz2" );
}

if ('@USE_PREBUILD_UNOWINREG_DLL@' eq 'YES') {
    push @files, ( "unowinreg.dll" );
}

if ('@OOO_LANGS@' ne 'en-US' || '@BUILD_WIN32@' ne '' ) {
    push @files, ( "$tag-lang.tar.bz2" );
}

if ('@HUNSPELL_UNO_SRC@' ne '') {
    push @files, ( '@HUNSPELL_UNO_SRC@' );
}

if ('@HUNSPELL_LIB_SRC@' ne '') {
    push @files, ( '@HUNSPELL_LIB_SRC@' );
}

if ('@GRAPHITE_SRC@' ne '') {
    push @files, ( '@GRAPHITE_SRC@' );
}

if ('true' ne '') {
   push @files, ( 'lp_solve_5.5.tar.gz' );
}

my $distro = '@DISTRO@';
if ($distro eq 'Ark' || $distro eq 'Ark64' ||
    $distro eq 'DroplineGNOME' ||
    $distro eq 'Frugalware' || $distro eq 'Frugalware64' ||
    $distro eq 'Gentoo.conf' ||
    $distro eq 'QiLinux' ||
    $distro eq 'SUSE-9.1' || $distro eq 'SUSE-9.2' || $distro eq 'SUSE-9.3' ||
    $distro eq 'translate-org-za-linux' || $distro eq 'translate-org-za-win32' ) {
    push @files, ( "$tag-system.tar.bz2" );
}

if ($distro =~ m/Debian/ ||
    $distro =~ m/Ubuntu/ ||
    '@BUILD_WIN32@' ne '') {
    push @files, ( "$tag-sdk_oo.tar.bz2" );
}

download_files (\@files, \%SRC_URLS, '@SRCDIR@');

open (FILELIST, ">download.list");
print FILELIST join ("\n", @files);
print FILELIST "\n";

print "Done\n";
