#! /usr/bin/perl
#
# Read the id of a speaker and tranfer all speech files
# from the sound recording directory to the SLspeech directory.
#
###############################################################################
#
# 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.
#
#
#######################################################
#

$RecordingSoundsDir = "/data1/rob/SLrecordings/";
$SLspeech    = "/u/rob/SLspeech/chunks/";

# Make sure the source directories exist
sub check_Recordingdirectories  # ($ID) -> 1/0 recording texts directories exists or not
{
	my $ID = shift || return 0;
	#
	my $DirsExists = -d "$RecordingSoundsDir${ID}";
	return $DirsExists;
}

# Make sure the target directory exists
sub create_SLspeechdirectory  # ($ID) -> 1/0 create directory if necessary
{
	my $ID = shift || return 0;
	my $SubDirectory = shift || "";
	#
	my $return_value = 0;
	# Root directory for this speaker
	my $DirExists = -d "$SLspeech$ID";
	($return_value = mkdir("$SLspeech$ID", 0744) || die "$SLspeech$ID: $!\n") unless $DirExists;

	return $return_value;   # Something done?
}

# Check  and prepare source and target directories
sub prepare_directories    # ($ID) -> 0/1 Check and create directories, 0: Error
{
	my $ID = shift || return 0;
	#
	# Make sure, the source directory exists
	return 0 unless check_Recordingdirectories($ID);
	#
	create_SLspeechdirectory($ID);

	return 1;
}

# Copy all files
sub copy_speech_files	# ($ID) -> 0/1   # Copy all sound files to corresponding directories
{
	my $ID = shift || return 0;
	#
	my $Path = "$SLspeech$ID/";
	my @FileList = glob("$RecordingSoundsDir$ID/*");
	foreach $FilePath (@FileList)
	{
		$FilePath =~ m@/([^/]+)$@;
		my $FileName = $1;
		die "$FilePath does not contain a filename\n" unless $FileName =~ /\w/;
		next if -e "$Path$FileName" && -s "$Path$FileName" == -s "$FilePath";
		print STDERR "Current copy: $FilePath -> $Path$FileName\n";
		`cp $FilePath $Path$FileName`;
		die "$FilePath -> $Path$FileName: $!\n" unless -e "$Path$FileName";
	};
	
	return 1;
};

sub run_copy_of_speech_files	#	($ID) -> 0/1
{
	my $ID = shift || return 0;
	prepare_directories($ID);
	copy_speech_files($ID);
	return 1;
};

# Run the conversion
my $ID = shift(@ARGV) || return 0;

run_copy_of_speech_files($ID);

