#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2014, 2015 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import os
import subprocess
import signal
import yaml

SYNAPSE = ["python", "-B", "-m", "synapse.app.homeserver"]

CONFIGFILE = "homeserver.yaml"

GREEN = "\x1b[1;32m"
RED = "\x1b[1;31m"
NORMAL = "\x1b[m"

if not os.path.exists(CONFIGFILE):
    sys.stderr.write(
        "No config file found\n"
        "To generate a config file, run '%s -c %s --generate-config"
        " --server-name=<server name>'\n" % (
            " ".join(SYNAPSE), CONFIGFILE
        )
    )
    sys.exit(1)

CONFIG = yaml.load(open(CONFIGFILE))
PIDFILE = CONFIG["pid_file"]


def start():
    print "Starting ...",
    args = SYNAPSE
    args.extend(["--daemonize", "-c", CONFIGFILE])
    try:
        subprocess.check_call(args)
        print GREEN + "started" + NORMAL
    except subprocess.CalledProcessError as e:
        print (
            RED +
            "error starting (exit code: %d); see above for logs" % e.returncode +
            NORMAL
        )


def stop():
    if os.path.exists(PIDFILE):
        pid = int(open(PIDFILE).read())
        os.kill(pid, signal.SIGTERM)
        print GREEN + "stopped" + NORMAL


def main():
    action = sys.argv[1] if sys.argv[1:] else "usage"
    if action == "start":
        start()
    elif action == "stop":
        stop()
    elif action == "restart":
        stop()
        start()
    else:
        sys.stderr.write("Usage: %s [start|stop|restart]\n" % (sys.argv[0],))
        sys.exit(1)


if __name__ == "__main__":
    main()

@@ -645,16 +645,31 @@
 port os%0A
+import os.path%0A
 import s
@@ -769,40 +769,8 @@
 %22%5D%0A%0A
-CONFIGFILE = %22homeserver.yaml%22%0A%0A
 GREE
@@ -828,355 +828,8 @@
 m%22%0A%0A
-if not os.path.exists(CONFIGFILE):%0A    sys.stderr.write(%0A        %22No config file found%5Cn%22%0A        %22To generate a config file, run '%25s -c %25s --generate-config%22%0A        %22 --server-name=%3Cserver name%3E'%5Cn%22 %25 (%0A            %22 %22.join(SYNAPSE), CONFIGFILE%0A        )%0A    )%0A    sys.exit(1)%0A%0ACONFIG = yaml.load(open(CONFIGFILE))%0APIDFILE = CONFIG%5B%22pid_file%22%5D%0A%0A
 %0Adef
@@ -835,16 +835,26 @@
 f start(
+configfile
 ):%0A    p
@@ -935,19 +935,72 @@
 c%22, 
-CONFIGFILE%5D
+configfile%5D)%0A    cwd = os.path.dirname(os.path.abspath(__file__)
 )%0A  
@@ -1040,16 +1040,25 @@
 all(args
+, cwd=cwd
 )%0A      
@@ -1295,16 +1295,23 @@
 ef stop(
+pidfile
 ):%0A    i
@@ -1327,23 +1327,23 @@
 .exists(
-PIDFILE
+pidfile
 ):%0A     
@@ -1364,15 +1364,15 @@
 pen(
-PIDFILE
+pidfile
 ).re
@@ -1469,16 +1469,482 @@
 main():%0A
+    configfile = sys.argv%5B2%5D if len(sys.argv) == 3 else %22homeserver.yaml%22%0A%0A    if not os.path.exists(configfile):%0A        sys.stderr.write(%0A            %22No config file found%5Cn%22%0A            %22To generate a config file, run '%25s -c %25s --generate-config%22%0A            %22 --server-name=%3Cserver name%3E'%5Cn%22 %25 (%0A                %22 %22.join(SYNAPSE), configfile%0A            )%0A        )%0A        sys.exit(1)%0A%0A    config = yaml.load(open(configfile))%0A    pidfile = config%5B%22pid_file%22%5D%0A%0A
     acti
@@ -2021,32 +2021,42 @@
 :%0A        start(
+configfile
 )%0A    elif actio
@@ -2077,24 +2077,31 @@
        stop(
+pidfile
 )%0A    elif a
@@ -2217,16 +2217,29 @@
 restart%5D
+ %5Bconfigfile%5D
 %5Cn%22 %25 (s

Allow config file path to be configurable in in synctl