#!/usr/bin/perl -w
# System Format = Win32
#
##################################################################
##  Technical Services Scripty Thing
##  ================================
##  Author:  Xxx Xxxxxx - Technical Support Officer
##  Creation Date:  Friday, 4th August 2000.
##
##  This script is designed to better manage MARC records that
##  need to be sent to customers and a summary file to NLA.
##  Archive attributes will play a large role in the script,
##  in the future, it is hoped that the ability to automatically
##  send files via FTP to NLA and the DA FTP server for customers.
##                                                                
###################################################################

# Variable initialise
$InputDIR1	= "./wuexport/";
$InputDIR2	= "./cuexport/";
$OutputDIR	= "./";
$LastOut	= 0;

# Find the last entrman mkdiry number for output dir
$LastOut = &FindLastOut;
$NextOut = $LastOut + 1;
$NextOut = &GetPadString($NextOut);

# Find the files that need to be outputted in each dir
&GrabFileList;

&CreateInput(1);
&CreateInput(2);

&CopyFiles;

# &WriteOutput;


sub CopyFiles {
    @CopyList = @FileList1;
    foreach $CopyList (@CopyList) {
       	next if ($CopyList =~ /^\./);
       	next if !($CopyList =~ (/[0-9]/));
       	next if !($CopyList =~ (/txt/));
	$x1 = $InputDIR1.$CopyList;
	$x2 = $InputDIR1.$NextOut."/".$CopyList;
	$Tmp = `cp $x1 $x2`;
	print $x1,"\-\>",$x2,"\n";
    }
    @CopyList = @FileList2;
    foreach $CopyList (@CopyList) {
       	next if ($CopyList =~ /^\./);
       	next if !($CopyList =~ (/[0-9]/));
       	next if !($CopyList =~ (/txt/));
	$x1 = $InputDIR1.$CopyList;
	$x2 = $InputDIR1.$NextOut."/".$CopyList;
	$Tmp = `cp $x1 $x2`;
	print $x1,"\-\>",$x2,"\n";
    }
}

sub CreateInput {
    my $What = shift;
    if ($What == 1) {
	print "Creating DIR: ",$InputDIR1.$NextOut,"\n";
	$I = mkdir ($InputDIR1.$NextOut,0777);
    } else {
	print "Creating DIR: ",$InputDIR2.$NextOut,"\n";
	$I = mkdir ($InputDIR2.$NextOut,0777);
    }
    if ($I) {
        print "Success! \n";
    } else {
        print "Fail! : $! \n";
    }
}

#sub WriteOutput {
#    open (OUTPUT,">$NextOut\.txt") or die ("ERROR opening output file: $!");
#    print OUTPUT "$InputDIR1\n\n";
#    print @FileList1[0];
#    foreach $FileList1 (@FileList1){
#	print "\"",$FileList1,"\"";
#       	next if ($FileList1 =~ /^\./);
#       	next if !($FileList1 =~ (/[0-9]/));
#	print OUTPUT "$FileList1\n";
#    }
#    print OUTPUT "$InputDIR2\n\n";
#    foreach $FileList2 (@FileList2){
#       	next if ($FileList2 =~ /^\./);
#       	next if !($FileList2 =~ (/[0-9]/));
#	print OUTPUT "$FileList2\n";
#    }
#}

sub FindLastOut {
   opendir (FINDLASTOUT_OUT,$OutputDIR);
   my @Files = readdir (FINDLASTOUT_OUT);
   closedir (FINDLASTOUT_OUT);
	my $Highest = $LastOut;
	foreach $File (@Files) {
		next if ($File =~ /^\./);
		next if !($File =~ (/[0-9]/));
		if (int(substr($File,0,5)) >= $Highest) {
			$Highest = int(substr($File,0,5));
		}
	}
   print "Last Entry = ",$Highest,"\n";
	return $Highest;
}

sub GrabFileList {
   opendir (FILELISTDIR1,$InputDIR1);
   @FileList1 = readdir (FILELISTDIR1);
   closedir (FILELISTDIR1);
   opendir (FILELISTDIR2,$InputDIR2);
   @FileList2 = readdir (FILELISTDIR2);
   closedir (FILELISTDIR2);
}

sub GetPadString {
	my $Integer = shift;
	if ($Integer < 10) {
		return "0000".$Integer;
	} elsif (($Integer >= 10) and ($Integer < 100)) {
		return "000".$Integer;
	} elsif (($Integer >= 100) and ($Integer < 1000)) {
		return "00".$Integer;
	} elsif (($Integer >= 1000) and ($Integer < 10000)) {
		return "0".$Integer;
	} elsif ($Integer >= 1000) {
		return $Integer;
	}
}



