]> git.plutz.net Git - busy/blob - index.cgi
write messages for fatal errors to debug channel
[busy] / index.cgi
1 #!/bin/zsh
2
3 # Copyright 2014 Paul Hänsch
4 #
5 # This file is part of Busy.
6 # (originally written for and copied from Confetti)
7
8 # Busy is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # Busy is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU Affero General Public License for more details.
17
18 # You should have received a copy of the GNU Affero General Public License
19 # along with Busy.  If not, see <http://www.gnu.org/licenses/>. 
20
21 export LC_ALL=C LANG=C
22 DBG_ENABLED=false
23 DBG_FILE=/dev/stderr
24
25 # this program is supposed to be symlinked into a http root directory
26 # we will use the http root as object storage (data directory) and call sub
27 # programs from the directory in which the real executable resides
28 # therefore we need to identify the code and data directories _EXEC and _DATA
29 call="$0"
30 real="$(readlink -f $call)"
31 _EXEC="$(dirname "$real")"  #execution directory
32 _DATA="$(dirname "$call")"  #storage directory
33
34 # put debug options in the local.opts file
35 [ -r "$_DATA/local.opts" ] && . "$_DATA/local.opts"
36
37 # basic functions
38 debug() { #change to false to disable debugging
39   if [ "$DBG_ENABLED" = true -a -n "$*" ]; then
40     echo -E "$*" >>"$DBG_FILE"
41   elif [ "$DBG_ENABLED" = true -a -z "$*" ]; then
42     tee -a "$DBG_FILE"
43   elif [ -z "$*" ]; then
44     cat
45   fi
46 }
47
48 die() {
49   debug "$*"
50   echo -E "$*" >>/dev/stderr
51   exit 1
52 }
53
54 # create directories for object storage
55 [ -w "$_DATA" ] && [ -d "$_DATA" ] || die "storage directory must be writable"
56
57 for each in "$_DATA"/{Home,Tasks,Wiki,QRcodes}; do
58   [ ! -e "$each" ] && mkdir "$each"
59   [ -w "$each" ] && [ -d "$each" ] || die "storage $each must be a writable directory"
60 done
61
62 # create htaccess file
63 [ -f .htaccess ] || cat >.htaccess <<EOF
64 Options         +ExecCGI
65 AddHandler      cgi-script .cgi
66 DirectoryIndex  index.cgi
67 EOF
68 [ -f .htaccess ] || die "no htaccess file present and unable to create one"
69
70 . "$_EXEC/cgi.sh"
71 . "$_EXEC/constants.sh"
72
73 cgi_get
74
75 if [ -n "$_GET[\"action\"]" ]; then
76   . "$_EXEC/action.sh"
77 elif [ -n "$_GET[\"export\"]" ]; then
78   . "$_EXEC/export.sh"
79 else
80   . "$_EXEC/page.sh"
81 fi