#!/bin/bash
# pipeMisc
# from a txt file named ~/bin/misc, that may look like:
# geany
# blender 2.9,~/apps/blender-2.79-testbuild1-linux-glibc219-x86_64/blender
# ---
# inkscape
# generate a pipe menu for openbox that could be called from menu.xml like
#
#
#
# var
database="$HOME/bin/misc"
outputMenu="$HOME/bin/misc.xml"
# xmlescape and trim whitespace bash
# https://stackoverflow.com/questions/12873682/short-way-to-escape-html-in-bash
xmlescape () {
echo "$@" | sed 's/&/\&/g; s/\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g; s/^[ \t]*//;s/[ \t]*$//'
}
# trim whitespace at start/end function (This is now done in that sed sentence ^)
# https://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-a-bash-variable
#trim() {
#local var="$*"
## remove leading whitespace characters
#var="${var#"${var%%[![:space:]]*}"}"
## remove trailing whitespace characters
#var="${var%"${var##*[![:space:]]}"}"
#echo -n "$var"
#}
# MAIN
action() {
# header
cat << EOFMENU
EOFMENU
file="$database"
if [ -f "$file" ]; then # start if database
# main
while read -r line; do
IFS=, read -r label action <<< "$line"
# skip if label is empty or 1st char is #
if [ -z "$label" ] || [ "${label:0:1}" == "#" ]; then
continue
fi
# if second field is empty, lets equal it to first
[ -z "$action" ] && { action="$label"; }
# remove white space at begining/end and xmlescape both
#label=$(xmlescape $(trim "$label"))
#action=$(xmlescape $(trim "$action"))
label=$(xmlescape "$label")
action=$(xmlescape "$action")
if [[ "$label" != "---" ]]; then # if label="---" then echo separator
# body
cat << EOFMENU
-
$action
EOFMENU
else
cat << EOFMENU
EOFMENU
fi
done < "$database" # < This is where your database is
fi # end if database
# footer
cat << EOFMENU
EOFMENU
}
action > "$outputMenu"