#!/usr/bin/env python
"""
Start or stop moin twisted server

Usage:
    mointwisted start|stop|kill

When you start the server, it creates the file 'mointwisted.pid', which
is used later to stop the process. Do not delete this file.

@copyright: 2004-2005 Thomas Waldmann, Nir Soffer
@license: GNU GPL, see COPYING for details.
"""

import sys, os

commands = {
    'start': 'twistd --python mointwisted.py --pidfile mointwisted.pid',
    'stop': 'kill `cat mointwisted.pid`',
    'kill': 'kill -9 `cat mointwisted.pid`',
}

def exit(msg):
    print 'Error:', msg
    print __doc__
    sys.exit(1)    

try:
    command = commands[sys.argv[1]]
    os.system(command)
except KeyError, why:
    exit('unkown command: %s' % why)
except IndexError, why:
    exit('missing argument')
except OSError, why:
    exit('could not %s server: %s' % (sys.argv[1], why)) 
 
