#!/usr/bin/perl -w

# man2ztxt [--length=NN] [-v [-i [-c]] -h [-p [-m -f -q]] [section] <man_pages>
#
#	Options may be of the following: 
#	--length=NN | -l=NN Where NN is the width for text formatting. Defaults to
#	42 (of course).
#	--verbose | -v Shows bookmarks being generated. gives a good feeling that
#	something is happening.
#	--perl | -p  Uses `perldoc' instead of `man' and takes the following extra
#	options:
#		--question | -q searches the perldoc faq's, same as perldoc -q
#		--function | -f searches perldoc for function name, same as perldoc -f
#		--module | -m Dispays the entire module, like perldoc -m
#	--install | -i Installs to the device, allows for the following option:
#		--clean | -c rm's the .pdb files after installing.
#	--help | -h Display synopsis.
#	Section may be 1-9, otherwise the first manual page picked up in \$MANPATH
#	will be used. Just like `man'.
#		
#	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 diagnostics;
use Getopt::Long;
use vars qw/$verbose
			$length
			$args
			$manpage
			$section
			$scratchfile
			$asciifile
			$makeztxt
			$tmpdir
			$prefix
			$verbose
			$list
			$man
			$fmt
			$man_path
			$perldoc_path
			$pilot_xfer_path
			$perl
			$module
			$question
			$function
			$man_name
			$install
			$outputfile
			@installable
			$clean
			$help
			/;

#--------------------------------------------------------------
#	Things you might want to change
#--------------------------------------------------------------
$length 		= '42';						#	The Answer
$makeztxt 		= 'makeztxt';				#	If the following proggies are not in 
$man_path 		= 'man';					#	$your PATH, you can fix it here.
$perldoc_path 	= 'perldoc';		
$pilot_xfer_path= 'pilot-xfer';
$fmt			= 'fmt';
$prefix 		= '00';						#	I prefix my ztxt's with numbers to work
											#	around PalmOS fs's lack of directories
											#	(optional, make it '' if you like).
$tmpdir 		= "/tmp/man2ztxt" . $$ . "/";
#--------------------------------------------------------------
$ENV{MANWIDTH} = $length;
$ENV{PERLDOC_PAGER} = $length;
system 'echo $MANWIDTH';
system 'echo $PERLDOC_PAGER';
mkdir $tmpdir || die "couldn't make $tmpdir: $!\n";
END	{ rmdir $tmpdir or die "Couldn't remove" . $tmpdir, "$!\n"; }

GetOptions ('length=s'	=> 	\$length, 
			'verbose'	=> 	\$verbose, 
			'install'	=>	\$install,
			'clean'		=>	\$clean,		#	requires --install
			'perl'		=>	\$perl,
			'module'	=>	\$module,		#	<----- these three need --perl
			'question'	=>	\$question,		#	<---/
			'function'	=>	\$function,		#	<--/
			'help'		=>	\$help,
			);
			
if ($help)	{ usage() }
unless (@ARGV)	{ usage() }
$section = '';

if ($verbose)	{ 
	$list = '-l';
}
else	{
	$list = '';
}

if ($perl)	{
	$man = "$perldoc_path -t";
	$man_name = 'perldoc';
	if ($module)	{
		$man = "$perldoc_path -t -m";
	}
	elsif ($question)	{
		$man = "$perldoc_path -t -q";
	}
	elsif ($function)	{
		$man = "$perldoc_path -t -f";
	}
}
else	{
	$man = $man_path;
	$man_name = 'man';
}

@installable = ();
foreach (@ARGV)	{
	s/\ /\\\ /g;
	if ($_ =~ /[1-7]/)	{
		$section = $_;
		next;
	}
	else	{
		$manpage = $_;
	}
	print "processing $manpage $section\n";
	$scratchfile = $tmpdir . $manpage;
	$scratchfile =~ s/\ /\_/g;
	if ($perl)	{
		system "$man $section \"$manpage\" | col -b | $fmt -40 > $scratchfile";
	}
	else	{
		system "$man $section \"$manpage\" | col -b  > $scratchfile";
	}
		
	open MANPAGE, "< $scratchfile" or die "Something went very wrong $!\n";
	while (<MANPAGE>)	{
		if ($_ =~ /\((\d)\)/)	{
			$section = $1;
			last;
		}
		s/^\t/ /;
		s/^\s{4}/ /;
		s/^\s{4}/ /;				#	still tweaking this bit  <g>
	}
	close MANPAGE;
	$asciifile = $scratchfile . $section;
	$outputfile = "$manpage\\\($section\\\)\\\.pdb";
	rename($scratchfile, $asciifile) or system ("mv", $scratchfile, $asciifile);
	
	system "$makeztxt -a2 -t $prefix$man_name:$manpage\\\($section\\\) \\
		-o $outputfile $list -r ^[A-Z]+\$ $asciifile"; 
	$section = '';
	unlink $asciifile;
	unlink $scratchfile;
	
	push @installable, $outputfile;
}

if ($install)	{
	system "$pilot_xfer_path -i @installable";
	if ($clean)		{
		foreach (@installable)	{
			system "rm $_";
		}
	}
}
	
sub usage	{
	print <<"EOF"
useage: $0 [options] [section] <man_pages>.
Options may be of the following:
 --length=NN | -l=NN Where NN is the width for text formatting. Defaults 
  to 42 (of course).
 --verbose | -v Shows bookmarks being generated. gives a good feeling that 
 something is happening.
 --perl | -p  Uses `perldoc' instead of `man' and takes the following extra 
 options:
   --question | -q searches the perldoc faq's, same as perldoc -q
   --function | -f searches perldoc for function name, same as perldoc -f
   --module | -m Dispays the entire module, like perldoc -m
 --install | -i Installs to the device, allows for the following option:
   --clean | -c rm's the .pdb files after installing.
 --help | -h Display this help.
Section may be 1-9, otherwise the first manual page picked up in \$MANPATH will
 be used. Just like `man'.
EOF
;
}
