#!/bin/sh

# user configurable stuffs go here

WINDOW_WIDTH=320
WINDOW_HEIGHT=240
CHROME_WIDTH_OFFSET=5
CHROME_HEIGHT_OFFSET=40

# end of user configurable section

# get screen width and height
WIDTH=`xdpyinfo | grep dimensions | awk '{print $2}' | awk -Fx '{print $1}'`
HEIGHT=`xdpyinfo | grep dimensions | awk '{print $2}' | awk -Fx '{print $2}'`
echo "display is $WIDTH x $HEIGHT"

# start at 0,0
X=0
Y=$CHROME_HEIGHT_OFFSET
NUM=0

while read ROOM_LINE; do
  ROOM="`echo $ROOM_LINE | cut -d\| -f 1`"
  NUM=`echo $ROOM_LINE | cut -d\| -f 4`
  HEX="`echo "obase=16; $NUM" | bc | tr '[A-Z]' '[a-z]'`"
  WINTITLE="[$HEX] $ROOM"
  echo "moving $WINTITLE to $X,$Y"
  wmctrl -Fr "$WINTITLE" -e "0,$X,$Y,$WINDOW_WIDTH,$WINDOW_HEIGHT"
  NUM=$((NUM+1))
  X=$((X+WINDOW_WIDTH+CHROME_WIDTH_OFFSET))
  if [ $X -gt $WIDTH ]; then
    echo "new row"
    X=0
    Y=$((Y+WINDOW_HEIGHT+CHROME_HEIGHT_OFFSET))
  fi
done < HOSTS
