allow running from sub directory of base location
authorPaul Hänsch <paul@plutz.net>
Wed, 28 Apr 2021 10:57:03 +0000 (12:57 +0200)
committerPaul Hänsch <paul@plutz.net>
Wed, 28 Apr 2021 10:57:03 +0000 (12:57 +0200)
17 files changed:
cards/edit_card.sh
cards/filter_card.sh
cards/index.cgi
cards/list.sh
cards/new_card.sh
cards/update_card.sh
cards/widgets.sh
categories/edit_categories.sh
categories/update_categories.sh
courses/edit_course.sh
courses/export_pdf.sh
courses/index.cgi
courses/list.sh
courses/new_course.sh
courses/update_course.sh
index.cgi
style.css

index 4239cc5..897bcc2 100755 (executable)
@@ -26,11 +26,11 @@ filter="$(REF f)"
 order="$(REF o)"
 
 if tempfile="$(SLOCK "$cardfile" "$locktimeout")"; then
-  REDIRECT "/cards/?o=${order}&f=${filter}&e=${card}"
+  REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}&e=${card}"
 elif [ -f "$tempfile" ]; then
   SET_COOKIE session message="SESSLOCK"
-  REDIRECT "/cards/?o=${order}&f=${filter}#${card}"
+  REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}#${card}"
 else
   SET_COOKIE session message="EDITLOCK"
-  REDIRECT "/cards/?o=${order}&f=${filter}#${card}"
+  REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}#${card}"
 fi
index aacacbb..504fb08 100755 (executable)
@@ -44,15 +44,15 @@ filter="$(
 
 case $(POST choice) in
   filter)
-    REDIRECT "/cards/?o=$(POST order)&f=${filter}"
+    REDIRECT "${_BASE}/cards/?o=$(POST order)&f=${filter}"
     ;;
   new_filter)
-    REDIRECT "/cards/?o=$(POST order)&f=${filter}&newfilter=yes"
+    REDIRECT "${_BASE}/cards/?o=$(POST order)&f=${filter}&newfilter=yes"
     ;;
   export_csv)
-    REDIRECT "/cards/export_csv.sh?o=$(POST order)&f=${filter}"
+    REDIRECT "${_BASE}/cards/export_csv.sh?o=$(POST order)&f=${filter}"
     ;;
   *)
-    REDIRECT '/cards/'
+    REDIRECT "${_BASE}/cards/"
     ;;
 esac
index 934c19a..2c9f267 100755 (executable)
@@ -16,10 +16,10 @@ edit="${edit##*/}"
 
 { w_filter_diag
   printf '
-  [form class="newcard" action="/cards/new_card.sh" method="POST"
+  [form class="newcard" action="%s/cards/new_card.sh" method="POST"
     [button type="submit" %s]
     [input name="seed" placeholder="%s"]
-  ]' "$(l10n newcard)" "$(l10n vcf_seed_label)"
+  ]' "${_BASE}" "$(l10n newcard)" "$(l10n vcf_seed_label)"
   [ "$edit" ] && edit_card "$edit"
   list_cards
 } | yield_page cards #/cards/cards.css
