#!/usr/bin/perl

use Getopt::Long;
use Cwd qw(cwd);



sub usage() {
    print "Usage: perl $0 [-listfile=<listfile>][-directory=<directory>]\n";
    print "Example:\n";
    print "perl $0 -listfile=list -directory=/dir/subdir \n";
    print"where listfile is the root name for the mif and fgdc files \n";
    print "and /dir/subdir is the directory containing the files to be listed\n";
}

&GetOptions(\%gOptions, qw(listfile=s directory=s  product=s));
unless ($gOptions{listfile} and $gOptions{directory} ) {
        usage();
        exit(1);
};

chomp($dir= $gOptions{directory});
$Infile = $gOptions{listfile};

$userdir = cwd;
$ListFileDir ="/mtdata/geo/gpms/input/list";
$miffile= $ListFileDir . "/" . $Infile . "_mif_listfile.txt";
open LISTFILE, ">$miffile";

print "now reading from the $dir directory\n";
opendir DH, $dir or die "cannot open $dir";
$loopcounter=0;

print "pre loop\n";
foreach $file (readdir DH){
print "in the loop\n";
print "now evaluating $file\n";
$dotLoc = index($file, ".")+1;
$ext=substr($file,$dotLoc);
print "\$ext = $ext\n";

if ($ext eq "mif"){
$mainpart=substr($file,0,$dotLoc-1);
print "\$mainpart = $mainpart\n";
$file2 =$dir . "/" .  $mainpart . ".xml";
$file3 = $mainpart . ".mif";
print "\$file3 = $file3\n";
print "\$file2 = $file2\n";
$cpFile =$dir . "/" . $file;
print "\$cpFile = $cpFile\n";
#system "cp $cpFile $file2";
print "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";
}else{
$file2 = $file;
};

if ($file3 ne ''){
print LISTFILE "$file3 p\n";
$filecounter ++;
};

};#foreach $file (readdir DH){


print "\nThe listfile $Infile has been created and $filecounter files have been added to it. It can be found in the $ListFileDir directory.\n\n";  
