#!/usr/bin/awk -f

# kcs (c) 1995 by Joerg Hessdoerfer (joe@amber.dinoco.de)
# Permission to use and re-distribute this software is 
# granted according to the GPL

BEGIN {
  line = 0;
  opened = 0;
  disabled = 0;

  lookup = sprintf("%s.lookup", ENVIRON["CONFTMP"]);

  entry = 1;
  menu =  sprintf("%s.menu", ENVIRON["CONFTMP"]);

  printf("dialog --title \"Linux(tm) kernel configuration\" --menu \"Global Options\" %d 75 %d", ENVIRON["LINES"]-1, ENVIRON["LINES"] - 10 )> menu;
  printf(" \\\nCOF \"Complete configuration\" ") > menu
}

END { 
  printf(" \\\nBUI \"Build kernel\"") > menu;
  printf(" \\\nEXI \"Exit config (with saving)\"") > menu;
  printf(" \\\nEND \"Exit config (without saving)\"") > menu;
  printf ("\n") > menu;
  close(menu);

  close(lookup);
  close(file);
}

{ line++ }

/^menu/ { 
  if(opened != 0)
    {
      printf "\n" > file;
      close(file);
    }
  
  opened++;
  file = sprintf("%s.%03d", ENVIRON["CONFTMP"], opened);
  
  match($0, /\'.*\'/);
  text = substr($0, RSTART+1, RLENGTH-2);
  gsub(/\"/, "\\\"", text);
  
  printf("dialog --title \"Linux(tm) kernel configuration\" --checklist \"%s\" %d 75 %d ", text, ENVIRON["LINES"]-1, ENVIRON["LINES"] - 10 ) > file;
  
  printf(" \\\n%03d \"%s\"", entry++, text) > menu;
}

/^option/ {
  match($0, /\'.*\'/);
  text = substr($0, RSTART+1, RLENGTH-2); 
  gsub(/\"/, "\\\"", text);
  sub(/\'.*\'/, "DUMMY", $0);
  if($4 == "y") yesno = "on";
  if($4 == "n") yesno = "off";
  printf(" \\\n%03d \"%s\" %s ", line, text, yesno) > file;
  printf("%03d %s\n",line, $3) > lookup;

  taglist[$3] = line; # remember number-token relations
}

/^require/ {
  match($0, /\'.*\'/);
  text = substr($0, RSTART+1, RLENGTH-2); 
  gsub(/\"/, "\\\"", text);
  sub(/\'.*\'/, "DUMMY", $0);
  
  printf("# %03d %03d %d\ndialog --title \"%s\" --inputbox \"\\n(Old value was %s)\" 9 40 \n", taglist[$3], line, $6, text, $6) > sprintf("%s.req", file);

  printf("%03d %s\n",line, $5) > lookup;
  taglist[$5] = line; # remember number-token relations
}




