Merge commit '69f00ca6b1c936ca39cba43a670852919eefb82c'
[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="/cgilite/common.css"]
56           [link rel="stylesheet" type="text/css" href="/style.css"]
57     '
58     [ -n "$style" ] && printf '
59           [link rel="stylesheet" type="text/css" href="%s"]
60     ' "$style"
61     printf '
62         ] [body #top class="%s"
63     ' "$class"
64     printf '[ul .menu [li [a "/cards/" . %s]][li [a "/courses/" . %s]]]' "$(l10n cards)" "$(l10n courses)"
65     [ "$message" ] && printf '[p #message\n%s\n]' "$(l10n "$message")"
66     cat
67     printf '] ]'
68   } \
69   | "${_EXEC}/cgilite/html-sh.sed"
70 }
71
72 topdir="${_PATH#/}"
73 topdir="/${topdir%%/*}"
74
75 case ${_PATH} in
76   /) REDIRECT /cards/
77     ;;
78   /export/*.pdf) . "$_EXEC/cgilite/file.sh"
79     FILE "${_DATA}/${_PATH}" "application/pdf"
80     ;;
81   /export/*) . "$_EXEC/cgilite/file.sh"
82     FILE "${_DATA}/${_PATH}"
83     ;;
84   *)
85     if   [ -d "${_EXEC}/${_PATH}" -a -x "${_EXEC}/${_PATH}/index.cgi" ]; then
86       . "${_EXEC}/${_PATH}/index.cgi"
87     elif [ -f "${_EXEC}/${_PATH}" -a -x "${_EXEC}/${_PATH}" ]; then
88       . "${_EXEC}/${_PATH}"
89     elif [ -f "${_EXEC}/${_PATH}" -a -r "${_EXEC}/${_PATH}" ]; then
90       . "$_EXEC/cgilite/file.sh"
91       FILE "${_EXEC}/${_PATH}"
92     elif [ -d "${_EXEC}/${topdir}" -a -x "${_EXEC}/${topdir}/index.cgi" ]; then
93       . "${_EXEC}/${topdir}/index.cgi"
94     else
95       printf '%s\r\n' 'Status: 404 Not Found' 'Content-Length: 0' ''
96     fi
97     ;;
98 esac