index 0ed8af4..2f2e767 100755 (executable)
@@ -13,7 +13,7 @@ edit_card(){
   else
     card="$(pdi_load "$tempfile")"
     cat <<-EOF
-       [form .card #${cardfile##*/} action="/cards/update_card.sh" method="POST"
+       [form .card #${cardfile##*/} action="${_BASE}/cards/update_card.sh" method="POST"
          [input type="hidden" name="tid" value="$(transid ${tempfile})"]
          [div .section .basic $(
            edit_item "$card" N GENDER
@@ -94,15 +94,15 @@ print_card(){
       [div .section .note    . $(card_item "$card" NOTE)]
       [div .section .attendance [h3 $(l10n course_attendance) ] [ul
         $(grep -F "    ${cardfile##*/}" "$_DATA/mappings/attendance" |while read each discard; do
-          printf '[li [a .item .attendance href="/courses#%s" . %s]]' \
-                 "$each" \
+          printf '[li [a .item .attendance href="%s/courses#%s" . %s]]' \
+                 "${_BASE}" "$each" \
                  "$(pdi_value "$(pdi_load "$_DATA/ical/$each")" SUMMARY || l10n "(unnamed course)" |unescape |HTML)"
         done |sort -k7)]
         $(card_item "$card" CATEGORIES)
       ]
       [div .control
-        [a .button .item href="/cards/edit_card.sh?card=${cardfile##*/}" $(l10n edit)]
-        [a .button .item href="/cards/export_card.sh?card=${cardfile##*/}" $(l10n vcf_export)]
+        [a .button .item href="${_BASE}/cards/edit_card.sh?card=${cardfile##*/}" $(l10n edit)]
+        [a .button .item href="${_BASE}/cards/export_card.sh?card=${cardfile##*/}" $(l10n vcf_export)]
       ]
     ]
        EOF
index 424f242..69f89c6 100755 (executable)
@@ -68,8 +68,8 @@ if mkdir "$lockdir"; then
        UID:${uid}
        END:VCARD
        EOF
-  REDIRECT "/cards/?o=${order}&f=${filter}&e=${card}"
+  REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}&e=${card}"
 else
   SET_COOKIE session message="EDITLOCK"
-  REDIRECT "/cards/?o=${order}&f=${filter}"
+  REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}"
 fi
index d942e9a..2b87632 100755 (executable)
@@ -42,11 +42,11 @@ fi
 
 if ! tempfile=$(CHECK_SLOCK "$cardfile"); then
   SET_COOKIE 0 message="NO VALID FILE LOCK"
-  REDIRECT "/cards/?o=${order}&f=${filter}&e=${card}"
+  REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}&e=${card}"
   exit 0
 elif [ "$(POST tid)" != "$(transid "$tempfile")" ]; then
   SET_COOKIE 0 message="INVALID TRANSACTION ID"
-  REDIRECT "/cards/?o=${order}&f=${filter}&e=${card}"
+  REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}&e=${card}"
   exit 0
 fi
 
@@ -105,7 +105,7 @@ printf '%s' "$vcf" |grep -vx '' >"$tempfile"
 
 case "$action" in
   addfield)
-    REDIRECT "/cards/?o=${order}&f=${filter}&e=${card}"
+    REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}&e=${card}"
     ;;
   update)
     if LOCK "$attfile"; then
@@ -126,13 +126,13 @@ case "$action" in
 
     cp "$tempfile" "$cardfile"
     RELEASE_SLOCK "$cardfile"
-    REDIRECT "/cards/?o=${order}&f=${filter}#${card}"
+    REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}#${card}"
     ;;
   cancel)
     RELEASE_SLOCK "$cardfile"
     [ -f "$cardfile" ] \
-    && REDIRECT "/cards/?o=${order}&f=${filter}#${card}" \
-    || REDIRECT "/cards/?o=${order}&f=${filter}"
+    && REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}#${card}" \
+    || REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}"
     ;;
   delete)
     rm "$cardfile"
@@ -146,6 +146,6 @@ case "$action" in
     else
       SET_COOKIE 0 message="COULD NOT UPDATE COURSE MAPPINGS"
     fi
-    REDIRECT "/cards/?o=${order}&f=${filter}"
+    REDIRECT "${_BASE}/cards/?o=${order}&f=${filter}"
     ;;
 esac
index 09956a7..eb9d0fd 100755 (executable)
@@ -51,7 +51,7 @@ cat <<EOF
                  "$(printf %s "$cat" |grep -qxEe "$2" && printf checked )" \
                  "$(HTML "$cat")"
       done)
