#!/usr/bin/perl
use strict;



if (!$ARGV[0] || $ARGV[0]eq "-h") {
    print   "\n\t-p <DIR> \t\t:: directory that is the root of the subdirectory structure, use the COMPLETE path\n",
	    "\t-o <DIRECTORY> \t\t:: output directory  [default=/tmp]\n",
	    "\t-h      \t\t:: display help information\n";
	exit;
}

my $prefix = "";
my $out_dir = "/tmp";
               
while (my $args = shift(@ARGV)) {
	if ($args eq "-o"){
	    $out_dir = shift(@ARGV);
	}
	elsif ($args eq "-p"){
	    $prefix =  shift(@ARGV);
	}
	else{
	    die "invalid option: $args\n";
	}
    }
if ($prefix eq ""){
    die "program failure: missing mandatory option -p";
}
 
#
#first check if we need to create destination directory
#
if (!(-e "$out_dir")){
    system("mkdir $out_dir") 
	or print STDERR "could not create $out_dir\n";
}
my $rootdirlist = `ls $prefix`;
my @root_dirs = split("\n",$rootdirlist);
foreach my $root_dir (@root_dirs){
    print STDERR "processing dir>>$root_dir<<\n";
    my $dirlist = `ls $prefix/$root_dir | grep "_dir" `; 
    my @dirs = split("\n",$dirlist);
    foreach my $dir (@dirs){
	if (!(-e "$out_dir/$dir")){
	    system("mkdir $out_dir/$dir") 
		and  print STDERR "could not create $out_dir/$dir\n";
	}
	my $files = `ls $prefix/$root_dir/$dir`;
	my @files = split("\n",$files);
	foreach my $file (@files){
	    system("ln -s $prefix/$root_dir/$dir/$file $out_dir/$dir/$file\n")
		and print STDERR "could not create for $dir/$file\n"; 
	}
    }
    print STDERR "getting txt files >>ls $prefix/$root_dir/*.txt<<\n";
    my $txtlist = `ls $prefix/$root_dir/*.txt`; 
    
    my @txts = split("\n",$txtlist);
    foreach my $file (@txts){
	#
	#remove the path from the file name
	#
	$file =~ s/.*\///;
	print STDERR "\t FILE:$file\n";
	print STDERR "cat $prefix/$root_dir/$file >> $out_dir/$file\n";
	system("cat $prefix/$root_dir/$file >> $out_dir/$file\n")
	    and  print STDERR "could not process  $file\n"; 
    }
}
