#!/bin/sh
-# Copyright 2022 Paul Hänsch
+# Copyright 2022, 2025 Paul Hänsch
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
. "$_EXEC/acl.sh"
. "$_EXEC/tools.sh"
-page="$1"
-
-if [ "${page#/}" = "$page" ]; then
- page="$(PATH "${PATH_INFO}/$page")"
-fi
-
-acl_read "$page" || exit 0
+[ $# = 0 ] && set -- "./*"
printf %s\\n '<ul class="macro attachment list">'
-
-for file in "$_EXEC/pages/$page/#attachments"/* "$_DATA/pages/$page/#attachments"/*; do
- [ "$file" = "$_EXEC/pages/$page/#attachments/${file##*/}" \
- -a -f "$_DATA/pages/$page/#attachments/${file##*/}" ] && continue
- stat="$(stat -c '%s %Y' -- "$file" 2>&-)" || continue
+for glob in "$@"; do
+ page="${glob%/*}"
+ [ "$page" = "$glob" ] && page=./
+ [ ! "$page" ] && page=/
+
+ acl_read "$(page_abs "${page}")" \
+ && attachment_glob "$glob"
+done \
+| while read attachment; do
+ case $attachment in
+ /*)
+ stat="$( stat -c '%s %Y' -- "$_DATA/pages/${attachment%/*}/#attachments/${attachment##*/}" \
+ || stat -c '%s %Y' -- "$_EXEC/pages/${attachment%/*}/#attachments/${attachment##*/}" \
+ 2>&-)" || continue
+ ;;
+ *)
+ stat="$( stat -c '%s %Y' -- "$_DATA/pages/${PATH_INFO}/${attachment%/*}/#attachments/${attachment##*/}" \
+ || stat -c '%s %Y' -- "$_EXEC/pages/${PATH_INFO}/${attachment%/*}/#attachments/${attachment##*/}" \
+ 2>&-)" || continue
+ ;;
+ esac
size="${stat% *}" date="${stat#* }"
-
- printf '<li><a class=name href="[attachment]/%s">%s</a>
+ printf '<li><a class=name href="%s">%s</a>
<span class=size>%s</span><span class=date>%s</span></li>' \
- "$(HTML "${file##*/}")" "$(HTML "${file##*/}")" \
- "$(size_human "$size")" "$(date -d @"$date" +"%F %T")"
+ "$(HTML "${attachment%/*}/[attachment]/${attachment##*/}")" \
+ "$(HTML "${attachment#./}")" \
+ "$(size_human "$size")" "$(date -d @"$date" +"%F %T")"
done
-
printf %s\\n '</ul>'