# -*- C -*- --- well, not actually...
#
# Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
#                    Helsinki University of Technology, Finland.
#                    All rights reserved.
# Copyright (C) 2005 - 2007 The AROS Dev Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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.
#

BEGIN {
  FS="[\t ]*;[\t ]*"; 
  level=1; 
  tab=""; 
  vars=""; 
  var2="";
  GFN = "kern/variables.src (" FILENAME ")";
  TEXTARGET=0;CTARGET=0;TEXITARGET=0
  if (TARGETTI == "TEX") {
    TEXTARGET = 1;
    startcomment = "% -*- LaTeX -*-";
    comment = "%";
    endcomment = "%";
  }else if (TARGETTI == "TEXINFO") {
    level = 0; 
    TEXITARGET = 1;
    startcomment = "@c -*- Texinfo -*-";
    comment = "@c ";
    endcomment = "@c ";
  } else if (TARGETTI == "C") {
    CTARGET = 1;
    startcomment = "/* -*- C -*-";
    comment = " *";
    endcomment = " */";
  } else printf "Unknown target format '%s'\n",TARGETTI > "/dev/stderr"
  print startcomment;
  printf "%s This file is automatically generated from %s\n", comment, GFN;
  print comment;
  printf "%s Do not change this file\n", comment;
  print endcomment;
  if (CTARGET) {
    print "";
    print "\#include <sys/param.h>";
    print "\#include <netinet/in.h>";
    print "\#include <kern/amiga_includes.h>";
    print "\#include <kern/amiga_config.h>";
    print "\#include <sys/socket.h>";
    print "";
  }
}

# skip comments and empty lines
/^#/ { next } 
/^ *$/ { next }
# Collect global variables
/^global/ { 
  gsub("^global ", ""); 
  globals = globals "\n" $0;
  next;
}
# Change _ to \_ in TeX
TEXTARGET && /_/ { gsub("_","\\_") }
# Generate indentation, add environment parenthesis
level != $2 && TEXTARGET { 
  if (level < $2) 
    for(i=level; i < $2; i++) {
      tab = sprintf("%"2*i"s", "");
      print tab "\\begin{description}";
    } 
  else 
    for(i=level; i > $2; i--) {
      tab = sprintf("% "2*i "s", "");
      print tab "\\end{description}";
    }
  level = $2;
  tab = sprintf("%" 2*level "s", "");
  print "";
}
TEXTARGET { 
  n = split($1,temp,"=");
  printf "%s\\item[\\code{%s}] %s\n",tab,temp[n],$3;
  if(n>1) {
    printf tab " \\Alias%s{%s",(n>2)?"es":"",temp[1];
      for(i=2;i<n;i++) 
	printf "{\\rm ,} %s",temp[i];
    print "}";
  }
  print "";
  if ($2 == 1) {
    # Skip C declarations
    getline dummy; 
    getline dummy; 
  }
}
#
# TEXINFO
#
$2 == 2 && TEXITARGET { 
  if (level == 1)
      print "@table @code";    
  n = split($1,temp,"=");print "level=" $2, n > "/dev/stderr"
  printf "@item %s\n",temp[n];
  for(i=1;i<n;i++) {
    printf "@itemx %s\n",temp[i];
  }
  gsub("\\\\n","\n",$3) # replace with newlines
  print $3;print "";
  level = $2;
  print "";
}
$2 == 1 && TEXITARGET { 
  if (level == 2)
    print "@end table";
  if (level != 0)
    print "@end deffn\n";
  n = split($1,temp,"="); print "level=" $2, n > "/dev/stderr"
  printf "@deffn {AREXX Variable} %s\n",temp[n];
  for(i=1;i<n;i++) {
    printf "@deffnx {AREXX Variable} %s\n",temp[i];
  }
  gsub("\\\\n","\n",$3) # replace with newlines
  print $3;print "";
  level = $2;
  print "";

  if ($2 == 1) {
    # Skip C declarations
    getline dummy; 
    getline dummy; 
  }
}
($2 != 1 && $2 != 2) && TEXITARGET {
  print FILENAME ":" FNR ": Invalid level: " $2 > "/dev/stderr"
}
#
# Make a C file
#
CTARGET {
  n = split($1, temp, "=");
  if ($2 == 1) {
    if (length(var2) > 0) {
      printf "/* %s */\n#define KW_%s \\\n \"", varcom, curvar;
      n2 = split(var2, t2, ",");
      cnt = 0;
      for (i = 1; i < n2; i++) {
	if ((cnt + length(t2[i])) > 60) {
	  printf "\" \\\n \"";
	  cnt=0;
	}
	cnt += length(t2[i]);
	printf "%s,",t2[i];
      }
      if (length(t2[n2]) + cnt > 60) 
	printf "\" \\\n \"";
      printf "%s\"\n\n", t2[n2];
    }
    curvar=temp[n];
    varcom=$3;
    if (length(vars) > 0) {
      vars=vars "," $1;
    } else {
      vars=$1;
    }
    var2 = "";
    # Get external vars
    getline extern;
    externs = externs "\n" extern;
    # get variables line
    getline varline;
    if (length(varlines) > 0) {
      varlines = varlines ",\n" varline;
    } else {
      varlines = varline;
    }
  } else {
    if(length(var2) == 0) {
      var2=$1;
    } else {
      var2 = var2 "," $1;
    }
  }
}
	
END {
  if (TEXTARGET) {
    for(i = level; i > 1; i--)
      print "\\end{description}";
  } else if(TEXITARGET) {
    if (level == 2)
       print "@end table";
    if (level != 0)
       print "@end deffn\n";
  } else if(CTARGET) {
    if (length(var2) > 0) {
      printf "/* %s */\n#define KW_%s \\\n \"", varcom, curvar;
      n2 = split(var2, t2, ",");
      cnt = 0;
      for(i=1;i<n2;i++) {
	if((cnt+length(t2[i]))>60) {
	  printf "\" \\\n \"";
	  cnt=0;
	}
	cnt += length(t2[i]);
	printf "%s,",t2[i];
      }
      if (length(t2[n2])+cnt> 60) printf "\" \\\n \"";
      printf "%s\"\n\n", t2[n2];
    }

    if (length(vars) > 0) {
      printf "/* %s */\nSTRPTR KW_%s =\n  \"", "Level 1 variables", "VARS";
      n2 = split(vars, t2, ","); cnt=0;
      for(i=1; i<n2; i++) {
	if(cnt + length(t2[i]) > 60) {
	  printf "\"\n  \"";
	  cnt=0;
	}
	cnt+=length(t2[i]);
	printf "%s,",t2[i];
      }
      printf "%s\";\n\n", t2[n2];
    }
    if (length(externs)) {
      print "/* extern declarations */";
      print externs;
      print;
    }
    if (length(globals)) {
      printf "/* Global variables */";
      print globals;
      print;
    }
    if (length(varlines)) {
      print "struct cfg_variable variables[] = {";
      print varlines;
      print "};";
    }
  }
}
