#!/usr/bin/env python
import atexit
import random
import time

from spyderlib.baseconfig import get_conf_path
from spyderlib.config import CONF
from spyderlib.utils.external import lockfile

if CONF.get('main', 'single_instance'):
    # Minimal delay (0.1-0.2 secs) to avoid that several
    # instances started at the same time step in their
    # own foots while trying to create the lock file
    time.sleep(random.randrange(1000, 2000, 90)/10000.)
    
    # Lock file creation
    lockf = get_conf_path('spyder.lock')
    lock = lockfile.FilesystemLock(lockf)
    
    # lock.lock() tries to lock spyder.lock. If it fails,
    # it returns False and so we try to start the client
    if not lock.lock():
        from spyderlib import client
        client.main()
    else:
        atexit.register(lock.unlock)
        from spyderlib import spyder
        spyder.main()
else:
    from spyderlib import spyder
    spyder.main()
