#! /usr/bin/perl
#
#
# Use:
if(!caller() && grep(/\-(h|\-help)/i, @ARGV))
{
	@ARGV = grep(!/\-(h|\-help)/i, @ARGV);
	print STDOUT << 'ENDOFHELP';
./downloadIMDI.pl [--find 'RegExp'] [--replace 'ReplacementString'] 'URL'

Recursively download IMDI corpus and session files.
Downloads all <CorpusLink name="">URL.imdi</CorpusLink>
files and looks inside them for new CorpusLink tags.

URL.imdi is the full URL of the top node, e.g.,
'http://145.18.230.99/corpus/IMDI/IFAcorpus.imdi'
You can add as many top-node URL's as you like.
This perl script needs LWP::Simple. If needed,
you can install this perl module with the command:
# perl -MCPAN -e 'install LWP::Simple' 

-f
--find RegExp
-r
--replace ReplacementString
Find and replace all occurences of 'RegExp' by 'ReplacementString'. Uses
perl regular expressions, i.e.,
$FileText =~ s/RegExp/ReplacementString/sg;

-h
--help
This text.

-l
--license
License text.

ENDOFHELP
	exit;
};
# 
#
#
if(!caller() && grep(/\-(l|\-license)/i, @ARGV))
{
	@ARGV = grep(!/\-(l|\-license)/i, @ARGV);
	print STDOUT << 'ENDOFLICENSE';
###############################################################################
#
# Copyright R.J.J.H. van Son © 2002
#
# Author Rob van Son
# Institute of Phonetic Sciences & ACLC
# University of Amsterdam
# Herengracht 338
# NL-1016CG Amsterdam, The Netherlands
# Email: Rob.van.Son@hum.uva.nl
#        rob.van.son@workmail.com
# WWW  : http://www.fon.hum.uva.nl/rob/
# mail:  Institute of Phonetic Sciences
#        University of Amsterdam
#        Herengracht 338
#        NL-1016CG Amsterdam
#        The Netherlands
#        tel +31 205252183
#        fax +31 205252197
#
# License for use and disclaimers
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#
#######################################################
ENDOFLICENSE
	exit;
};
#
package downloadIMDI;
use LWP::Simple;

my $MatchString = "";
my $ReplaceString = "";

sub getIMDItree	# ($URL) -> noFiles
{
	my $URL = shift;
	my $NoFiles = 0;
	my $FileContents;
	
	# Get base URL
	$URL =~ m![^\/]+$!;
	my $BaseURL = $`;
	$BaseURL =~ m!\://!;
	my $Server = $`.$&;
	my $FilePath = $';
print STDERR "$URL\n";
	if($FileContents = get($URL))
	{
		# Global replace of text
		$FileContents = s/$MatchString/$ReplaceString/sg if $MatchString;
		# Extract File Name
		$URL =~ m!([\w\-\.]+)(\?|$)!i;
		my $NewFileName = $1;
		# Do NOT redo an existing file
		return $NoFiles if -s $NewFileName;
		
		# File doesn't exist, go on
		++$NoFiles;
		# Write file
		open(IMDIFILE, ">$NewFileName") || die "$NewFileName: $!\n";
		print IMDIFILE $FileContents;
		close(IMDIFILE);
		
		# Recurse into file
		while($FileContents =~ m!\<CorpusLink\b[^\>]+\>\s*!is)
		{
			$FileContents = $';
			$FileContents =~ m!\s*\</CorpusLink\s*\>\s*!is;
			my $NewURL = $`;
			my $FileContents = $';
			unless($NewURL =~ m!^\s*(ht|f)tp://!)
			{
				if($NewURL =~ m!^/!)
				{
					$NewURL = $Server.$NewURL;
				}
				else
				{
					$NewURL = $BaseURL.$NewURL;
				};
			};
			$NoFiles += getIMDItree($NewURL) if $NewURL =~ /\.imdi\b/i;
		};
	};
	
	return $NoFiles;
}

# Run only when called on the command line
unless(caller())
{
	# Get Find Replace strings
	my $i = 0;
	while($i < scalar(@ARGV))
	{
		if($ARGV[$i] =~ /\-(f|\-f(ind)?)/i)
		{
			$MatchString = $ARGV[$i+1];
			@ARGV = splice(@ARGV, $i, 2);
		}
		elsif($ARGV[$i] =~ /\-(r|\-r(eplace)?)/i)
		{
			$ReplaceString = $ARGV[$i+1];
			@ARGV = splice(@ARGV, $i, 2);
		}
		else
		{
			++$i;
		};
	};
	
	# Run arguments
	foreach $URL (@ARGV)
	{
		print STDERR "$URL\n";
		my $FilesDownloaded = getIMDItree($URL);
		print "Downloaded $FilesDownloaded files\n";
	};
}
