#!/usr/bin/env python

import logging
import sh

import formic
from livereload import Server

logger = logging.getLogger("livereload")


def reload_app():
    logger.info("app restarting...")
    try:
        sh.stop("hackathon-app")
    except:
        pass

    try:
        sh.start("hackathon-app")
    except:
        pass
    logger.info("app restarted...")


server = Server()

for filepath in formic.FileSet(directory="/vagrant",
                               include=["*.java", "*.go", "*.py", "*.yml",
                                        "Makefile", "*.xml", "*.json", "*.sh"],
                               exclude=["*log"]):
    server.watch(filepath, reload_app)

# trigger reload on start
reload_app()

server.serve(root='/vagrant')
