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