about summary refs log tree commit diff stats
path: root/cacophony.sh
blob: f893a606a8bf540388b83aa81a7ba18d0ee65440 (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
#!/bin/sh
# cacophony by C. Duckworth --- public domain

SCRIPTDIR=source

crlf() { # crlf < INPUT
	## Convert LF to CRLF
	sed 's/$/
/g'
}

http_header() { # header [STATUS]
	cat <<HEADER | crlf
HTTP/1.1 ${1:-200}
Content-Type: text/html;charset=utf-8

HEADER
}

html_footer() { # footer PROGRAM
	cat <<EOF
<footer>
copyright (C) $(stat -c %y "$1" | cut -d- -f1) Case Duckworth.
this page is a <a href="$SCRIPTDIR/${1##*/}">program</a>
served by <a href="https://git.acdw.net/cacophony">cacophony</a>.
</footer>
</body></html>
EOF
}

hexec() { # hexec PROGRAM [header args]
	prog="$1"; shift
	http_header "$@"
	"$prog"
	html_footer "$prog"
}

run() { # run
	if test / = "$PATH_INFO"
	then PATH_INFO=/index run
	else
		path="$PWD/$SCRIPTDIR$PATH_INFO"
		if ! test -d "$path" && test -x "$path"
		then hexec "$path" 200
		else http_header 404
		fi
	fi
}

run