#-----------------------------------------------------------------------
# File:		fns-built-trayapplist
# Version:	1.0.2
# Licence: 	GPL 2
#
# Description:	shell script to generate a list of actual available
#		applets in systray (stalonetray)
#
# Parameters:	fns-built-trayapplist <stalonetray_logfile> <applist_file>
#
# Remarks:	stalonetray must started with --log-level info and
#		output redirected with 2>stalonetray_logfile
#
# Author:	Thomas Funk <t.funk@web.de>	
# Created:	08/13/2012
# Changed:	10/23/2012
#-----------------------------------------------------------------------

#!/bin/bash

version=1.0.2

if [ "$1" = "-h" ] || [ "$1" = "--help" ]
then
	echo "------------------------------------------------------------------------"
	echo "Usage: `basename $0` <logfile> <appfile>"
	echo "       <logfile>       path to stalonetray logfile"
	echo "       <appfile>       path to app list file"
	echo "       -h|--help       short help"
	echo "       -v|--version    version"
	echo
	echo "Example:"
	echo "`basename $0` ~/systray.log $FVWM_USERDIR/.applist"
	echo "------------------------------------------------------------------------"
	exit 0
elif [ "$1" = "-v" ] || [ "$1" = "--version" ]
then
	echo `basename $0` V $version
	exit 0
fi

if [ $# -lt 2 ]
then
	echo Not enough parameter! Exiting.
	exit 1
elif  [ $# -gt 2 ]
then
	echo Too much parameter! Exiting.
	exit 1
fi

infile=$1
outfile=$2

# first delete outfile
if [ -f $outfile ]
then
    rm -f $outfile
fi

# get windows ids of last available applets
windows_ids=`sed -n '/tray status/h;/tray status/!H;$!b;x;p' $infile |sed -n '/tray status/,/-----------------------------------/p' | grep wid |cut -d" " -f3`

for id in $windows_ids
do
    #echo id: $id
    # get PID of the applet
    PID=`xprop -id $id _NET_WM_PID |cut -d" " -f3`
    #echo PID: $PID
    # get command string
    cmd=`ps -p $PID -o cmd=`
    #echo cmd: $cmd
    # put cmd in outfile
    echo $cmd >> $outfile
done

exit 0
