#!/usr/bin/perl -w
# Emacs should use -*- cperl -*- mode
#
# Copyright (c) 2003 Simon L. Nielsen <simon@FreeBSD.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# Version 0.0 pre revision control system

# Parse the list of supported hardware out of section 4 manual pages
# and output it on stdout as SGML/DocBook entities.

# WARNING: This is only a proof-of-concept script!

# The script will look for the following line in the manual page:
# .\"HWLIST START
# and make an entity of the content until the line containing:
# .\"HWLIST STOP

# Usage:
# ./mdco2sgml manpage [manpage ...]

use strict;

# Conf
my $curlevel = 0;

# Globals
my %mdocvars;
my $isinhwlist;
my $isin_para;

foreach my $page (@ARGV) {
  parse($page);
}

sub parse {
  my ($manpage) = @_;

  my $cur_section;
  my $cur_manname;
  my $cur_mansection;
  my $isin_it = 0;
  my $isinpara = 0;

  $isinhwlist = 0;
  $isin_para = 0;
  undef(%mdocvars);

  open(MANPAGE, "$manpage") || die();
  while(<MANPAGE>) {
    chomp;
    my $line = $_;
    my $iscomment = 0;

    #print "Got '$line'\n";

    # Find commands
    if(s/^\.(.*)$/$1/) {
      # Detect comment lines
      if(s/^\\"(.*)$/$1/) {
	$iscomment = 1;
      }

      if($iscomment && /^HWLIST START/) {
	dlog(2, "Found a HWLIST START key!");
	$isinhwlist = 1;
	sgmlout_tag("<!ENTITY hwlist.$cur_manname.$cur_mansection '");
      }

      if($iscomment && /^HWLIST STOP/) {
	dlog(2, "Found a HWLIST STOP key!");
	sgmlout_tag("'>");
	$isinhwlist = 0;
      }

      if($iscomment) {
	# All other comment lines are... just comments
	next;
      }

      if(/^Nm (\w+)/) {
	dlog(3, "Setting Nm to $1");
	$mdocvars{Nm} = $1;

      } elsif(/^Nm$/) {
	sgmlout_text("&man.".$mdocvars{Nm}.".$cur_mansection;");

      } elsif(/^Sh (.+)$/) {
	dlog(4, "Setting section to $1");
	$cur_section = $1;

      } elsif(/^Dt ([^ ]+) ([^ ]+)/) {
	dlog(4, "Setting mansection to $2");
	$cur_manname = lc($1);
	$cur_mansection = $2;

      } elsif(/^It(.*)$/) {
	if($isin_it) {
	  sgmlout_tag("</listitem>");
	}
	sgmlout_tag("<listitem>");
	if($1 ne "") {
	  sgmlout_text($1);
	}
	$isin_it = 1;

      } elsif(/^Bl/) {
	sgmlout_tag("<itemizedlist>");

      } elsif(/^El/) {
	if($isin_it) {
	  sgmlout_tag("</listitem></itemizedlist>");
	  $isin_it = 0;
	}

      }
    } else {
      # This is then regular text
      if($isinhwlist) {
	sgmlout_text("$_");
      }
    }
  }
  close(MANPAGE) || die();
}

sub dlog {
  my ($level, $txt) = @_;

  if($level < $curlevel) {
    print STDERR "$level: $txt\n";
  }
}

sub sgmlout_text {
  my ($txt) = (@_);

  my $out;

  if($isin_para) {
    $out = $txt;
  } else {
    $out = "<para>$txt";
    $isin_para = 1;
  }

  # We only care about the HW list for now.
  if($isinhwlist) {
    print "$out\n";
  }
}

sub sgmlout_tag {
  my ($txt) = (@_);

  my $out;

  if($isin_para) {
    $out = "</para>\n$txt";
    $isin_para = 0;
  } else {
    $out = $txt;
  }

  # We only care about the HW list for now.
  if($isinhwlist) {
    print "$out\n";
  }
}
