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