#!/bin/bash
# Extract information for instances of the X2 editor.
#
# NB: X2 programs may be restored to the wrong windows/positions if the
#     order of ps output is different from the order of xwininfo output.
#
# Content of ~/activeXapps.info:
# .---------------------------------- Instance
# . .-------------------------------- Desktop
# | |   .---------------------------- Width
# | |   |   .------------------------ Height
# | |   |   |   .-------------------- Left
# | |   |   |   |   .---------------- Top
# | |   |   |   |   | .-------------- Path to program
# | |   |   |   |   | |           .-- Arguments for program
# V V   V   V   V   V V           V
# 0 2 484 559  58   0 path/to/bin switches, path/to/file, etc.
# 1 4 100 100 100 100 path/to/bin switches, path/to/file, etc.
# 2 3  75 200  75   0 path/to/bin switches, path/to/file, etc.

# Clean up in case restoreXapps failed.
  if [ -f ~/activeXapps.info ]; then
    rm ~/activeXapps.info
  fi

# Begin counting instances.
  instance=0

# Collect information about X2 instances:
# Get the full name of the program.
  X2Bin=$(which xx)

  for ID in $(/usr/bin/xwininfo -root -all \
            | /usr/bin/grep 'X2 Editor Version 2' \
            | /usr/bin/tr --squeeze-repeats ' ' \
            | /usr/bin/cut --delimiter=' ' --field=2)
    do
#     Get the desktop number:
      desktop[$instance]=$(/usr/bin/xprop -id $ID \
                         | /usr/bin/egrep '^_NET_WM_DESKTOP' \
                         | /usr/bin/tr  --squeeze-repeats ' ' \
                         | /usr/bin/cut --delimiter=' ' --field=3)

#     Get the window size and position:
      geometry[$instance]=$(/usr/bin/xwininfo -id $ID \
                          | /usr/bin/egrep '^  -geometry' \
                          | /usr/bin/tr  --squeeze-repeats ' ' \
                          | /usr/bin/tr 'x+' '  ' \
                          | /usr/bin/cut --delimiter=' ' --fields=3-)

#     Get the program arguments (can't get them from xwininfo):
      argList[$instance]=$(/usr/bin/ps aux \
                         | /usr/bin/grep xx \
                         | /usr/bin/grep --invert grep \
                         | /usr/bin/tail --lines=1 \
                         | /usr/bin/tr   --squeeze-repeats ' ' \
              | /usr/bin/cut  --delimiter=' ' --fields=12-)

#     Write them out:
      echo "$instance $desktop[$instance] $geometry[$instance] $X2Bin $argList[$instance]"

#     Count this instance:
      (( instance += 1 ))
    done >> ~/activeXapps.info

# Collect information about xthe instances:
# Get the full name of the program.
  xtheBin=$(which xthe)

  for ID in $(/usr/bin/xwininfo -root -all \
            | /usr/bin/egrep 'THE 3.3' \
            | /usr/bin/tr --squeeze-repeats ' ' \
            | /usr/bin/cut --delimiter=' ' --field=2)
    do
#     Get the desktop number:
      desktop[$instance]=$(/usr/bin/xprop -id $ID \
                         | /usr/bin/egrep '^_NET_WM_DESKTOP' \
                         | /usr/bin/tr --squeeze-repeats ' ' \
                         | /usr/bin/cut --delimiter=' ' --field=3)

#     Get the window size and position:
      geometry[$instance]=$(/usr/bin/xwininfo -id $ID \
                          | /usr/bin/egrep '^  -geometry' \
                          | /usr/bin/tr --squeeze-repeats ' ' \
                          | /usr/bin/tr 'x+' '  ' \
                          | /usr/bin/cut --delimiter=' ' --fields=3-)

#     Get the program arguments:
      argList[$instance]=$(/usr/bin/xwininfo -id $ID \
                         | /usr/bin/egrep '^xwininfo: ' \
                         | /usr/bin/cut --delimiter=' ' --field=8-)

#     Write them out:
      echo "$instance $desktop[$instance] $geometry[$instance] $xtheBin $argList[$instance]"

#     Count this instance:
      (( instance += 1 ))
    done >> ~/activeXapps.info
exit
