]> git.plutz.net Git - busy/blob - index.cgi
improved debugging functions
[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   echo -E "$*"
50   exit 1
51 }
52
53 # create directories for object storage
54 [ -w "$_DATA" ] && [ -d "$_DATA" ] || die "storage directory must be writable"
55
56 for each in "$_DATA"/{Home,Tasks,Wiki,QRcodes}; do
57   [ ! -e "$each" ] && mkdir "$each"
58   [ -w "$each" ] && [ -d "$each" ] || die "storage $each must be a writable directory"
59 done
60
61 # create htaccess file
62 [ -f .htaccess ] || cat >.htaccess <<EOF
63 Options         +ExecCGI
64 AddHandler      cgi-script .cgi
65 DirectoryIndex  index.cgi
66 EOF
67 [ -f .htaccess ] || die "no htaccess file present and unable to create one"
68
69 . "$_EXEC/cgi.sh"
70 . "$_EXEC/constants.sh"
71
72 cgi_get
73
74 if [ -n "$_GET[\"action\"]" ]; then
75   . "$_EXEC/action.sh"
76 elif [ -n "$_GET[\"export\"]" ]; then
77   . "$_EXEC/export.sh"
78 else
79   . "$_EXEC/page.sh"
80 fi