#!/bin/sh
#=================================================================
# PURPOSE:  Provide info on menuitems of JWM root menu, optionally create desktop shortcuts 
# CREATED:  2015-09-01
# BY:       MochiMoppel
# FORUM:    http://www.murga-linux.com/puppy/viewtopic.php?p=862495#862495
#=================================================================

export WMCONFIG="/root/.jwmrc"
####################################
## FUNCTION
#####################################
iteminfo () {                                             # <label> <iconname> [exec] <prog basename> <arguments>
label=$1 ;shift
icon=$1  ;shift
[ "$1" = "exec" ] && shift
prog=$1  ;shift
args=$@
path=$(which $prog)
real=$(realpath "$path" 2>/dev/null)
[ "$real" ] || path="$path"                               # just in case realpath doesn't produce result
idir=$(sed -nr 's/^<IconPath>([^<]*).*$/\1/p' $WMCONFIG)  # read JWM's IconPaths
ipth=$(find $idir -maxdepth 1 -name $icon)                # find full icon path
ipth=${ipth%%$'\n'*}                                      # Take only 1st of (unlikely) multiple filespecs found
[ "$ipth" ] && icon=$ipth

gxmessage -name "JWM Menu Info" -nearmouse -fn "monospace" -buttons "Run":2,"Show location":3,"Make shortcut":4,Close: -default Close "
  Label:     $label
  Icon:      $icon
  Executable:$real
  Arguments: $args
  Cmd line:  $real $args"
case $? in
2)	$path $args & ;;
3)	rox -s "$path";;
4)	rox --RPC << XYZ
<?xml version="1.0"?> 
	<env:Envelope xmlns:env="http://www.w3.org/2001/12/soap-envelope"> 
	<env:Body xmlns="http://rox.sourceforge.net/SOAP/ROX-Filer"> 
	<PinboardAdd> 
		<Label>$label</Label> 
		<Path>$real</Path> 
		<Args>$args</Args>
	</PinboardAdd> 
	<SetIcon> 
		<Path>$path</Path> 
		<Icon>$icon</Icon>
	</SetIcon> 
	</env:Body> 
	</env:Envelope>
XYZ
;;
esac
} ; export -f iteminfo
#####################################
## CONVERT JWM MENU TO GTKDIALOG MENU
#####################################
t=$(< $WMCONFIG)
t=${t#*<RootMenu*>}
t=${t%%</RootMenu>*}
P1='s|Program|menuitem|g'
P2='s|Menu|menu|g'
P3='s|<Separator/>|<menuitemseparator></menuitemseparator>|'
P4='s|<!--.*-->||'
P5='s|(^.*<menuitem [^>]*>)([^>]*)(<.*$)|\1<action>iteminfo \2</action>\3|'
P6='s|(<Restart)([^/].*)(/.*$)|<menuitem\2><action>iteminfo jwm -restart</action></menuitem>|'
P7='s|(^.*label=)("[^"]*")(.*icon=)("[^"]*")(.*iteminfo )(.*$)|\1\2\3\4\5\2 \4 \6|'
P8='s|icon=\"[^"]*\"||'
t=$(sed -r "$P1;$P2;$P3;$P4;$P5;$P6;$P7;$P8" <<< "$t")
#####################################
## CREATE GTKDIALOG
#####################################
head='<window title="MI"><menubar><menu label="JWM Menu Info">'
tail='</menu></menubar></window>'
echo -n $head$t$tail | gtkdialog -s