]> git.plutz.net Git - confetti/blob - index.cgi
4d050e8700c19407fc2c85a8082109077a958d7a
[confetti] / index.cgi
1 #!/bin/zsh
2
3 # basic functions
4 die() {
5   echo "$*" >/dev/stderr
6   exit 1
7 }
8 debug() { #change to false to disable debugging
9   true && echo "$*" >>debug
10 }
11
12 # this program is supposed to be symlinked into a http root directory
13 # we will use the http root as object storage (data directory) and call sub
14 # programs from the directory in which the real executable resides
15 # therefore we need to identify the code and data directories _EXEC and _STOR
16 call=$0
17 real=$call
18 while [ -L "$real" ]; do
19   real="$(stat -c %N "$real" |sed -r "s:..*. -> .(.*).$:\1:")"
20 done
21
22 _EXEC="$(dirname "$real")"  #execution directory
23 _STOR="$(dirname "$call")"  #storage directory
24
25 debug "Execution dir: $_EXEC"
26 debug "Storage dir: $_STOR"
27
28 [ -w "$_EXEC" ] && [ -d "$_EXEC" ] || die "storage directory must be writable"
29
30 # create directories for object storage
31 for each in "$_STOR"/{vcard,mappings,courses}; do
32   [ ! -e "$each" ] && mkdir "$each"
33   [ -w "$each" ] && [ -d "$each" ] || die "storage $each must be a writable directory"
34 done
35
36 # create htaccess file
37 [ -f .htaccess ] || cat >.htaccess <<EOF
38 Options         +ExecCGI
39 AddHandler      cgi-script .cgi
40 DirectoryIndex  index.cgi
41 EOF
42 [ -f .htaccess ] || die "no htaccess file present and unable to create one"
43
44 . "$_EXEC/cgi.sh"
45
46 cgi_get
47 debug "$_GET"
48
49 . "$_EXEC/page.sh"