#!/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 = (
    'http:\/\/.*'                        => '',
    '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',
    'ooe680-m.*'			 => '@MIRROR@/OOE680',
    'oof680-m.*'			 => '@MIRROR@/OOF680',
    '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@/@MWS@',
    'mdbtools.*'                         => '@MIRROR@/SRC680',
    'libpixman-.*'			 => '@MIRROR@/SRC680',
    'glitz-.*'			         => '@MIRROR@/SRC680',
    'hunspell.*'			 => '@MIRROR@/SRC680',
    'biblio.*'				 => '@MIRROR@/SRC680',
# 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://kent.dl.sf.net/sourceforge/dejavu/',
# Win32 ant binaries ...
    'apache-ant-.*'                      => 'http://archive.apache.org/dist/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-1\.2\..*'                      => '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/',
# Windows .ico icons
    'ooo-windows-icons-.*'               => '@MIRROR@/SRC680',
# Updated libwpd
    'libwpd-*'                           => '@MIRROR@/libwpd',
# MS Works importer bits
    'libwps-*'                           => '@MIRROR@/SRC680',
# WPG importer bits
    'libwpg-*'                           => '@MIRROR@/SRC680',
# cairo sources
    'cairo-.*'			         => 'http://cairographics.org/releases/'
);

if ( '@UPSTREAM_NAME@' ne '' ) {
    $SRC_URLS{'@UPSTREAM_NAME@.*'} = '@MIRROR@/upstream-copy';
}

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

sub source_file($)
{
    my ($upstream_what) = @_;
    my $what = $upstream_what;

    if ( "$upstream_what" eq "l10n" ) {
        $what = "lang";
    }
    elsif ( "$upstream_what" eq "sdk" ) {
        $what = "sdk_oo";
    }

    my $tag = '@CVSTAG@';
    if ( '@UPSTREAM_SOURCE@' eq "YES" ) {
        return '@UPSTREAM_URI@' . "&lang=$upstream_what";
    }
    elsif ( '@UPSTREAM_SOURCE@' eq "MIRROR" ) {
        return '@UPSTREAM_NAME@' . '_' . $upstream_what . '.tar.bz2';
    }
    else {
        return '@CVSTAG@' . '-' . $what . '.tar.bz2';
    }
}

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

    for my $file ( @{$files} ) {
        if ( $file ne '' ) {
            my $to_check = "$dest/$file";
            if ( '@UPSTREAM_SOURCE@' eq "YES" ) {
                if ( $file =~ /^.*=([^=]*)/ ) {
                    $to_check = "$dest/" . '@UPSTREAM_NAME@' . '_' .  $1 . '.tar.bz2';
                }
            }

            if ( -f "$to_check" ) {
                print "Skipping $file\n";
            } else {
                print "No file $to_check\n";
                my $location = 'NOT_FOUND';
                for $regexp (keys %{$locations}) {
                    if ($file =~ m/^$regexp/) {
                        $location = $locations->{$regexp};
                        last;
                    }
                }

                my $to_get = $location."/$file";
                $location eq 'NOT_FOUND' && die "Can't find '$file' to download";
                if ( $location eq '' ) {
                    $to_get = $file;
                }

                webget( $to_get, $dest );
            }
        }
    }
}

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

# Files to download
@files = ();

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

# Defaults
print "Downloading files for @CVSTAG@\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, ( source_file( "system" ) );
#    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' );
    # Icons
    push @files, ( 'ooo-windows-icons-0.1.tar.bz2' ) if '@DISTRO@' =~ m/NovellWin32/;
}
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 '') {
    if ('@DEJAVU_FONTS_VER@' gt '2.11') {
        push @files, ( 'dejavu-ttf-@DEJAVU_FONTS_VER@.tar.bz2' );
    } else {
        push @files, ( 'dejavu-ttf-@DEJAVU_FONTS_VER@.tar.gz' );
    }
}

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

push @files, ( source_file( "core" ) );
push @files, ( '@OOO_CUSTOM_IMAGES@') if '@OOO_CUSTOM_IMAGES@';
push @files, ( '@OOO_EXTRA_ARTWORK@' ) if '@OOO_EXTRA_ARTWORK@';
# Temporary utf-8ization of bibliograpy bits
push @files, ( "biblio.tar.bz2" );

if ('@ENABLE_BINFILTER@' eq 'TRUE') {
    push @files, ( source_file( "binfilter" ) );
}

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

if ('@OOO_LANGS@' ne 'en-US' || '@BUILD_WIN32@' ne '' ) {
    push @files, ( source_file( "l10n" ) );
}

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' );
}

if ('@SYSTEM_LIBWPD@' eq 'NO') {
   push @files, ( 'libwpd-0.8.10.tar.gz' );
}

if ('@SYSTEM_LIBWPS@' eq 'NO') {
   push @files, ( 'libwps-0.1.0~svn20070129.tar.gz' );
}

if ('@SYSTEM_LIBWPG@' eq 'NO') {
   push @files, ( 'libwpg-0.1.0~cvs20070608.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, ( source_file( "system" ) );
}

if ($distro =~ m/Debian/ ||
    $distro =~ m/Ubuntu/ ||
    '@BUILD_WIN32@' ne '') {
    push @files, ( source_file( "sdk" ) );
}

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

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

print "Done\n";
