#!/bin/bash

# pipeScreenfetch

# call from .config/openbox/menu.xml like
#        <menu id="screenfetch" label="screenfetch" execute="pipeScreenfetch" />

# header
header () {
cat << HEAD
<openbox_pipe_menu>
HEAD
}

# footer
footer () {
cat << FOOT
</openbox_pipe_menu>
FOOT
}

xmlescape () {
    echo "$@" | sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g; s/^[ \t]*//;s/[ \t]*$//'
}

# checks critical
command -v screenfetch >/dev/null 2>&1 || { header; echo "<item label=\"Horrible error, I need screenfetch installed.\" />"; footer ; exit 1; }

header
# items loop over lines
screenfetch-dev -nNE -d "-host;-shell" | while read -r line 
do 

    #line=$(xmlescape "$line")
    #sed replacement https://github.com/koalaman/shellcheck/wiki/SC2001
    line="${line//_/__}"
    echo "<item label=\"$line\">
        <action name=\"Execute\">
            <execute>
                bash -c \"echo '$line' | /usr/bin/xsel --clipboard\"
            </execute>
        </action>
    </item>"
    

done

footer