#!/usr/bin/python3
# -*- coding: utf-8 -*-

# ------------------------------------------------------------------------------
# 
# Author: Gabriele Girelli
# Email: gigi.ga90@gmail.com
# Version: 0.0.1
# Description: Web-Server
# 
# TODO:
# 	- Fix behavior when run from different working directory. (and in sections)
# 
# ------------------------------------------------------------------------------


# DEPENDENCIES =================================================================

import argparse
import bottle as bot
import os
from os.path import dirname
import paste

import fish_prode as fp
import fish_prode.sections as sec

# INPUT ========================================================================

# Add script description
parser = argparse.ArgumentParser(description = 'Run WebServer.')

# Add params
parser.add_argument('-u', '--url', metavar = 'url', type = str,
	default = '0.0.0.0', help = 'Web Server URL. Default: 0.0.0.0')
parser.add_argument('-p', '--port', metavar = 'port', type = int,
	default = 8080, help = 'Web Server port. Default: 8080')
parser.add_argument('-s', '--static', metavar = 'port', type = str,
	default = "%s/static/" % dirname(dirname(fp.__file__)),
	help = 'Web Server static folder (created if not found). Default: "%s"' % (
		"%s/static/" % dirname(dirname(fp.__file__))))

# Parse arguments
args = parser.parse_args()

# INIT =========================================================================

# Server params
root_path = "%s/" % dirname(fp.__file__)
ruri_complete = 'http://%s:%d/' % (args.url, args.port)
section_path = "%s/" % dirname(sec.__file__)

# App title
title = 'FISH ProDe'

# Add views folder to TEMPLATE_PATH
bot.TEMPLATE_PATH.append(root_path)
bot.TEMPLATE_PATH.append('%sviews/' % root_path)

# Prepare static folder
assert_msg = "folder expected, file found: %s" % args.static
assert not os.path.isfile(args.static), assert_msg
if not os.path.isdir(args.static):
	os.mkdir(args.static)
if not os.path.isdir("%s/db" % args.static):
	os.mkdir("%s/db" % args.static)
if not os.path.isdir("%s/query" % args.static):
	os.mkdir("%s/query" % args.static)

# Start root app
root = bot.Bottle()

# ROUTES =======================================================================

# Home
@root.route('/')
@bot.view('home')
def index():
	d = {}
	d['custom_stylesheets'] = ['home.css']
	d['title'] = title
	d['description'] = title
	return d

# 404 Error
@root.error(404)
def error404(error):
	return 'Nothing here, sorry :('

# Static files -----------------------------------------------------------------

# CSS files
@root.route('/css/<path>')
def callback(path):
    return bot.static_file(path, '%s/css/' % root_path)

# JS files
@root.route('/js/<path>')
def callback(path):
    return bot.static_file(path, '%s/js/' % root_path)

# Fonts files
@root.route('/fonts/<path>')
def callback(path):
    return bot.static_file(path, '%s/fonts/' % root_path)

# Images
@root.route('/images/<path>')
def callback(path):
    return bot.static_file(path, '%s/images/' % root_path)

# Documents
@root.route('/documents/<path>')
def callback(path):
    return bot.static_file(path, '%s/documents/' % root_path)

# Documentation ----------------------------------------------------------------

@root.route('/docs/<path:re:.*>')
def go_docs(path):
	if 0 == len(path): path = 'index.html'
	return bot.static_file(path, '%sdocs/_build/html/' % root_path)

# Sections ---------------------------------------------------------------------

# Probe designer
root.mount('probe-design/',
	sec.probe_design.App(section_path, args.static,
		root_path, ruri_complete, 'probe-design/'))

# RUN ==========================================================================

root.run(host = args.url, port = args.port, debug = True, server = 'paste')

# END ==========================================================================

################################################################################
