#!/usr/bin/perl -w

#	"jarg2ztxt [-s] jargNNN.gz" -Takes a copy of the flat-text version of The
#	Jargon File <http://tuxedo.org/~esr/jargon/> and splits it into pieces
#	somewhat like the html version and makes those into ztxt files with
#	appropriate bookmarks, suitable for reading with gutenpalm
#	<http://gutenpalm.sourceforge.net/>.  The '-s' option makes the lexicon
#	itself be made into 27 separate files ([0A-Z]) with bookmarks for each
#	entry.  The default is to make the lexicon one (huge) file with bookmarks
#	for only each letter (and 0).  I use the -s option, but don't let me
#	tell you what to do  :) .  Needs makeztxt, also available from the gutenpalm
#	site.  
#
#	Copyright (C) 2001 Mick McMillan		mick@lowdrag.org
#
#	This program is free software; you can redistribute it and/or modify it
#	under the terms of the GNU General Public License as published by the Free
#	Software Foundation; either version 2 of the License, or (at your option)
#	any later version.
#	
#	This program is distributed in the hope that it will be useful, but
#	WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
#	or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#	for more details.
#	
#	You should have received a copy of the GNU General Public License along
#	with this program; if not, write to the Free Software Foundation, Inc., 59
#	Temple Place, Suite 330, Boston, MA  02111-1307 USA



use strict;
use vars qw/$makeztxt
			$gzipfile
			$jargon
			$line
			$tempdir
			$separate_letters
			$introduction
			$terms
			$revisions
			$construction
			$hacker_write
			$email_quotes
			$hacker_speech
			$i18n_style
			$lamer_speak
			$pronunciation
			$conventions
			$new_entries
			$lexicon
			$letter_prefix
			$folklore
			$j_random_hacker
			$help_grow
			$bibliography
			$no_zip
	 		/;
use Getopt::Long;

#	Things you might want to change below
	#	If makeztxt is somewhere not in your $PATH you can set it's full path
	#	here.
$makeztxt	=	"makeztxt";
	#	Okay, this program is going to want to make a temporary directory in
	#	/tmp .  If you have some problem with that here's the place to change
	#	it. And yes, I do know there is a Right Way to do this.
$tempdir	=	"/tmp/jarg2ztxt" . "$$" . "/";
	#	The file names correspond to the files in the html version and are
	#	made here with leading digits to preserve the ordering of the html
	#	index. Change 'em to whatever you like.
$introduction		= 	"00introduction";
$terms				=	"01terms";
$revisions			=	"02revisions";
$construction		=	"03construction";
$hacker_write		=	"04hacker_write";
$email_quotes		=	"05email_quotes";
$hacker_speech		=	"06hacker_speech";
$i18n_style			=	"07i18n_style";
$lamer_speak		=	"08lamer_speak";
$pronunciation		=	"09pronunciation";
$conventions		=	"10conventions";
$new_entries		=	"11new_entries";
$lexicon			=	"12lexicon";			#	not made if -s option
$letter_prefix		=	"12lexicon_";			#	not made if !-s
$folklore			=	"13folklore";
$j_random_hacker	=	"14j_random_hacker";
$help_grow			=	"15help_grow";
$bibliography		=	"16bibliography";



&get_options;
if (@ARGV == 0) { die &usage; }
if ($ARGV[0] =~ /jarg\d*\.gz/) {
	$gzipfile = $ARGV[0]; 
	$jargon = $gzipfile;
	$jargon =~ s/\.gz//;
	system "gzip -d  $gzipfile" || system "mv $gzipfile $jargon";
		#	This is just to deal with those over-helpful browsers that insist
		#	on gunzipping things without giving any indication.
} elsif ($ARGV[0] =~ /jarg\d*$/) {
	$jargon = $ARGV[0];
	$no_zip = 1;	#	Leave things as we found them
} else { &usage }		

system "mkdir $tempdir";
END { unlink($tempdir) }	#	this can't happen  :)
&split;
&makeztxt;


$separate_letters = "";
sub get_options {
	my $split_lexicon = '';
	GetOptions (split => \$split_lexicon);
	if ($split_lexicon) {
		$separate_letters = "1"
	}
}

