]> git.plutz.net Git - cgilite/blob - index.cgi
require no external index.cgi
[cgilite] / index.cgi
1 #!/bin/zsh
2
3 # Copyright 2014, 2015 Paul Hänsch
4 #
5 # This file is part of shcgi.
6
7 # shcgi 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 # shcgi 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 shcgi.  If not, see <http://www.gnu.org/licenses/>. 
19
20 export LC_ALL=C
21
22 # this program is supposed to be symlinked into a http root directory
23 # we will use the http root as object storage (data directory) and call sub
24 # programs from the directory in which the real executable resides
25 # therefore we need to identify the code and data directories _EXEC and _DATA
26 call="$0"
27 real="$(readlink -f $call)"
28 _DATA="$(dirname "$call")"  #storage directory
29 _EXEC="${real%/shcgi/index.cgi}"  #execution directory
30
31 # basic functions
32 die() {
33   echo "$@" >&2
34   exit 1
35 }
36
37 debug() { #change to false to disable debugging
38   #true && echo "$*" >>debug
39   true && [ -n "$*" ] && echo -E "$*" >>/dev/stderr
40   true && [ -z "$*" ] && tee /dev/stderr
41 }
42
43 data_dirs(){
44   # create directories for object storage
45
46   [ -d "$_DATA" -a -w "$_DATA" ] || die "storage directory must be writable"
47   for each in "$@"; do
48     [ ! -e "$_DATA/$each" ] && mkdir "$_DATA/$each"
49     [ -d "$_DATA/$each" -a -w "$_DATA/$each" ] || die "storage \"$_DATA/$each\" must be a writable directory"
50   done
51 }
52
53 # create htaccess file
54 [ -f "$_DATA/.htaccess" ] || cat >"$_DATA/.htaccess" <<EOF
55 Options         +ExecCGI
56 AddHandler      cgi-script .cgi
57 DirectoryIndex  index.cgi
58 EOF
59 [ -f "$_DATA/.htaccess" ] || die "no htaccess file present and unable to create one"
60
61 . "$_EXEC/shcgi/cgi.sh"
62
63 cgi_get
64
65 [ -x "$_EXEC/constants.sh" ] && . "$_EXEC/constants.sh"
66
67 if [ -n "$_GET[\"action\"]" ]; then
68   . "$_EXEC/shcgi/action.sh"
69 elif [ -n "$_GET[\"export\"]" ]; then
70   . "$_EXEC/shcgi/export.sh"
71 else
72   . "$_EXEC/shcgi/page.sh"
73 fi