#! /usr/bin/perl

# select 'planref1-2_0099.djvu'
#set-txt

#(page 0 0 2484 3509 
#...
#(line 300 2778 936 2815 
#(word 300 2785 431 2812 "Format") 
#(word 521 2778 675 2814 "Operation") 
#(word 689 2785 767 2815 "Code") 
#(word 854 2785 936 2813 "BFP")) 

# (bookmarks
# ("Preface"
#  "#planref1-2_0004.djvu" )
# ("Part 1 - Basic Information"
#  "#planref1-2_0006.djvu" ) )

print "(bookmarks\n";

while (<>) {
	if (/select '([^']*)'/) {
		$page = $1;
		next;
	}

	if (defined $page &&

	/\(word(?:\s\d+){4}\s"Format"\)\s
		\(word(?:\s\d+){4}\s"Op[a-z]+on"\)\s
		\(word(?:\s\d+){4}\s"C[oa]de"\)\s
		\(word(?:\s\d+){4}\s"([A-Z]+)"\)/x
	) {
		print "(\"$1\" \"#$page\")\n";
		undef $page;
		next;
	}

	if (defined $page &&
	/\(line(?:\s\d+){4}\s
		\(word(?:\s\d+){4}\s"The"\)\s
		\(word(?:\s\d+){4}\s"(\#[A-Z]+)"\)\s
		\(word(?:\s\d+){4}\s"directive"\)\s/x
	) {
		print "(\"$1\" \"#$page\")\n";
		undef $page;
		next;
	}

#(page 0 0 2484 3509 (line 275 2807 384 2836 (word 275 2807 384 2836 "NAME")) (line 274 2736 400 2769 (word 274 2736 400 2769 "#XPLD")) 

	if (defined $page && 
	/\(page(?:\s\d+){4}\s
		\(line(?:\s\d+){4}\s
		\(word(?:\s\d+){4}\s"NAME"\)\)\s
		\(line(?:\s\d+){4}\s
		\(word(?:\s\d+){4}\s"(\#[A-Z]{4})"\)/x
	) {
		print "(\"$1\" \"#$page\")\n";
		undef $page;
		next;
	}

# (page 0 0 2484 3509 (line 280 3035 476 3063 (word 280 3035 476 3063 "FUNCTION")) (line 277 2949 2052 2996 (word 277 2967 337 2996 "The") (word 351 2965 665 2995 "DACOSYCREATE") (word 678 2964 779 2984 "macro")

	if (defined $page && 
	/
		\(line(?:\s\d+){4}\s
		\(word(?:\s\d+){4}\s"FUNCTION"\)\)\s
		\(line(?:\s\d+){4}\s
		\(word(?:\s\d+){4}\s"The"\)\s
		\(word(?:\s\d+){4}\s"([A-Z][A-Z4]+)"\)\s
		\(word(?:\s\d+){4}\s"macro"\)\s/x
		
	) {
		print "(\"$1\" \"#$page\")\n";
		undef $page;
		next;
	}

}

print ")\n";
