-#!/bin/zsh
+#!/bin/sh
-# Copyright 2016 Paul Hänsch
+# Copyright 2016, 2021 Paul Hänsch
#
# This file is part of Confetti.
#
# You should have received a copy of the GNU Affero General Public License
# along with Confetti. If not, see <http://www.gnu.org/licenses/>.
-. "$_EXEC"/pages/categories.sh
+. "$_EXEC"/cgilite/storage.sh
+. "$_EXEC"/pdiread.sh
catfile="${_DATA}/mappings/categories"
-set_categories(){
- card="${_DATA}/vcard/$1"
- cats="$2"
- cardcats="$(sed -nr 's;^CATEGORIES(\;[^":]+|\;"[^"]+")*:([^\r]+)\r?$;\2;p' "$card")"
+for card in "${_DATA}"/vcard/*.vcf; do
+ n='' postcats='' cardcats=''
+ vcf="$(pdi_load "$card")"
- debug "CARD: $card"
- debug "CATS: $cardcats"
- debug "NEW: $cats"
- if [ "$cats" != "$cardcats" ]; then
- sed -ri '
+ n=1; while postcats="${postcats}${postcats:+,}$(POST "${card##*/}" $n)"; do n=$((n+1)); done
+ n=1; while cardcats="${cardcats}${cardcats:+,}$(pdi_value "$vcf" CATEGORIES $n)"; do n=$((n+1)); done
+
+ if [ "${postcats}" != "${cardcats}" ] && LOCK "$card"; then
+ sed -E -i '
/^CATEGORIES[;:]/d
- /^END:VCARD *\r?$/iCATEGORIES:'"$cats"'\r
+ /^END;?:VCARD *\r?$/iCATEGORIES:'"${postcats%,}"'\r
' "${card}"
+ RELEASE "$card"
fi
-}
-
-listcards |while read card; do
- cardcats=''
- for n in "$card" "$card"{0..100}; do
- if [ -z "${_POST[$n]}" ]; then
- set_categories "$card" "$cardcats"
- break
- fi
- cardcats="${cardcats}${cardcats:+,}${_POST[$n]}"
- done
done
-redirect "?p=categories"
+REDIRECT /categories/