#!/usr/bin/perl

my @files=`ls -1 *.abw`;
my @ident=`cat books`;

foreach (@files){ 

        my @lines;
	chop;
	open KW, ">>$_.keywords";
	open USFM, ">>$_.usfm";
	open VS, ">>$_.verse";
	chomp(@lines=`cat $_`);
	
 	@lines[1]="\\id @ident[$_-1] ";

	foreach (@lines) {
                s/<m\ .*?\/m>//;
                
		s/<p\ style=\"Parallelen [Ü]*bers Kapitel\".*?><c.*?>\ *[K|P]\ (.*?)<\/c><\/p>/\n\n\\c\ $1\ /g;

                s/<p\ style=\"Parallelenverweise\" xid=\"[0-9]+\"\ props=\"text-align:left; line-height:1.0; dom-dir:ltr; orphans:0; widows:0\"><\/p>//g;
 
                s/<p\ style=\"Parallelenverweise\" xid=\"[1-7]\".+?><c.+?>.*?<\/c><\/p>//g;
                s/<p style=\"Parallelenverweise\".*?>(.*?)<\/p>/\n$1\ \\x\*/g;
		s/<c style=\"Verszahl\ Parallelensignal\".*?>(.*?)<\/c>/\\v\ $1\ \ /g;
		s/<c style=\"Parallelentext Leerz\".*?>\ <\/c>/\ \\x\ /g;
		s/<c style=\"Parallelentext kursiv\".*?>(.*?)<\/c>/\ \\xk\ $1\ /g;
		s/<c .*?props=\"lang:de-DE;\ font-size:8pt;\ font-family:(Utopia|Times\ New\ Roman)\".*?>(.*?)<\/c>/\ \\xt\ $2/g;
		s/^.*?style=\"Normal.*?$//g;
		s/^.*?style=\"En-tête.*?$//g;
		s/<br\ *\/>//g;
		s/xid\=\".*?\"//g;
		s/^\ x\*//;
                s/<.*?>//g;
                s/(\\xt.*?)(\\xk)/$1\ \\x\*\ \\x\ $2/g;
                

}
        $book = join ("", @lines);	
        
        @lines = split("\n",$book);
        
        my $chapter, $verse =1;

        foreach (@lines) {
                
                if (/^\\c\ ([0-9]+)\ /) {
                        
                        $chapter=$1;
                        }
                         
                if (/^\\v\ ([0-9]+)\ /) {
                        
                        $verse=$1;
                        }
                        
                s/^\\v\ ([0-9]+)\ /$chapter.":".$verse."|"/e;
                s/\\x\*\ \\x\ /\\x\*\n$chapter:$verse\|\ \\x\ /g;
                s/\ (\\x\ +\\xk)\ +(.*?)(\\xt)/":".substr($2,0,4)."\| ".$1." ".$2.$3/eg;
                s/$/\n/;
                }

        @lines=split("\n",join("",@lines));
        
        foreach (@lines) {
                s/\\c\ .*?$//;
                s/^\s*$//;
                s/\ :/:/;
                s/\s+\|/\|/;
                s/\|:/:/; 
                s/\ +/\ /;
                s/$/\n/;
                }


        foreach (@lines) {
        
                if (/^[0-9]+:[0-9]+\|/) {
                        print (VS $_);
                        }
                elsif (/^[0-9]+:[0-9]+:/) {
                        print (KW $_);
                        }
                else {print (USFM $_);}
                }                        

#	print (USFM @lines);
	close USFM,VS,KW;
}                                                                                  
