#!/usr/bin/env python
import os
import sys
import logging

from threading import Thread
from time import sleep
from shoal_server import config
config.setup()
from shoal_server.shoal import ThreadMonitor, WebpyServer

def main():

    shoal_list = {}
    threads = []

    monitor_thread = ThreadMonitor(shoal_list)
    monitor_thread.daemon = True
    threads.append(monitor_thread)

    webpy_thread = WebpyServer(shoal_list)
    webpy_thread.daemon = True
    threads.append(webpy_thread)

    for thread in threads:
        print "starting", thread
        thread.start()

    try:
        while True:
            for thread in threads:
                if not thread.is_alive():
                    logging.error('{0} died.'.format(thread))
                    sys.exit()
            sleep(1)
    except KeyboardInterrupt:
        sys.exit()

if __name__ == '__main__':
    shoal_dir = config.shoal_dir
    log_file = config.log_file
    log_format = '%(asctime)s - %(levelname)s - [%(filename)s:%(lineno)s] - %(message)s'

    try:
        logging.basicConfig(level=logging.ERROR, format=log_format, filename=log_file)
    except IOError as e:
        print "Could not set logger.", e
        sys.exit(1)

    # change working directory so webpy static files load correctly.
    try:
        os.chdir(shoal_dir)
    except OSError as e:
        print "{0} doesn't seem to exist. Please set `shoal_dir` in shoal-server config file to the location of the shoal-server static files.".format(DIRECTORY)
        sys.exit(1)
    main()
