#!/usr/local/bin/ruby -w
#Preprocessor for gnome-session
#Author: Thomas Adam
#Date: 20/04/1998

arr = Array.new()
arr2 = Array.new()
path=File.expand_path("~/")

#open the files for reading and writing...
myfile = File.open("#{path}/.gnome/session", "r")
myfile2 = File.new("#{path}/.gnome/session2", "w+")

#read anylines with 'xclock' in them, and store in array
arr = myfile.readlines().grep(/xclock/i)

#iterate through the array
arr.each { |content|
  #push into the new array just the numbers from the start of the line
  #removing any duplicates.
  arr2.push( content.to_s().slice(0..1).gsub(',', '') ).uniq()
}

#move the file pointer back to the start
myfile.rewind()

#find the number of clients.
numclient = $1.to_i() if myfile.readlines().to_s().grep(/num_clients=(\d+)/)

#time to start over again...
myfile.rewind()

#read back into the arr array the entire file
arr = myfile.readlines()

#take each element in turn
arr2.each { |c|
  #delete all lines if the numbers match
  arr.delete_if{ |d|
    c.to_s() == $1.to_s() if d.grep(/^(\d+)/)
  }
}

#close the file
myfile.close()

#print into the temporary file, the data
myfile2.print(arr)

#close it
myfile2.close()

#use the underlying shell (bash?) to copy the files back
#NOT using fstools for a good reason -- any comments about security issues
#will be ignored.
system("cp -f #{path}/.gnome/session #{path}/.gnome/session.orig")
system("mv -f #{path}/.gnome/session2 #{path}/.gnome/session")

exit 0
