#!/usr/bin/perl
# makescripts.pl
#
# Mark Hasegawa-Johnson, 6/25/2002, modified 10/2003
# Illinois Speech and Language Engineering (ISLE group)
#
# Usage (printed if makescripts.pl is called with no arguments):
#
$USAGE=<<ENDUSAGE;

makescripts.pl:
   Search a directory hierarchy for input files, and create scripts
 
 Usage:
   makescripts.pl EXT DIR1 DIR2 > output.scp
 Example:
   makescripts.pl wav TIMIT/TRAIN TIMIT/TEST > timit.scp
 
 This program searches the given directories for files with extension
 EXT, and prints to STDOUT a two-file script file suitable for HCopy.
 STDOUT contain two columns; the first column gives the full pathname
 of the discovered wav-file, the second column gives a proposed
 MFC-filename.  In the example above, a line of the output might
 contain

 d:/timit/TIMIT/TRAIN/DR1/FAEM0/SI1392.WAV data/FAEM0SI1392.mfc
   
ENDUSAGE

#
###############################################
# Subroutine to peruse a directory tree $_[0].
#  If a listing is a directory, recursively process subtree.
#  Else if listing matches $pattern, then call process_file 
#    to process it.
#  Outfiles is an array of filehandle references to be used
#    by process_file
#
# Syntax: peruse_tree( "dir1", $pattern, \@outfiles );
#
sub peruse_tree {
  # Top directory is whatever was given as $_[0]
  # If I can't open it, die with an error message
  opendir(TOPDIR, $_[0]) || die "Can't opendir $_[0]: $!";
  print STDERR "Reading from directory ",$_[0],"\n";
  my(@filelist) = readdir(TOPDIR);
  closedir(TOPDIR);

  # Read entries of TOPDIR
  foreach $filename (@filelist) {
    # If last character of the filename is ., ignore it
    if ( $filename =~ /\.$/ ) { }

    # If it's any other directory,call peruse_tree on it
    elsif ( -d "$_[0]/$filename" ) {
      peruse_tree("$_[0]/$filename", $_[1], $_[2]);
    }

    # Otherwise, call function process_file to process it
    elsif ( $filename =~ /$_[1]/i ) {
      process_file("$_[0]/$filename", $_[2]);
    }
  }
}

###################################################
# Subroutine to read, convert, and write to output files
# phoneme data corresponding to one waveform.
#
# Syntax: process_file($speechfile)
#
# $speechfile is the name of the raw input speech file
# $fh is the output filehandle of an open SCP file
#
sub process_file {
  # Get rid of any file extension
  my($speechfile) = $_[0];
  my($parmfile) = $speechfile;

  # Create parameter filename: change extension to mfc
  $parmfile =~ s/(\.\S*)/\.mfc/;
  
  # Special processing for TIMIT files, to avoid loss of speaker ID:
  #  if parm file contains [mf]\w\w\w\d, keep that part of the path
  if($parmfile =~ /\/([mf]\w\w\w\d)\//i) { $modifier = $1; }
  else { $modifier = ""; }

  # Get rid of everything in the path except possibly the modifier
  $parmfile =~ s|.*/|data/$modifier|i ;

  # Print out the SCP entry
  print $speechfile, " ", $parmfile, "\n";
}

###########################################
# Main Program

if (@ARGV==0) { print STDERR $USAGE; exit; }

# First, read in the extension that we will be searching for
$EXT = shift(@ARGV);

# Now, for each directory specified on the command line:
# Open and initialize the SCP file
# then call peruse_tree to do the rest.
#
foreach $arg (@ARGV) {
  # Find the root directory name
  $dir = $arg;
  $dir =~ s:.*/::;
  
  # Process the tree
  peruse_tree($arg, "\.$EXT");
}

