summaryrefslogtreecommitdiff
path: root/index.cgi
blob: 231db39249c4525188cf54b801d428be6e94d278 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/awk -f
#
# AWKsite v0.2
#
# AWK CGI website main program file
#
# AWK CGI is Copyright by Paul C. Buetow (2005)
#	http://buetow.org 
#	Runs on FreeBSD 6.x 
#
# AWK CGI is Copyright by Paul C. Buetow (2009)
#	Update for Debian Lenny (GNU AWK)
#

BEGIN {
  config_file = "awksite.conf"

  read_config_values(config_file)
  print_http_header()
  process_foreach_line()
}

function print_http_header() {
  print "Content-type: text/html\n\n"
}

function read_config_values(config_file) {
  while ((getline < config_file) > 0) {
    position = index($0,"=")
    if (position == 0 || /^#/)
      continue

    key = substr($0, 0, position)
    val = substr($0, position+1, 100)

    if (val ~ /^!/) 
       substr(val, 2, 100) | getline val	

    values[key] = val
  }

  close(config_file)
}

function process_foreach_line() {
  template_file = values["template"]

  while ((getline < template_file) > 0)
    print process_line($0)
  close(template_file)
}

function process_line(line) {
  if (line ~ /%%.+%%/)
    return insert_template_value(line) 
  return line
}

function insert_template_value(line) {
   position1 = index(line, "%%") + 2 
   temp = substr(line, position1, 100)

   if ((position2 = index(temp, "%%") ) == 1 ) 
	return line

   key = substr(temp, 0, position2)  

   if (key ~ /^!sort /) 
     values[key] = read_file_sorted(substr(key, 7, 100))

   gsub("%%" key "%%", values[key], line)

   if (line ~ /%%/) 
	return insert_template_value(line)

   return line 
}

function read_file_sorted(file) {
   retval = ""; command = "cat " file " | sort"
   while (( command | getline ) > 0 )
     retval = retval $0 "<br>\n"
   return retval
}

function debug(message) {
   print "DEBUG " message
}