# Copyright 2014-2015 Boxkite Inc.

# This file is part of the DataCats package and is released under
# the terms of the GNU Affero General Public License version 3.0.
# See LICENSE.txt or http://www.fsf.org/licensing/licenses/agpl-3.0.html

from ConfigParser import SafeConfigParser
from os import listdir
from os.path import expanduser
import webbrowser
import sys

def write(s):
    sys.stdout.write(s)
    sys.stdout.flush()

def stop(environment, opts):
    """Stop serving environment and remove all its containers

Usage:
  datacats stop [-r] [ENVIRONMENT]

Options:
  -r --remote        Stop DataCats.com cloud instance

ENVIRONMENT may be an environment name or a path to an environment directory.
Default: '.'
"""
    environment.stop_web()
    environment.stop_postgres_and_solr()

def start(environment, opts):
    """Create containers and start serving environment

Usage:
  datacats start [-bp] [-a HOST] [ENVIRONMENT [PORT]]
  datacats start -r [-b] [-a HOST] [ENVIRONMENT]

Options:
  -b --background    Don't wait for response from web server
  -p --production    Start with apache and debug=false
  -r --remote        Start DataCats.com cloud instance
  -a --address=HOST   Host to listen on (Linux-only) [default: 127.0.0.1]

ENVIRONMENT may be an environment name or a path to an environment directory.
Default: '.'
"""
    environment.require_data()
    address = environment.web_address()
    if address is not None:
        print 'Already running at {0}'.format(address)
        return
    reload_(environment, opts)
def reload_(environment, opts):
    """Reload environment source and configuration

Usage:
  datacats reload [-bp] [-a HOST] [ENVIRONMENT [PORT]]
  datacats reload -r [-b] [-a HOST] [ENVIRONMENT]

Options:
  -b --background    Don't wait for response from web server
  -p --production    Reload with apache and debug=false
  -r --remote        Reload DataCats.com cloud instance
  -a --address=HOST   Host to listen on (Linux-only) [default: 127.0.0.1]

ENVIRONMENT may be an environment name or a path to an environment directory.
Default: '.'
"""
    environment.require_data()
    environment.stop_web()
    if opts['PORT']:
        environment.port = int(opts['PORT'])
        environment.save()
    if 'postgres' not in environment.containers_running():
        environment.stop_postgres_and_solr()
        environment.start_postgres_and_solr()

    environment.start_web(opts['--address'], opts['--production'])
    write('Starting web server at {0} ...'.format(environment.web_address()))
    if opts['--background']:
        write('\n')
        return

    try:
        environment.wait_for_web_available()
    finally:
        write('\n')

def info(environment, opts):
    """Display information about environment and running containers

Usage:
  datacats info [-qr] [ENVIRONMENT]

Options:
  -q --quiet         Echo only the web URL or nothing if not running
  -r --remote        Information about DataCats.com cloud instance

ENVIRONMENT may be an environment name or a path to an environment directory.
Default: '.'
"""
    addr = environment.web_address()
    if opts['--quiet']:
        if addr:
            print addr
        return

    datadir = environment.datadir
    if not environment.data_exists():
        datadir = ''
    elif not environment.data_complete():
        datadir += ' (damaged)'

    print 'Environment name: ' + environment.name
    print '    Default port: ' + str(environment.port)
    print ' Environment dir: ' + environment.target
    print '        Data dir: ' + datadir
    print '      Containers: ' + ' '.join(environment.containers_running())
    if not addr:
        return
    print '    Available at: ' + addr

def list_(opts):
    """List all environments for this user

Usage:
  datacats list
"""
    for p in sorted(listdir(expanduser('~/.datacats'))):
        if p == 'user-profile':
            continue
        print p

def logs(environment, opts):
    """Display or follow container logs

Usage:
  datacats logs [-d | -s] [-tr] [--tail=LINES] [ENVIRONMENT]
  datacats logs -f [-d | -s] [-r] [ENVIRONMENT]

Options:
  -d --postgres-logs Show postgres database logs instead of web logs
  -f --follow        Follow logs instead of exiting immediately
  -r --remote        Retrieve logs from DataCats.com cloud instance
  -s --solr-logs     Show solr search logs instead of web logs
  -t --timestamps    Add timestamps to log lines
  --tail=LINES       Number of lines to show [default: all]

ENVIRONMENT may be an environment name or a path to an environment directory.
Default: '.'
"""
    container = 'web'
    if opts['--solr-logs']:
        container = 'solr'
    if opts['--postgres-logs']:
        container = 'postgres'
    tail = opts['--tail']
    if tail != 'all':
        tail = int(tail)
    l = environment.logs(container, tail, opts['--follow'],
        opts['--timestamps'])
    if not opts['--follow']:
        print l
        return
    try:
        for message in l:
            write(message)
    except KeyboardInterrupt:
        print

def open_(environment, opts):
    """Open web browser window to this environment

Usage:
  datacats open [-r] [ENVIRONMENT]

Options:
  -r --remote        Open DataCats.com cloud instance address

ENVIRONMENT may be an environment name or a path to an environment directory.
Default: '.'
"""
    environment.require_data()
    addr = environment.web_address()
    if not addr:
        print "Site not currently running"
    else:
        webbrowser.open(addr)

@@ -898,36 +898,34 @@
 start %5B-bp%5D %5B-a 
-HOST
+IP
 %5D %5BENVIRONMENT %5B
@@ -952,36 +952,34 @@
 art -r %5B-b%5D %5B-a 
-HOST
+IP
 %5D %5BENVIRONMENT%5D%0A
@@ -1170,35 +1170,37 @@
 a --address=
-HOST   Host
+IP    Address
  to listen o
@@ -1646,28 +1646,26 @@
 d %5B-bp%5D %5B-a 
-HOST
+IP
 %5D %5BENVIRONME
@@ -1705,20 +1705,18 @@
 -b%5D %5B-a 
-HOST
+IP
 %5D %5BENVIR
@@ -1925,19 +1925,21 @@
 ess=
-HOST   Host
+IP    Address
  to 

Change IP->HOST in --address help.