#!/bin/ash
# ------------------------------------------------------------
# Version: 	0.02
# Released:	2/26/02
# Author: 	Jamin W. Collins (jcollins@asgardsrealm.net)
# Web Site: 	http://www.asgardsrealm.net/linux/
# E-Mail:	jcollins@asgardsrealm.net
# ------------------------------------------------------------
# 
# This is a first take at a script to capture window position
# information to be used in conjuction with a launcher
# application (perhaps bblaunch) to provide a simple means
# for starting applications up in the same location.
#
# Uses the following programs to gather the needed information:
#	xprop	xwininfo   xlsclients
#
# Uses the following programs for data manipulation:
#	grep   sed   cut
#
# Uses the following programs for file manipulations:
#	tempfile   mv
#
# ------------------------------------------------------------
#
# Copyright (C) 2002 Jamin W. Collins
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# ------------------------------------------------------------
# History
#
# 2/26/02 - v0.02
#	- adjust script to run under ash (per user feedback)
#	- removed extranious IFS variable setting
#	- removed shell header comment from created bbsession file
#	- added check for temp file creation programs
#
# 2/23/02 - v0.01
# 	- Initial release 
#
# ------------------------------------------------------------

if [ -f /bin/tempfile ]; then
	TEMPFILE=`/bin/tempfile`
elif [ -f /bin/mktemp ]; then
	TEMPFILE=`/bin/mktemp /tmp/$0.XXXXXX`
fi

# get hex ids for all windows
WINDOW_IDS=`xlsclients -l | grep Window | sed s/"Window "// | sed s/://`

# loop through each entry in WINDOW_IDS
for WINDOW_ID in $WINDOW_IDS; do
	
	# get the application command line (with arguments)
	COMMAND_LINE=`xlsclients -l | grep $WINDOW_ID -A 5 | grep Command | sed s/"  Command:  "//`

	# grab just the executable, not the command line arguments
	APP_NAME=`echo $COMMAND_LINE| cut -f1 -d ' '`
	
	# Get application's geometry
	APP_GEOM=`xwininfo -id $WINDOW_ID | grep 'geometry'`

	# Grab list of BB attributes for later use
	BB_ATTR=`xprop -id $WINDOW_ID | grep _BLACKBOX_ATTRIBUTES`

	# get workspace that application is running on (3rd BB attribute?)
	WORKSPACE=`echo $BB_ATTR | cut -f 3 -d ',' | sed s/" 0x"//`
	
	# display retrieved information
	echo "WINDOW_ID = $WINDOW_ID"
	echo "Command line = $COMMAND_LINE"
	echo "Executable = $APP_NAME"
	echo "Blackbox Attributes= $BB_ATTR"
	echo "Workspace = '$WORKSPACE'"
	echo "Application Geometry = $APP_GEOM"
	echo ""
	
	# Check to see if we were able to retrieve a valid workspace
	if [ -n "$WORKSPACE" ]; then
		# add entry to bbsession file
		echo "bblaunch -w $(($WORKSPACE+1)) $COMMAND_LINE $APP_GEOM" >> $TEMPFILE
	fi
 
done

mv $TEMPFILE ~/.bbsession
