#!/bin/sh
################################################################################
#
# Tcl script to convert tgdb's help file into a "man page" (tgdb.man).
# Simply type "mkman" on the shell's command line.
#
# (c) 1994 HighTec EDV-Systeme GmbH
#
################################################################################
set DummyVar {
  if test -d `pwd`/tcllib
  then
    TCL_LIBRARY=`pwd`/tcllib
    TK_LIBRARY=`pwd`/tcllib
    BLT_LIBRARY=`pwd`/tcllib
    export TCL_LIBRARY TK_LIBRARY BLT_LIBRARY
  fi
  if test -f `pwd`/tgdb_wish
  then
    exec ./tgdb_wish -f `pwd`/mkman
  else
    exec tgdb_wish -f `pwd`/mkman
  fi
}
unset DummyVar

wm withdraw .
set infile tgdb.hlp
set outfile tgdb.man
puts "Creating man page \"$outfile\" from \"$infile\"..."

proc mknumber { sub } {
  global Number

  set lvl [expr [clength $sub] / 3]
  incr Number($lvl)
  loop i $lvl+1 5 { set Number($i) 0 }
  set val $Number(0)
  loop i 1 $lvl+1 { append val ".$Number($i)" }
  if { $lvl == 0 } {
    return "$val."
  } else {
    return $val
  }
}

set Number(0) -1
set version \
  [exec fgrep "set tgdb_version" tgdb.tcl | sed -e "s/set tgdb_version //"]
set odesc [open $outfile "w"]
puts $odesc ".TH tgdb 1 \"Version $version\" tgdb"
puts $odesc ".SH NAME"
puts $odesc "tgdb \\- a Tcl/Tk based frontend to gdb"
puts $odesc ".SH SYNOPSIS"
puts $odesc ".B\ntgdb\n\[gdb_options\]"
puts $odesc ".SH DESCRIPTION\n.br"
set state normal
for_file line $infile {
  regsub -all -- - $line "\\-" line
  while { 1 } {
    if { [string trim $line] == "" } {
      if { $state == "normal" } {
        puts $odesc ".br\n.PP"
      }
      break
    }
    if { $state == "tcl" } {
      if { [cindex $line 0] == ";" } {
        if { [set line [string trim [crange $line 1 end]]] == "" } {
	  break
	}
      } elseif { [regexp {^gotoidx "?([^";%]*)[";]?} $line {} where] } {
        puts -nonewline $odesc "\\fI$where\\fP"
        regsub {^gotoidx "?[^";%]*[";]?} $line "" line
      } elseif { [regexp {^make([sub]*)title "?([^";%]*)[";]?} $line {} \
		   sub title] } {
	regsub -all idx $title "" title
        puts $odesc ".SH [mknumber $sub]  $title"
        regsub {^make[sub]*title "?[^";%]*[";]?} $line "" line
      } elseif { [regexp {^[^;%]+;?} $line] } {
        regsub {^[^;%]+;?} $line {} line
      } elseif { [set pos [string first "%%" $line]] != -1 } {
	set state normal
	if { [set line [string range $line [expr $pos + 2] end]] == "" } {
	  puts $odesc ""
	  break
	} elseif { [cindex $line 0] == " " } {
	  puts -nonewline $odesc " "
	}
      }
    } else {
      set xline [string trimleft $line]
      if {   [string first "\\-" $xline] == 0 } {
	puts $odesc ".TP 2"
      }
      if { [set pos [string first "%%" $xline]] == -1 } {
        puts $odesc $xline
        break
      }
      set state tcl
      puts -nonewline $odesc [string range $xline 0 [expr $pos - 1]]
      if { [set line [string range $xline [expr $pos + 2] end]] == "" } {
	puts -nonewline $odesc " "
	break
      }
    }
  }
}
puts $odesc ".SH AUTHOR"
puts $odesc "Michael Schumacher, c/o HighTec EDV-Systeme GmbH\n.br"
puts $odesc "Neue Bahnhofstr. 71\n.br"
puts $odesc "66386 St.Ingbert, Germany\n.br"
puts $odesc "Email: mike@HighTec.saarlink.de"
puts $odesc ".SH BUGS"
puts $odesc "No one is perfect... If you should find a bug, feel free to"
puts $odesc "drop us a note, but include tgdb's version number, and make"
puts $odesc "sure that we can reproduce the erroneous behaviour. Please"
puts $odesc "send your bug reports/fixes, comments, suggestions, etc. to"
puts $odesc "tgdb@HighTec.saarlink.de."
close $odesc
puts "*** DONE ***"
destroy .
