#!/bin/bash
# toolbar-manager script to be called by app-select.conf to adds the selected App to the end of the IceWM toolbar
# BobC 12/06/2020 - credit to PPC for much of the code and thanks to Dave for app-select and adding the custom right click option feature to app-select that drives it

# This script should be located in /usr/local/bin
# a link to call it should be in /usr/lib/app-select/plugins
# it needs to be added to /usr/share/app-select/app-select.conf which will get copied to ~/.config/app-select.conf with the following line:
#Add to toolbar|toolbar-manager "%n" "%e" "%i" "%t"

# %n = name of item
# %e = executable
# %c = categories
# %f = filename
# %p = filepath (name included)
# %t = terminal (true/false)
# %i = icon
#

NOME=$1
ICONE=$3
TERME=$4

#use exec from app-select
EXEC0=$2

echo "toolbar-manager parms: 1:$1 2:$2 3:$3 4:$4 xxxxx"
#in case EXEC has more than one line, use only the first
readarray -t lines < <(echo "$EXEC0")
EXECperc="${lines[0]}"
# See if terminal should be used, check for true regardless of case
if fgrep -qix -- "$TERME" <<<"true"; then
	EXECperc="x-terminal-emulator -e $EXECperc"
fi
#add line to toolbar - the | cut -f1 -d"%"   part removes any %x option from the exec command.
echo "prog "\"${NOME}"\" "${ICONE}" "${EXECperc}""| cut -f1 -d"%"  >> ~/.icewm/toolbar
#instantly restart IceWm so the new icon appears
icewm --restart
