#! /usr/bin/perl
#
# Use:
# ConstructChunkcdrom.pl <Speaker> [fileglob] [fileglob] ...
#
# Copies all necessary files to the Chunkcdrom directory for distribution
#
###############################################################################
#
# Copyright R.J.J.H. van Son © 2000, 2001
#
# 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.
#
#
#######################################################
#
my $CVSsource = "/home/CVSrepository";
my $ModuleName = 'SLcorpus';
my $SourceSpeechDir = '/u/rob/SLspeech/oggchunks';
my $ChunkCDromDir = '/data1/rob/ChunkCDrom';
my @PraatList = ();

# Give a glob on the speakers you would like to use
my $Speakers = shift(@ARGV) || die "No speech files to distribute\n";

# Start README file
my @StartLines = ();
@StartLines = `cat $ChunkCDromDir/README|grep '\#'|grep -v '$Speakers @ARGV'` if -e "$ChunkCDromDir/README";
open(README, ">$ChunkCDromDir/README") || die "$ChunkCDromDir/README: $!\n";
print README @StartLines;
print README "# This distribution contains compressed (Ogg Vorbis format) speech\n" 
	unless scalar(@StartLines);
print README "# from the IFA Spoken Language Corpus\n" 
	unless scalar(@StartLines);
print README "# $Speakers @ARGV\n";
close(README);

# Unpack the corpus info if it has been compressed and remove the tar
`cd $ChunkCDromDir;tar -xzf SLcorpus.tgz` if -e "$ChunkCDromDir/SLcorpus.tgz";
`cd $ChunkCDromDir;rm ./SLcorpus.tgz` if  -e "$ChunkCDromDir/SLcorpus.tgz" && -d "$ChunkCDromDir/SLcorpus";

# First, extract the latest CVS tree (yes, we dump it on everything!)
`cd $ChunkCDromDir; cvs -d $CVSsource export -D tomorrow '$ModuleName'`;

# Add comment to README
open(README, ">>$ChunkCDromDir/README") || die "$ChunkCDromDir/README: $!\n";
print README "./SLcorpus:\tThe complete IFA Spoken Language Corpus\n";
close(README);

# Copy license info (this is dumped everywhere to!)
`cp --update $ChunkCDromDir/SLcorpus/*.txt $ChunkCDromDir/`;
`cp --update $ChunkCDromDir/SLcorpus/*.html $ChunkCDromDir/`;
`cp --update $ChunkCDromDir/SLcorpus/*.jpg $ChunkCDromDir/`;

# Add comment to README
open(README, ">>$ChunkCDromDir/README") || die "$ChunkCDromDir/README: $!\n";
my @LicenseInfo = `cd $ChunkCDromDir; ls -1 ./*.txt ./*.html ./*.jpg`; 
map {chomp} @LicenseInfo; # Remove newlines
print README join(":\tProject and Licensing information\n", @LicenseInfo);
print README ":\tProject and Licensing information\n"; # Last line
close(README);

# copy Praat directory
# (note, to actually FILL the Praat directory, run the 'GetPraat <secret-number>' script inside it)
my $CreateDir = "$ChunkCDromDir/Praat";
my $DirExists = -d "$CreateDir";
($return_value = mkdir("$CreateDir", 0744) || die "$CreateDir: $!\n") unless $DirExists;

`cp --update --recursive $ChunkCDromDir/SLcorpus/Praat/* $CreateDir`;

# Add comment to README
open(README, ">>$ChunkCDromDir/README") || die "$ChunkCDromDir/README: $!\n";
print README "./Praat:\tThe praat speech tool (multiple platforms)\n";
close(README);

# copy Ogg Vorbis directory
my $CreateDir = "$ChunkCDromDir/Ogg";
my $DirExists = -d "$CreateDir";
($return_value = mkdir("$CreateDir", 0744) || die "$CreateDir: $!\n") unless $DirExists;

`cp --update --recursive $ChunkCDromDir/SLcorpus/Ogg/* $CreateDir`;

# Add comment to README
open(README, ">>$ChunkCDromDir/README") || die "$ChunkCDromDir/README: $!\n";
print README "./Ogg:\tSource code and installation of the Ogg Vorbis audio compression software\n";
close(README);

#
# copy Label directories
my $CreateDir = "$ChunkCDromDir/Labels";
my $DirExists = -d "$CreateDir";
($return_value = mkdir("$CreateDir", 0744) || die "$CreateDir: $!\n") unless $DirExists;

`cp --update --recursive $ChunkCDromDir/SLcorpus/Labels/chunks/$Speakers $ChunkCDromDir/Labels/` 
	if -d "$ChunkCDromDir/SLcorpus/Labels/chunks/$Speakers";

# Add comment to README
open(README, ">>$ChunkCDromDir/README") || die "$ChunkCDromDir/README: $!\n";
print README "./Labels:\tLabel files for all audio (speech) files\n";
close(README);

#
# copy Transcription directories
my $CreateDir = "$ChunkCDromDir/Transcriptions";
my $DirExists = -d "$CreateDir";
($return_value = mkdir("$CreateDir", 0744) || die "$CreateDir: $!\n") unless $DirExists;

