my $sourcepdf = shift;

my $amllocation="K:\\klanten\\PIXBOOK\\AML\\";


$sourcepdf =~ s/([^\\]*.pdf)$//;
$sourcepdf = $1;


my $report="";



while ($sourcepdf =~ /\(([^\)]*)\)/g) {
	my $pixbookid=$1;
	opendir(AML, $amllocation);
	while ($scandir=readdir(AML)) {
		$scandir = $amllocation . $scandir . "\\";
		my $sourcedir= $scandir . $pixbookid . "\\";
		if (-d $sourcedir) {
			my $pixbookidctr=0;
			print "corrupt jpgs for " . $sourcedir . ":\n";
			opendir(DIR, $sourcedir);
			while ($filename=readdir(DIR)) {
				my $sourceFile = $sourcedir . $filename;
				if ((-f $sourceFile) && ($sourceFile =~ /\.jpg/)) {
					open (INFILE,"< $sourceFile");
					seek INFILE,0,2; $len=tell;  # get stream length
					seek INFILE,0,0; read INFILE,$data,$len;
					close INFILE;
					if (not($data =~ /\xff\xd9$/)) {
						print $sourceFile . "\n";
						$pixbookidctr++;
					}
				}
			}
			if ($pixbookidctr == 0) {
				print "no files found\n";
			}
		}
	}
}
closedir DIR;


open (OUTFILE, $amllocation . "report.txt");
print OUTFILE "$report";
close OUTFILE;