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