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