#! /usr/bin/perl
#
# Use:
# ConstructSLcdrom.pl [fm|hm] <Speaker> [fileglob] [fileglob] ...
#
# Copies all necessary files to the SLcdrom directory for distribution
#
#    Copyright (C) 2000, 2001  R.J.J.H. van Son
# 
#    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/sentences';
my $SourceCoGDir = '/u/rob/SLspeech/CenterOfGravity';
my $ShadowCDromDir = '/data1/rob/ExpCDrom';
my @PraatList = ();

my $Microphone = 'fm';  # Default is the fixed microphone
$Microphone = shift(@ARGV) if $ARGV[0] =~ /^[fh]m$/i;

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

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

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

# The praat scripts
`cp --update $ShadowCDromDir/SLcorpus/scripts/PlayOneSound.praat $ShadowCDromDir/`;
`cp --update $ShadowCDromDir/SLcorpus/scripts/RunPlayList.praat $ShadowCDromDir/`;

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

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

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

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

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

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

# Give a glob on the files you would like to extract (as in: glob("/$Speaker/*$Files*.aifc")
my @CopiedList = ();
my $LastSpeaker = "";
while(@ARGV)
{
  my $Files = shift(@ARGV);
	my @FileList = glob("$SourceSpeechDir/$Microphone/$Speakers/*$Files*.aifc");
	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("$ShadowCDromDir/SLcorpus/Labels/sentences/$CurrentSpeaker/phoneme/${LabelFile}_*.*");
			@LabelFileList = glob("$ShadowCDromDir/SLcorpus/Labels/sentences/$CurrentSpeaker/translit/${LabelFile}*.*")
											unless grep(/\w/, @LabelFileList);
			
			my $SourceLabelFile = shift(@LabelFileList);
			$SourceLabelFile =~ m@([^/]+)$@;
			$LabelFile = $1;
			
			`cp --update $SourceLabelFile $LabelPath$CurrentSpeaker/$LabelFile`;
		
			# 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, ">>$ShadowCDromDir/FILELIST.txt") || die "$ShadowCDromDir/FILELIST.txt: $!\n";
print COPYFILE join("\n", @CopiedList);
close(COPYFILE);
	
#

