merge from cgilite
[confetti] / index.cgi
1 #!/bin/sh
2
3 for n in "$@"; do case ${n%%=*} in
4   data) _DATA="${n#data=}";;
5   exec) _EXEC="${n#exec=}";;
6   debug) DEBUG="${n#debug=}";;
7 esac; done
8
9 [ ! "${_EXEC%/}" ] && _EXEC="$(realpath "${0%/*}")" || _EXEC="${_EXEC%/}"
10 [ ! "${_DATA%/}" ] && _DATA=. || _DATA="${_DATA%/}"
11 [ "$DEBUG" ] && exec 2>>"$DEBUG"
12
13 mkdir -p "${_DATA}/cache" "${_DATA}/mappings" "${_DATA}/export" "${_DATA}/lock" "${_DATA}/ical" "${_DATA}/vcard"
14
15 debug() {
16   local dbg=/dev/stderr
17   if [ ! "$DEBUG" ]; then
18     [ "$#" -gt 0 ] && : || cat;
19   elif [ "$#" -gt 0 ]; then
20     printf '%s\n' "$@" >>"$dbg"
21   else
22     tee -a "$dbg"
23   fi
24 }
25
26 . "$_EXEC/cgilite/cgilite.sh"
27 . "$_EXEC/cgilite/session.sh"
28
29 . "$_EXEC/l10n.sh"
30
31 _PATH="$(PATH "/${PATH_INFO}")"
32 ACTION="$(GET a)"
33
34 message="$(COOKIE message)"
35 [ "$message" ] && SET_COOKIE 0 message=''
36
37 checked(){
38   if [ "$1" = "$2" ] || [ "$1" -eq "$2" ]; then
39     printf 'checked="checked"'
40   fi 2>/dev/null
41 }
42 selected(){
43   if [ "$1" = "$2" ] || [ "$1" -eq "$2" ]; then
44     printf 'selected="selected"'
45   fi 2>/dev/null
46 }
47
48 yield_page() {
49   local class="$1" style="$2"
50   printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
51   { printf '
52         [html [head
53           [title Confetti]
54           [meta name="viewport" content="width=device-width"]
55           [link rel="stylesheet" type="text/css" href="/style.css"]
56     '
57     [ -n "$style" ] && printf '
58           [link rel="stylesheet" type="text/css" href="%s"]
59     ' "$style"
60     printf '
61         ] [body #top class="%s"
62     ' "$class"
63     printf '[ul .menu [li [a "/cards/" . %s]][li [a "/courses/" . %s]]]' "$(l10n cards)" "$(l10n courses)"
64     [ "$message" ] && printf '[p #message\n%s\n]' "$(l10n "$message")"
65     cat
66     printf '] ]'
67   } \
68   | "${_EXEC}/cgilite/html-sh.sed"
69 }
70
71 topdir="${_PATH#/}"
72 topdir="/${topdir%%/*}"
73
74 case ${_PATH} in
75   /) REDIRECT /cards/
76     ;;
77   /export/*.pdf) . "$_EXEC/cgilite/file.sh"
78     FILE "${_DATA}/${_PATH}" "application/pdf"
79     ;;
80   /export/*) . "$_EXEC/cgilite/file.sh"
81     FILE "${_DATA}/${_PATH}"
82     ;;
83   *)
84     if   [ -d "${_EXEC}/${_PATH}" -a -x "${_EXEC}/${_PATH}/index.cgi" ]; then
85       . "${_EXEC}/${_PATH}/index.cgi"
86     elif [ -f "${_EXEC}/${_PATH}" -a -x "${_EXEC}/${_PATH}" ]; then
87       . "${_EXEC}/${_PATH}"
88     elif [ -f "${_EXEC}/${_PATH}" -a -r "${_EXEC}/${_PATH}" ]; then
89       . "$_EXEC/cgilite/file.sh"
90       FILE "${_EXEC}/${_PATH}"
91     elif [ -d "${_EXEC}/${topdir}" -a -x "${_EXEC}/${topdir}/index.cgi" ]; then
92       . "${_EXEC}/${topdir}/index.cgi"
93     else
94       printf '%s\r\n' 'Status: 404 Not Found' 'Content-Length: 0' ''
95     fi
96     ;;
97 esac