#! /usr/bin/perl

use strict;
use warnings;
use File::Path;

my $orgdir = $ARGV[0];;
my $newdir = $orgdir . "-txt";
my $pipeline_test = "0";
my $message = "";
my $mytitle = "";

mkdir($newdir) or die "Failed to create new directory $newdir $!";

# This should be converted to OS independent code.
`cp $orgdir/*.smil $newdir`;
`cp $orgdir/*.opf $newdir`;
`cp $orgdir/*.ncx $newdir`;
`cp $orgdir/*.xml $newdir`;
`cp $orgdir/*.css $newdir`;
`cp $orgdir/*.res $newdir`;
`cp -r $orgdir/images $newdir`;


#get the book title.



opendir(DIR, $newdir) or die "Can't open $newdir $!";
while (defined(my $file = readdir(DIR))) {
	next if $file =~ /^\.\.?$/;

if ($file =~ /\.opf/i) {
	my $old = "$newdir/$file";
	open (OLD, "< $old");
	
	while (<OLD>) {
		m/<dc:Title>(.+)<\/dc:Title>/i;
		$mytitle = $1;
	}
	
}
	
	
	# Fix the OPF file.
	if ($file =~ /\.opf/i) {
		
		my $old = "$newdir/$file";
		my $new = "$newdir/tmpfile";
		open (OLD, "< $old");
		open (NEW,  "> $new");
		select(NEW);
		while (<OLD>) {
			$_ =~ s/<meta.+media-type="audio\/mpeg".+//gi;
			$_ =~ s/<meta.+ name="dtb:totalTime".+//gi;
			$_ =~ s/<item.+media-type="audio.+//gi;
			$_ =~ s/<meta content="audioFullText" name="dtb:multimediaType" \/>/<meta content="textOnly" name="dtb:multimediaType" \/>/gi;
			$_ =~ s/<meta content="audio,text" name="dtb:multimediaContent" \/>/<meta content="text" name="dtb:multimediaContent" \/>/gi;
			$_ =~ s/<item.+href="tpbnarrator.res".+//gi;
			$_ =~ s/audioFullText/textOnly/gi;
			$_ =~ s/<meta content="audio/<meta content="/gi;
			
			print NEW $_;
			}
			
	close (OLD);
	close (NEW);
	unlink($old) or die "Could not unlink $old $!";
	rename($new, $old)  or die "Could not rename $new to $old $!";
	$pipeline_test = "1";
		
	}
	
	
	
		
	
	# Fix the SMIL files.
	if ($file =~ /\.smil/i) {
		my $old = "$newdir/$file";
		my $new = "$newdir/tmpfile";
		open (OLD, "< $old")   or die "Could not open $old $!";
		open (NEW,  "> $new") or die "Could not open $new $!";;
		select(NEW);
		while (<OLD>) {
			$_ =~ s/<audio.+//gi;
			$_ =~ s/<seq.+(id=".+")/<seq $1>/gi;
			$_ =~ s/<meta.+name="dtb:totalElapsedTime".+//gi;
			$_ =~ s/<text/<text region="text"/gi;
			print NEW $_;
		
		}
	
	close (OLD);
	close (NEW);
	unlink($old)  or die "Could not unlink $old $!";
	rename($new, $old)   or die "Could not rename $new to $old $!";
		
		
	}
			
			
			
			
			
			
			
			
			
			
	

			
			if ($file =~ /tpbnarrator\.res/i) {
			$pipeline_test = "1";
			} else {
			$pipeline_test = "0";
			$message = "Not at DAISY Pipeline produced book. Can not convert.\n";
			}


}
closedir(DIR);
			
			
opendir(DIR, $newdir) or die "Can't open $newdir $!";
while (defined(my $file = readdir(DIR))) {
	next if $file =~ /^\.\.?$/;
			
			# Fix the NCX file.
			if ($file =~ /\.ncx/i) {
			my $old = "$newdir/$file";
			my $new = "$newdir/tmpfile";
			open (OLD, "< $old")   or die "Could not open $old $!";
			open (NEW,  "> $new") or die "Could not open $new $!";;
			select(NEW);
			while (<OLD>) {
			$_ =~ s/<audio .+//gi;
			$_ =~ s/<head>/<head>\n<title>$mytitle<\/title>\n/gi;
			print NEW $_;
			
			}
			
			close (OLD);
			close (NEW);
			unlink($old)  or die "Could not unlink $old $!";
			rename($new, $old)   or die "Could not rename $new to $old $!";
			
			
			}			
			
			
			
			}
			
			
closedir(DIR);

# If we don't have a OPF file so stop and remove the files we made and end the program.
	if ($pipeline_test eq 0) {
			if ($message eq "") {
		print STDOUT "No OPF file found. Convertion can not proceed.\n";
			} else {
			print STDOUT $message;
			}
		rmtree([$newdir], 0, 1)  or die "Could not remove $newdir $!";
		exit;
	} else {
		print STDOUT "Conversion completed.\n$newdir";
		exit;
	}