#!/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 "now reading $file\n";
$file2 = $file;
$file3 = $dir . "/" . $file;

open METFILE, $file3;

while (<METFILE>){
#print "now reading $file\n";
$line =$_;

if ($line =~/profile_type="vector"/){
$profile ="v";
#print "we have a vector victor\n";
}elsif ($line =~/profile_type="KML"/){
$profile ="k";
}elsif($line =~/profile_type="/){
$profile ="u";
$unknowProfileArray[$unknowProfileArrayCounter]= $line;
$unknowProfileArrayCounter++; 
};


}

#print "\$file2 = $file2\n";



open MIFFILE, "$dir/$file" or die ($!);

#while (<MIFFILE>){
#print "$_";
#};

if ($file ne '' and $file ne '.' and $file ne '..'){

print LISTFILE "$file2 $profile p\n";
$filecounter ++;
};

};#foreach $file (readdir DH){

if ($unknowProfileArrayCounter >0){
$unknownFile = $ListFileDir . "/" . $Infile . "_UnknownProfile.txt";
open UNKNOWN,  ">$unknownFile";

foreach $unknowProfileArray (@unknowProfileArray){
print UNKNOWN "$unknowProfileArray\n";
};
};
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";  
