From 48b6390c54588ea9721602e1e848916696914e4b Mon Sep 17 00:00:00 2001
From: =?utf8?q?Paul=20H=C3=A4nsch?= <paul@plutz.net>
Date: Sun, 23 Oct 2022 19:38:05 +0200
Subject: [PATCH] added flip option for tagging

---
 multitag.sh | 35 ++++++++++++++++++++++++++---------
 style.css   |  2 +-
 widgets.sh  |  4 ++--
 3 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/multitag.sh b/multitag.sh
index 12117cd..c66ff14 100755
--- a/multitag.sh
+++ b/multitag.sh
@@ -4,42 +4,59 @@
 . "$_EXEC/cgilite/session.sh" nocookie
 . "$_EXEC/widgets.sh"
 
+# newtags=''  # tags selected in ui
+# tags=''     # taglist from database
+# detag=''    # temporary remove list
+# extags=''   # exclusive tags, tags that preclude others within their categorie
+# group=''    # id for grouping videos (videos in a group get a common group id)
+# op=''       # operation: add | del | flip
+
 newtags=''
 for tn in $(seq 1 $(POST_COUNT tag)); do
   newtags="$(POST tag $tn)${BR}${newtags}"
 done
-newtags="$(POST newtagn |tr -d '\r' |tr , '\n')${BR}${newtags}"
+newtags="$(POST newtag |tr -d '\r' |tr , '\n')${BR}${newtags}"
 
 if [ "$(POST makegroup)" = true ]; then
   group="$(timeid)"
 fi
 
+# strip leading and trailing line breaks
 while [ "${newtags#${BR}}" != "${newtags}" ]; do newtags="${newtags#${BR}}"; done
 while [ "${newtags%${BR}}" != "${newtags}" ]; do newtags="${newtags%${BR}}"; done
 
-[ "$(POST op)" = del ] && deltags="$newtags"
+op="$(POST op)"
 
 for select in $(seq 1 $(POST_COUNT select)); do
   file="$(POST select $select)"
-
   read_meta "${file%.*}" || continue
+
   tags="$(printf %s\\n "$META_TAGS" |tr , \\n)"
 
-  if [ ! "$deltags" ]; then
+  if [ "$op" = add ]; then
     extags="$(printf '%s' "${newtags}" |grep -e "^-" |cut -d: -f1 )"
     [ "$extags" ] && tags="$(printf %s\\n "$tags" |grep -vwFe "$extags")"
     if printf %s "${newtags}" |grep -e "^-" |grep -qEe "^-[^:]+$"; then
       tags="$(printf %s\\n "$tags" |grep -vEe '^-[^:]+$')"
     fi
-    tags="$(printf '%s\n' "${tags},${newtags}" \
-            | tr , '\n' |sort -u |tr '\n' ,)"
-  else
-    detag="${deltags}${BR}"; while [ "$detag" ]; do
+    tags="${tags}${BR}${newtags}"
+
+  elif [ "$op" = del ]; then
+    detag="${newtags}${BR}"; while [ "$detag" ]; do
       tags="$(printf '%s\n' "$tags" |grep -vxFe "${detag%%${BR}*}")"
       detag="${detag#*${BR}}"
     done
-    tags="$(printf '%s\n' "$tags" |sort -u |tr '\n' ,)"
+
+  elif [ "$op" = flip ]; then
+    fliptag="${newtags}${BR}"; while [ "$fliptag" ]; do
+      comp="$tags"
+      tags="$(printf '%s\n' "$tags" |grep -vxFe "${fliptag%%${BR}*}")"
+      [ "$comp" = "$tags" ] && tags="${tags}${BR}${fliptag%%${BR}*}"
+      fliptag="${fliptag#*${BR}}"
+    done
+
   fi
+  tags="$(printf '%s\n' "$tags" |sort -u |tr '\n' ,)"
   tags="${tags#,}"; tags="${tags%,}"
 
   if [ "$group" ]; then
diff --git a/style.css b/style.css
index 7f6f2bb..17b88e4 100644
--- a/style.css
+++ b/style.css
@@ -255,7 +255,7 @@ body {
   display: block;
   white-space: pre;
 }
-#multitag fieldset [name=newtagn] + label {
+#multitag fieldset textarea[name=newtag] + label {
   display: block;
 }
 
diff --git a/widgets.sh b/widgets.sh
index b80f56b..cd9cf3b 100755
--- a/widgets.sh
+++ b/widgets.sh
@@ -264,9 +264,9 @@ w_tagging(){
   [ "$w_tags" ] && printf ']]'
 
   cat <<-EOF
-	  [fieldset [legend New:][textarea name=newtag\n]
+	  [fieldset [legend New:][textarea name=newtag]
 	  [label [checkbox "makegroup" "true"] Join selected into group]
-	  [submit "op" "del" Remove Tags][submit "op" "add" Add Tags]
+	  [submit "op" "del" Remove][submit "op" "add" Add][submit "op" "flip" Flip]
 	]]
 	EOF
 }
-- 
2.39.5