#!/usr/local/bin/tclsh8.0

# ---------------------------------------------------------------------
# Copyright (c) 1996-1997 Christian Krone. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Tcl itself.
# See also ../licence.terms
# ---------------------------------------------------------------------

if {[llength $argv] < 3} {
  puts stderr "Call: $argv0 html|latex|plain deu|eng file"; exit 1
}
switch [lindex $argv 0] {
  html  {set _cgi_target html}
  latex {set _cgi_target latex}
  plain {set _cgi_target plain}
  default {puts stderr "Unknown target [lindex $argv 0]"; exit 1}
}
switch [lindex $argv 1] {
  deu {set _cgi_lang deu}
  eng {set _cgi_lang eng}
  default {puts stderr "Unknown language [lindex $argv 1]"; exit 1}
}
set file [lindex $argv 2]

source cgi.tcl
cgi_uid_check -off
switch $_cgi_target {
  latex {source cgi2tex.tcl}
  plain {source cgi2plain.tcl}
}

switch $_cgi_target {
  html {
    proc sec    {text} {anchor_name sec:$text;   h2 $text}
    proc ssec   {text} {anchor_name ssec:$text;  h3 $text}
    proc sssec  {text} {anchor_name sssec:$text; h4 $text}
    proc table_of_contents {} {}
  }
  latex {
    proc title  {text} {cgi_title $text}
    proc sec    {text} {h2 $text; anchor_name sec:$text}
    proc ssec   {text} {h3 $text; anchor_name ssec:$text}
    proc sssec  {text} {h4 $text; anchor_name sssec:$text}
    proc table_of_contents {} {cgi_toc}
  }
  plain {
    proc title  {text} {cgi_title $text; h1 $text}
    proc sec    {text} {h2 $text; anchor_name sec:$text}
    proc ssec   {text} {h3 $text; anchor_name ssec:$text}
    proc sssec  {text} {h4 $text; anchor_name sssec:$text}
    proc table_of_contents {} {}
  }
}

proc ref {kind text {anchor ""}} {
  global _cgi_link _cgi_lang _cgi_target

  if {![string length $anchor]} {
    set anchor $text
  }
  if {![info exists _cgi_link($kind:text)]} {
    switch $_cgi_target {
      html  {set hash #}
      latex {set hash ""}
      plain {set hash ""}
    }
    link $kind:$text $anchor $hash$kind:$text
  }
  switch $_cgi_lang {
    deu {
      switch $kind {
	sec     {set titel Kapitel}
	ssec    {set titel Abschnitt}
	sssec   {set titel Unterabschnitt}
	fig     {set titel Bild}
	tab     {set titel Tabelle}
	default {set titel Referenz}
      }
    }
    eng {
      switch $kind {
	sec     {set titel chapter}
	ssec    {set titel section}
	sssec   {set titel subsection}
	fig     {set titel picture}
	tab     {set titel table}
	default {set titel reference}
      }
    }
  }
  return "$titel [link $kind:$text]"
}

proc prog   {text} {typewriter $text}
proc func   {text} {typewriter $text}
proc screen {text} {typewriter $text}
proc path   {text} {typewriter $text}
proc dq     {text} {return ",,$text''"}

switch $_cgi_target {
  html {
    proc img {name {caption ""}} {
      global _cgi_lang
      anchor_name fig:$name
      p align=center [cgi_img gif-$_cgi_lang/$name.gif]
      if {[string length $caption]} {
	p align=center $caption
      }
    }
  }
  latex {
    proc img {name {caption ""}} {
      global _cgi_lang
      if {[string length $caption]} {
	set capOpt caption=$caption
      } else {
	set capOpt ""
      }
      cgi_figure $capOpt label=fig:$name [cgi_img eps-$_cgi_lang/$name.ps]
    }
  }
  plain {
    proc img {name {caption ""}} {
    }
  }
}

proc table {name body} {
  append tabBody [list anchor_name tab:$name;]
  append tabBody "$body;"
  append tabBody "caption align=bottom \"$name\""

  uplevel #1 cgi_table border [list $tabBody]
}

source $file

# ---------------------------------------------------------------------
# Some stuff for editing with emacs...
# ---------------------------------------------------------------------
# Local Variables: 
# mode: tcl
# End: 
