#!/usr/bin/env python

import sys, os, signal, subprocess, shlex,time


def file_pid(name):
    proc1 = subprocess.Popen(shlex.split('ps aux'), stdout=subprocess.PIPE)
    proc2 = subprocess.Popen(shlex.split('grep ' + name), stdin=proc1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    proc3 = subprocess.Popen(shlex.split('awk \'{print $2}\' '), stdin=proc2.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    proc1.stdout.close()
    proc2.stdout.close()
    out, err = proc3.communicate()

    lines = out.splitlines()
    pids = []
    for line in lines :
        item = line.strip()
        pid = int(item)
        if pid > 0:
            pids.append(pid)

    if len(pids) > 0 :
        return pids
    else:
        return []


def has_proc(name):
    pids = file_pid(name)
    return len(pids) > 0


def restart(item):
    name = item['file_name']
    cmd  = item['cmd']
    if not name:
        print 'file name is empty'
        return

    cur_pid = os.getpid()
    pids = file_pid(name)
    print "start restart " + name
    for pid in pids:
        try:
            if cur_pid != pid:
                os.kill(pid, signal.SIGKILL)
                print 'kill pid=', pid
            else:
                continue
        except OSError, e:
            print "OSError no=", e.errno, " err=", e.strerror
            pass
        except BaseException, be:
            pass

    if cmd:
        print cmd
        time.sleep(1)
        os.system(cmd)
        time.sleep(1)
    return


def mac_sys():
    plat_name = sys.platform
    return (plat_name == 'darwin')


def main():
    print "you can input: fcgi,queue,ugc,center,all to restart server or no input it will restart fcgi,queue"

    if len(sys.argv) == 2:
        option = sys.argv[1]
    else:
        option = ''

    cmds = []
    if mac_sys() :
        if option == 'fcgi':
            cmds.append({'file_name': 'fcgi_run.php','cmd': 'spawn-fcgi -a 127.0.0.1 -p 9100 -F 1 -f "php fcgi_run.php"'})
        elif option == 'queue':
            cmds.append({'file_name': 'crontab.php','cmd': ''})
        elif option == 'ugc' :
            cmds.append({'file_name': 'mac_ugcman', 'cmd': 'sudo ./mac_ugcman'})
            cmds.append({'file_name': 'ugc_srv.php','cmd': 'php ugc_srv.php'})
        elif option == 'center':
            cmds.append({'file_name': 'centra_srv.php','cmd': 'php centra_srv.php'})
        elif option == 'all':
            cmds.append({'file_name': 'fcgi_run.php',   'cmd': 'spawn-fcgi -a 127.0.0.1 -p 9100 -F 1 -f "php fcgi_run.php"'})
            cmds.append({'file_name': 'crontab.php',    'cmd': ''})
            cmds.append({'file_name': 'mac_ugcman',     'cmd': './mac_ugcman'})
            cmds.append({'file_name': 'ugc_srv.php',    'cmd': 'php ugc_srv.php'})
            cmds.append({'file_name': 'centra_srv.php', 'cmd': 'php centra_srv.php'})
        else:
            cmds.append({'file_name': 'fcgi_run.php', 'cmd': 'spawn-fcgi -a 127.0.0.1 -p 9100 -F 1 -f "php fcgi_run.php"'})
            cmds.append({'file_name': 'crontab.php',  'cmd': ''})
    else :
        if option == 'fcgi':
            cmds.append({'file_name': 'fcgi_run.php',   'cmd': 'spawn-fcgi -a 127.0.0.1 -p 9100 -F 10 -f "php fcgi_run.php"'})
        elif option == 'queue':
            cmds.append({'file_name': 'crontab.php',    'cmd': ''})
        elif option == 'ugc':
            cmds.append({'file_name': 'ugcman',         'cmd': './ugcman'})
            cmds.append({'file_name': 'ugc_srv.php',    'cmd': 'php ugc_srv.php'})
        elif option == 'center':
            cmds.append({'file_name': 'centra_srv.php', 'cmd': 'php centra_srv.php'})
        elif option == 'all':
            cmds.append({'file_name': 'fcgi_run.php',   'cmd': 'spawn-fcgi -a 127.0.0.1 -p 9100 -F 10 -f "php fcgi_run.php"'})
            cmds.append({'file_name': 'crontab.php',    'cmd': ''})
            cmds.append({'file_name': 'ugcman',         'cmd': './ugcman'})
            cmds.append({'file_name': 'ugc_srv.php',    'cmd': 'php ugc_srv.php'})
            cmds.append({'file_name': 'centra_srv.php', 'cmd': 'php centra_srv.php'})
        else:
            cmds.append({'file_name': 'fcgi_run.php',   'cmd': 'spawn-fcgi -a 127.0.0.1 -p 9100 -F 10 -f "php fcgi_run.php"'})
            cmds.append({'file_name': 'crontab.php',    'cmd': ''})

    for item in cmds :
        restart(item)
    sys.exit(2)


if __name__ == '__main__':
    main()