#!/bin/bash

# pipeNeofetch (similar to pipeScreenfetch)

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

# License=YYCPL0
# Yes You Can Permissive License 0

# 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 neofetch >/dev/null 2>&1 || { header; echo "<item label=\"Horrible error, I need neofetch installed.\" />"; footer ; exit 1; }

header
# items loop over lines
neofetch --off --stdout --disable title term model --memory_percent on | while read -r line
do 

    #line=$(xmlescape "$line")
    #sed replacement https://github.com/koalaman/shellcheck/wiki/SC2001
    if (( ${#line} > 2 )); then
        line="${line//_/.}" # Dot . instead of __ should make this 'jgmenu ob' compatible
        echo "<item label=\"$line\">
            <action name=\"Execute\">
                <execute>
                    bash -c \"echo '$line' | /usr/bin/xsel --clipboard\"
                </execute>
            </action>
        </item>"
    fi

done

# debug
#parent="$(ps $PPID | tail -n 1 | awk "{print \$5}")"
#echo "<item label=\"$parent\" />"

footer

# x-terminal-emulator -e sh -c \"echo '$line' | /usr/bin/xclip -selection clipboard; sleep 5\" # < this is not working without sleep, woot??