#!/usr/bin/env python
import os,sys,subprocess,shutil

import pyonlinedesktop
from pyonlinedesktop.fsutil import iterdir_d

os.environ['OD_SESSION'] = "1"

# Configure GConf, in a very hackish way
subprocess.call(['gconftool-2', '--shutdown'])
os.environ['ONLINE_DESKTOP_GCONF'] = '/usr/share/online-desktop/gconf.path'

reset_autostart_data = '''
[Desktop Entry]
Name=od-reset
Encoding=UTF-8
Version=1.0
Exec=od-reset
X-GNOME-Autostart-enabled=true
Type=Application
'''

autostart_dir_path = os.path.expanduser('~/.config/autostart')
reset_autostart_path = os.path.join(autostart_dir_path, 'od-reset.desktop')
if not os.access(reset_autostart_path, os.R_OK):
	try:
		os.makedirs(autostart_dir_path)
	except:
		pass
	autostart_file = open(reset_autostart_path, 'w')
	autostart_file.write(reset_autostart_data)
	autostart_file.close()

# do pre-configuration
for fpath in iterdir_d('online-desktop/presession'):
  if os.access(fpath, os.X_OK):
    subprocess.call([fpath], stdout=sys.stdout, stderr=sys.stderr)

os.execvp('gnome-session', ['gnome-session'])
sys.exit(1)
