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