#!/usr/pkg/bin/perl
#
# Allow using L<-e> when the actual item is
# =item -e || --expirenodes device
#
# Also change to the behind-the-scenes syntax.
#
# This syntax change is needed because Pod::Html can't resolve links
# to items with special characters.  It's a fairly subtle bug deep
# inside Pod::HTML, described in perlbug #38295 and fixed in
# change 25083
# <http://public.activestate.com/cgi-bin/perlbrowse?patch=25083>.
#
# Once enough people are running the fixed perl, the regexp could
# be changed to substitute in $item, not $map.

# $Id: pod_arg_link_fix,v 1.2 2006/11/28 19:14:04 fenner Exp $

use Pod::Html;

$pod = $ARGV[0];
open(POD, $pod) || die "$pod: $!\n";
while (<POD>) {
	if (/^=item\s+(.*)/) {
		$item = $1;
		$fid = Pod::Html::fragment_id($item);
		$name = "item_" . $fid;
		$anchor = Pod::Html::anchorify($name);
		if ($seen{$anchor}++) {
			warn "duplicate anchor $anchor for item $item\n";
		}
		if ($item =~ /^-[a-zA-Z]/) {
			$flag = $&;
			$map{$flag} = $anchor;
			$fid{$flag} = $fid;
			$item{$flag} = $item;
		}
	}
}
close(POD);
rename($pod, $pod . ".orig") || die "rename: $!\n";
open(ORIG, $pod . ".orig") || die "${pod}.orig: $!\n";
open(NEW, ">$pod") || die "rewriting $pod: $!\n";
while (<ORIG>) {
	foreach $flag (keys %map) {
		s,L<$flag(|[^>]*)?>,L<$flag|/$map{$flag}>,g;
	}
	print NEW;
}
close(ORIG);
close(NEW);
