#!/usr/bin/python

import os
import subprocess
import signal

from launcher import Launcher

launcher = Launcher()

try:
    # Kill gnome-panel in a way that it won't autorespawn
    pidof = subprocess.Popen(["/sbin/pidof", "gnome-panel"], stdout=subprocess.PIPE)
    pids = pidof.communicate()[0].split()
    pidof.wait()
    devnull = open("/dev/null", "w")
    for pid in pids:
        subprocess.call(["gdb", "-batch-silent",
                         "-ex", "call panel_session_do_not_restart()",
                         "-ex", "call exit()",
                         "-p", pid], stdout=devnull, stderr=devnull)
    devnull.close()

    shell = launcher.start_shell()
    
    # Wait for shell to exit
    try:
        shell.wait()
    except KeyboardInterrupt, e:
        os.kill(shell.pid, signal.SIGKILL)
        shell.wait()
finally:
    # Restart gnome-panel and window manager

    subprocess.Popen(["/usr/bin/metacity"])
    subprocess.Popen(["/usr/bin/gnome-panel"])