-      [a href="/categories/" $(l10n edit_categories)]
+      [a href="${_BASE}/categories/" $(l10n edit_categories)]
     ]
     [fieldset .tab .courses
       $(list_courses | while IFS=/ read course coursename; do
@@ -67,7 +67,7 @@ EOF
 
 w_filter_diag(){
   cat <<EOF
-  [form .filter action="/cards/filter_card.sh" method="POST"
+  [form .filter action="${_BASE}/cards/filter_card.sh" method="POST"
     [h1 $(l10n filter_label)]
     [input type="hidden" name="page" value="cards"]
   
index 7ee6f36..2eef152 100755 (executable)
@@ -32,4 +32,4 @@ elif [ "$remove" ]; then
   sed -E -i '/^'"${remove}"'$/d' "$catfile"
 fi
 
-REDIRECT "/categories/"
+REDIRECT "${_BASE}/categories/"
index 108c5d5..a26f93d 100755 (executable)
@@ -38,4 +38,4 @@ for card in "${_DATA}"/vcard/*.vcf; do
   fi
 done
 
-REDIRECT /categories/
+REDIRECT "${_BASE}/categories/"
index 3c0c54f..e8ce0c6 100755 (executable)
@@ -24,11 +24,11 @@ course="$(GET course |PATH)"
 coursefile="$_DATA/ical/${course##*/}"
 
 if tempfile="$(SLOCK "$coursefile" "$locktimeout")"; then
-  REDIRECT "/courses/?e=${course}"
+  REDIRECT "${_BASE}/courses/?e=${course}"
 elif [ -f "$tempfile" ]; then
   SET_COOKIE session message="SESSLOCK"
-  REDIRECT "/courses/#${course}"
+  REDIRECT "${_BASE}/courses/#${course}"
 else
   SET_COOKIE session message="EDITLOCK"
-  REDIRECT "/courses/#${course}"
+  REDIRECT "${_BASE}/courses/#${course}"
 fi
index 62bb69e..92e10c5 100755 (executable)
@@ -7,11 +7,11 @@ coursefile="${_DATA}/ical/$(GET course)"
 
 if [ ! -r "$coursefile" ]; then
   SET_COOKIE 0 message="Cannot read course file"
-  REDIRECT /courses/
+  REDIRECT "${_BASE}/courses/"
   return 0
 elif ! mkdir -p "$_DATA/export"; then
   SET_COOKIE 0 message="Cannot create export directory"
-  REDIRECT /courses/
+  REDIRECT "${_BASE}/courses/"
   return 0
 fi
 
@@ -123,10 +123,10 @@ style_td='style="border: 1pt solid; padding: 1mm 2mm; vertical-align: top;"'
 ]]
 EOF
 
-export            HOME="$_DATA"
+export            HOME="$_DATA/"
 export XDG_CONFIG_HOME="$_DATA/xdg_config"
 export  XDG_CACHE_HOME="$_DATA/xdg_cache"
 export   XDG_DATA_HOME="$_DATA/xdg_local"
 
 lowriter --convert-to pdf --outdir "$_DATA/export/" "$htmlfile" >/dev/null
-REDIRECT "$(URL "/export/${pdffile##*/}")"
+REDIRECT "$(URL "${_BASE}/export/${pdffile##*/}")"
index 7ff426f..f860247 100755 (executable)
@@ -15,9 +15,9 @@ edit="${edit##*/}"
 
 { w_sort_courses
   printf '
-  [form .newcourses action="/courses/new_course.sh" method="POST"
+  [form .newcourses action="%s/courses/new_course.sh" method="POST"
     [button type="submit" %s]
-  ]' "$(l10n newcourse)"
+  ]' "${_BASE}" "$(l10n newcourse)"
 
   [ "$edit" ] && edit_course "$edit"
   printf '[div .courselist\n'
index 97356db..cc0deae 100755 (executable)
@@ -15,7 +15,7 @@ edit_course(){
   else
     course="$(pdi_load "$tempfile")"
     cat <<-EOF
-       [form .course #${coursefile##*/} action="/courses/update_course.sh" method="POST"
+       [form .course #${coursefile##*/} action="${_BASE}/courses/update_course.sh" method="POST"
           [input type="hidden" name="course" value="${coursefile##*/}"]
          [input type="hidden" name="tid" value="$(transid ${tempfile})"]
           [div .section .basic . $(
@@ -60,15 +60,15 @@ print_course(){
       [div .section .COMMENT   . $(cal_item "$course" COMMENT)]
       [div .section .attendance [h3 $(l10n course_attendance) ] [ul . 
         $(grep -F "${coursefile##*/}   " "$_DATA/mappings/attendance" |while read discard each; do
-          printf '[li [a .item .attendance href="/cards/#%s" . %s]]\n' \
-                 "$each" \
+          printf '[li [a .item .attendance href="%s/cards/#%s" . %s]]\n' \
+                 "${_BASE}" "$each" \
                  "$(pdi_value "$(pdi_load "$_DATA/vcard/$each")" FN |unescape |HTML)"
         done |sort -k7)]
       ]
       [div .control
-        [a .button .item href="/courses/edit_course.sh?course=${coursefile##*/}" $(l10n edit)]
-        [a .button .item href="/courses/export_pdf.sh?course=${coursefile##*/}" target="blank" $(l10n courselist)]
-        [a .button .item href="/courses/export_ical.sh?course=${coursefile##*/}" $(l10n ics_export)]
+        [a .button .item href="${_BASE}/courses/edit_course.sh?course=${coursefile##*/}" $(l10n edit)]
+        [a .button .item href="${_BASE}/courses/export_pdf.sh?course=${coursefile##*/}" target="blank" $(l10n courselist)]
+        [a .button .item href="${_BASE}/courses/export_ical.sh?course=${coursefile##*/}" $(l10n ics_export)]
         [a .button .item href="mailto:zack@vuesch.org?bcc=$(course_mail "${coursefile##*/}" |HTML)" $(l10n course_mail)]
       ]
     ]
index 362752d..36de913 100755 (executable)
@@ -44,8 +44,8 @@ if tempfile="$(SLOCK "$coursefile")"; then
        END:VEVENT
        END:VCARD
        EOF
-  REDIRECT "/courses/?e=${course}"
+  REDIRECT "${_BASE}/courses/?e=${course}"
 else
   SET_COOKIE session message="EDITLOCK"
-  REDIRECT "/courses/"
+  REDIRECT "${_BASE}/courses/"
 fi
index 4abdbe7..aa9632c 100755 (executable)
@@ -29,11 +29,11 @@ attfile="$_DATA/mappings/attendance"
 
 if ! tempfile="$(CHECK_SLOCK "$coursefile")"; then
   SET_COOKIE 0 message="NO VALID FILE LOCK"
-  REDIRECT "/courses/?e=${course}"
+  REDIRECT "${_BASE}/courses/?e=${course}"
   exit 0
 elif [ "$(POST tid)" != "$(transid "$tempfile")" ]; then
   SET_COOKIE 0 message="INVALID TRANSACTION ID"
-  REDIRECT "/courses/?e=${course}"
+  REDIRECT "${_BASE}/courses/?e=${course}"
   exit 0
 fi
 
@@ -105,13 +105,13 @@ case "$(POST action)" in
     newfield="$(POST newfield |grep -m 1 -xE '[A-Z][A-Z0-9-]*')"
     ics="$(pdi_update_value "$ics" "$newfield" $(( $(pdi_count "$ics" "$newfield") + 1 )) '')"
     printf '%s' "$ics" |grep -vx '' >"$tempfile"
-    REDIRECT "/courses/?e=${course}"
+    REDIRECT "${_BASE}/courses/?e=${course}"
     ;;
   addfield\ [A-Z]*)
     newfield="$(POST action |sed -nE '1s;^addfield ([A-Z][A-Z0-9-]*)$;\1;p')"
     ics="$(pdi_update_value "$ics" "$newfield" $(( $(pdi_count "$ics" "$newfield") + 1 )) '')"
     printf '%s' "$ics" |grep -vx '' >"$tempfile"
-    REDIRECT "/courses/?e=${course}"
+    REDIRECT "${_BASE}/courses/?e=${course}"
     ;;
   update)
     if LOCK "$attfile"; then
@@ -133,21 +133,21 @@ case "$(POST action)" in
     printf '%s' "$ics" |grep -vx '' >"${tempfile}.cp"
     mv "${tempfile}.cp" "$coursefile"
     RELEASE_SLOCK "$coursefile"
-    REDIRECT "/courses/#${course}"
+    REDIRECT "${_BASE}/courses/#${course}"
     ;;
   cancel)
     RELEASE_SLOCK "$coursefile"
     [ -f "$coursefile" ] \
-    && REDIRECT "/courses/#${course}" \
-    || REDIRECT "/courses/"
+    && REDIRECT "${_BASE}/courses/#${course}" \
+    || REDIRECT "${_BASE}/courses/"
     ;;
   delete)
     rm "$coursefile"
     RELEASE_SLOCK "$coursefile"
-    REDIRECT "/courses/"
+    REDIRECT "${_BASE}/courses/"
     ;;
   *)
     printf '%s' "$ics" |grep -vx '' >"$tempfile"
-    REDIRECT "/courses/?e=${course}"
+    REDIRECT "${_BASE}/courses/?e=${course}"
     ;;
 esac
index 1c4e717..276965f 100755 (executable)
--- a/index.cgi
+++ b/index.cgi
@@ -3,11 +3,13 @@
 for n in "$@"; do case ${n%%=*} in
   data) _DATA="${n#data=}";;
   exec) _EXEC="${n#exec=}";;
+  base) _BASE="${n#base=}";;
   debug) DEBUG="${n#debug=}";;
 esac; done
 
 [ ! "${_EXEC%/}" ] && _EXEC="$(realpath "${0%/*}")" || _EXEC="${_EXEC%/}"
-[ ! "${_DATA%/}" ] && _DATA=. || _DATA="${_DATA%/}"
+[ ! "${_DATA%/}" ] && _DATA="${PWD%/}" || _DATA="${_DATA%/}"
+_BASE="${_BASE%/}"
 [ "$DEBUG" ] && exec 2>>"$DEBUG"
 
 mkdir -p "${_DATA}/cache" "${_DATA}/mappings" "${_DATA}/export" "${_DATA}/lock" "${_DATA}/ical" "${_DATA}/vcard"
@@ -29,6 +31,7 @@ debug() {
 . "$_EXEC/l10n.sh"
 
 _PATH="$(PATH "/${PATH_INFO}")"
+_PATH="${_PATH#${_BASE}}"
 ACTION="$(GET a)"
 
 message="$(COOKIE message)"
@@ -52,16 +55,16 @@ yield_page() {
        [html [head
           [title Confetti]
          [meta name="viewport" content="width=device-width"]
-         [link rel="stylesheet" type="text/css" href="/cgilite/common.css"]
-         [link rel="stylesheet" type="text/css" href="/style.css"]
-    '
+         [link rel="stylesheet" type="text/css" href="%s/cgilite/common.css"]
+         [link rel="stylesheet" type="text/css" href="%s/style.css"]
+    ' "${_BASE}" "${_BASE}"
     [ -n "$style" ] && printf '
          [link rel="stylesheet" type="text/css" href="%s"]
     ' "$style"
     printf '
        ] [body #top class="%s"
     ' "$class"
-    printf '[ul .menu [li [a "/cards/" . %s]][li [a "/courses/" . %s]]]' "$(l10n cards)" "$(l10n courses)"
+    printf '[ul .menu [li [a "%s/cards/" . %s]][li [a "%s/courses/" . %s]]]' "${_BASE}" "$(l10n cards)" "${_BASE}" "$(l10n courses)"
     [ "$message" ] && printf '[p #message\n%s\n]' "$(l10n "$message")"
     cat
     printf '] ]'
@@ -73,12 +76,12 @@ topdir="${_PATH#/}"
 topdir="/${topdir%%/*}"
 
 case ${_PATH} in
-  /) REDIRECT /cards/
+  "/") REDIRECT "${_BASE}/cards/"
     ;;
-  /export/*.pdf) . "$_EXEC/cgilite/file.sh"
+  "/export"/*.pdf) . "$_EXEC/cgilite/file.sh"
     FILE "${_DATA}/${_PATH}" "application/pdf"
     ;;
-  /export/*) . "$_EXEC/cgilite/file.sh"
+  "/export"/*) . "$_EXEC/cgilite/file.sh"
     FILE "${_DATA}/${_PATH}"
     ;;
   *)
index 04b1336..298ae3d 100644 (file)
--- a/style.css
+++ b/style.css
@@ -25,8 +25,8 @@ body > .menu a {
   padding: .5em 3em;
   box-shadow: inset 0 0 .5em #000;
 }
-body.cards > .menu a[href="/cards/"],
-body.courses > .menu a[href="/courses/"] {
+body.cards > .menu a[href$="/cards/"],
+body.courses > .menu a[href$="/courses/"] {
   color: #000;
   background-color: #FFF;
   box-shadow: none;