`cp --update --recursive $ChunkCDromDir/SLcorpus/Transcriptions/$Speakers $ChunkCDromDir/Transcriptions/` 
	if -d "$ChunkCDromDir/SLcorpus/Transcriptions/$Speakers";

# Add comment to README
open(README, ">>$ChunkCDromDir/README") || die "$ChunkCDromDir/README: $!\n";
print README "./Transcriptions:\t(Raw) Transcriptions for all audio (speech) files\n";
close(README);

#
# Create Speech directories
$CreateDir = "$ChunkCDromDir/oggchunks";
$DirExists = -d "$CreateDir";
($return_value = mkdir("$CreateDir", 0744) || die "$CreateDir: $!\n") unless $DirExists;

# Add comment to README
open(README, ">>$ChunkCDromDir/README") || die "$ChunkCDromDir/README: $!\n";
print README "./oggchunks:\tAll Ogg Vorbis audio (speech) files\n";
close(README);

# Copy Speech files
my $SpeechPath = "$CreateDir/";

# Give a glob on the files you would like to extract (as in: glob("/$Speakers/*$Files*.aifc")
my @CopiedList = ();
my $LastSpeaker = "";
unshift(@ARGV, '*') unless scalar(@ARGV);
while(@ARGV)
{
  my $Files = shift(@ARGV);
	my @FileList = glob("$SourceSpeechDir/$Speakers/*$Files*.ogg");
	foreach $FilePath (@FileList)
	{
			$FilePath =~ m@/([^/]+)/([^/]+)$@;
			my $CurrentSpeaker = $1;
			if($CurrentSpeaker ne $LastSpeaker)
			{
		    	push(@CopiedList, "#\n# $CurrentSpeaker");
		    	$LastSpeaker = $CurrentSpeaker;
		  };
		
			my $FileName = $2;
			die "$FilePath does not contain a filename\n" unless $FileName =~ /\w/;

			# Copy Labels		
			# Make a speaker specific directory
			unless(-d "$LabelPath$CurrentSpeaker")
			{
		  	  mkdir("$LabelPath$CurrentSpeaker", 0744) || die "$LabelPath$CurrentSpeaker: $!\n";
			};
			$FileName =~ m@^([^\_\.]+)@;
			my $LabelFile = $1;
			@LabelFileList = glob("$ChunkCDromDir/SLcorpus/Labels/chunks/$CurrentSpeaker/${LabelFile}*.*");
			
			my $SourceLabelFile = shift(@LabelFileList);
			$SourceLabelFile =~ m@([^/]+)$@;
			$LabelFile = $1;
			
			`cp --update $SourceLabelFile $LabelPath$CurrentSpeaker/$LabelFile` if -e "$SourceLabelFile";
		
			# Make a speaker specific directory
			unless(-d "$SpeechPath$CurrentSpeaker")
			{
		  	  mkdir("$SpeechPath$CurrentSpeaker", 0744) || die "$SpeechPath$CurrentSpeaker: $!\n";
			};
		
			next if -e "$SpeechPath$CurrentSpeaker/$FileName" && -s "$SpeechPath$CurrentSpeaker/$FileName" == -s "$FilePath";
			print STDERR "Current copy: $FilePath -> $SpeechPath$CurrentSpeaker/$FileName\n";
			`cp --update $FilePath $SpeechPath$CurrentSpeaker/$FileName`;
			die "$FilePath -> $SpeechPath$CurrentSpeaker/$FileName: $!\n" unless -e "$SpeechPath$CurrentSpeaker/$FileName";
			push(@CopiedList, "$CurrentSpeaker/$FileName");
	};
};
open(COPYFILE, ">>$ChunkCDromDir/FILELIST.txt") || die "$ChunkCDromDir/FILELIST.txt: $!\n";
print COPYFILE join("\n", @CopiedList);
close(COPYFILE);
	
#
#
# If the size of the data becomes too large to fit on a CDrom, compress the SLcorpus tree.
#
# Determine total size of CDrom data
my $totalSize = `du -cms $ChunkCDromDir | grep total`;
$totalSize =~ s/[^\d\-]*$//ig;

# Don't re-compress the corpus (shouldn't be possible, but is tested anyhow)
unless( -e "$ChunkCDromDir/SLcorpus.tgz")
{
	# Compress SLcorpus if it is too large (don't remove yet)
	`cd $ChunkCDromDir;tar -czf SLcorpus.tgz ./SLcorpus` if $totalSize > 620;
	
	# Remove SLcorpus dir-tree and add comment to README
	if(-e "$ChunkCDromDir/SLcorpus.tgz")
	{
		`cd $ChunkCDromDir;rm -rf ./SLcorpus`; # Be carefull, the tar can fail
		# Add comment
		open(README, ">>$ChunkCDromDir/README") || die "$ChunkCDromDir/README: $!\n";
		print README "./SLcorpus.tgz:\tA compressed tar archive of SLcorpus (unpack with: tar -xvzf ./SLcorpus.tgz)\n";
		close(README);
	};
};