sub split {
	open JARGON, "< $jargon" or die "Can't read $jargon $!\n";
	open OUTFILE, "> $tempdir" . "$introduction"  or die "Can't open $introduction for writing $!\n";
	while ($line = <JARGON>)	{
		if ($line =~ /^\Q:Of Slang, Jargon, and Techspeak:\E\s*$/) {
			close OUTFILE;
			open OUTFILE, "> $tempdir" . "$terms"  or die "Can't open $terms for writing $!\n";
		}
		if ($line =~ /^\Q:Revision History:\E\s*$/) {
			close OUTFILE;
			open OUTFILE, "> $tempdir" . "$revisions"  or die "Can't open $revisions for writing $!\n";
		}		
		if ($line =~ /^\Q:How Jargon Works:\E\s*$/) {
			close OUTFILE;
			open OUTFILE, "> $tempdir" . "$construction"  or die "Can't open $construction for writing $!\n";
		}		
		if ($line =~ /^\Q:Hacker Writing Style:\E\s*$/) {
			close OUTFILE;
			open OUTFILE, "> $tempdir" . "$hacker_write"  or die "Can't open $hacker_write for writing $!\n";
		}
		if ($line =~ /^\Q:Email Quotes and Inclusion Conventions:\E\s*$/) {
			close OUTFILE;
			open OUTFILE, "> $tempdir" . "$email_quotes"  or die "Can't open $email_quotes for writing $!\n";
		}
		if ($line =~ /^\Q:Hacker Speech Style:\E\s*$/) {
			close OUTFILE;
			open OUTFILE, "> $tempdir" . "$hacker_speech"  or die "Can't open $hacker_speech for writing $!\n";
		}
		if ($line =~ /^\Q:International Style:\E\s*$/) {
			close OUTFILE;
			open OUTFILE, "> $tempdir" . "$i18n_style"  or die "Can't open $i18n_style for writing $!\n";
		}
		if ($line =~ /^\Q:Crackers, Phreaks, and Lamers:\E\s*$/) {
			close OUTFILE;
			open OUTFILE, "> $tempdir" . "$lamer_speak"  or die "Can't open $lamer_speak for writing $!\n";
		}
		if ($line =~ /^\Q:How to Use the Lexicon:\E\s*$/) {
			close OUTFILE;
			open OUTFILE, "> $tempdir" . "$pronunciation"  or die "Can't open $pronunciation for writing $!\n";
		}
		if ($line =~ /^\Q:Other Lexicon Conventions:\E\s*$/) {
			close OUTFILE;
			open OUTFILE, "> $tempdir" . "$conventions"  or die "Can't open $conventions for writing $!\n";
		}
		if ($line =~ /^\Q:Format For New Entries:\E\s*$/) {
			close OUTFILE;
			open OUTFILE, "> $tempdir" . "$new_entries"  or die "Can't open $new_entries for writing $!\n";
		}
		if ($line =~ /^\QThe Jargon Lexicon\E\s+\**\s*$/) {
			close OUTFILE;
			open OUTFILE, "> $tempdir" . "$lexicon"  or die "Can't open $lexicon for writing $!\n";
		}
		if ($line =~ /^\Q:Hacker Folklore:\E\s*$/) {
			close OUTFILE;
			open OUTFILE, "> $tempdir" . "$folklore"  or die "Can't open $folklore for writing $!\n";
		}
		if ($line =~ /^\Q:A Portrait of J. Random Hacker:\E\s*$/) {
			close OUTFILE;
			open OUTFILE, "> $tempdir" . "$j_random_hacker"  or die "Can't open $j_random_hacker for writing $!\n";
		}
		if ($line =~ /^\Q:Helping Hacker Culture Grow:\E\s*$/) {
			close OUTFILE;
			open OUTFILE, "> $tempdir" . "$help_grow"  or die "Can't open $help_grow for writing $!\n";
		}
		if ($line =~ /^\Q:Bibliography:\E\s*$/) {
			close OUTFILE;
			open OUTFILE, "> $tempdir" . "$bibliography"  or die "Can't open $bibliography for writing $!\n";
		}
		print OUTFILE $line;
	}
	close OUTFILE;
	close JARGON;
	system "gzip $jargon" unless $no_zip;
}	

sub makeztxt {
		
	system "$makeztxt -l -t 'Introduction' $tempdir$introduction";
	system "$makeztxt -l -t 'Of Slang, Jargon, and Techspeak' $tempdir$terms";
	system "$makeztxt -l -t 'Revision History' $tempdir$revisions";
	system "$makeztxt -l -t 'Construction' -r \"^:.+:\\s*\$\" $tempdir$construction";
	system "$makeztxt -l -t 'Hacker Writing Style' $tempdir$hacker_write";
	system "$makeztxt -l -t 'Email Quotes and Inclusion Conventions' $tempdir$email_quotes";
	system "$makeztxt -l -t 'Hacker Speech Style' $tempdir$hacker_speech";
	system "$makeztxt -l -t 'Internationalization Style' $tempdir$i18n_style";
	system "$makeztxt -l -t 'Crackers, Phreaks, and Lamers' $tempdir$lamer_speak";
	system "$makeztxt -l -t 'Pronunciation' $tempdir$pronunciation";
	system "$makeztxt -l -t 'New Entries' $tempdir$new_entries";
	system "$makeztxt -l -t 'Hacker Folklore' -r \"^:.+:\\s*\$\" $tempdir$folklore";
	system "$makeztxt -l -t 'A Portait of J. Random Hacker' -r \"^:.+:\\s*\$\" $tempdir$j_random_hacker";
	system "$makeztxt -l -t 'Helping Hacker Culture Grow'$tempdir$help_grow";
	system "$makeztxt -l -t 'Bibliography' -r \"^:.+:\\s*\$\" $tempdir$bibliography";
	if ($separate_letters) {
		my $letter = "empty";	#	"" doesn't cut it here due to impending filename "0".  :)
		my @letters = qw/0 Q W E R T Y U I O P A S D F G H J K L Z X C V B N M/;
		open LEXICON, "$tempdir$lexicon" or die "Internal screw up, can't open $tempdir$lexicon\nIs $tempdir readable?\n $!";
		while ($line = <LEXICON>) {
			if ($line =~ /= (.) =/) {
				$letter = $1;
				open LETTER, "> $tempdir" . "$letter_prefix" . "$letter" or die "Can't open $tempdir$letter for writing. $!\n";
			}
			if ($letter ne "empty") { 
				print LETTER $line;
			}
		}
		for (@letters) {
			system "$makeztxt -l -t 'Lexicon $_' -r \":.+:\" $tempdir$letter_prefix$_";
		}
		close LEXICON;
	} 
	else {
		system "$makeztxt -l -t 'The Lexicon Proper' -r \"= . =\" $tempdir$lexicon";
	}
	system "rm $tempdir\*";
	system "rmdir $tempdir";
}	

sub usage {
	print <<EOF;
Usage: $0 [-s|--split] jargNNN.gz 
	where NNN is the version number of the jargon file.
	Get the flat text version from
	http://tuxedo.org/~esr/jargon/download.html
EOF
}
