#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright © 2013 Miguel González <migonzalvar@activitycentral.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>

import argparse

import xs_authserver


def runserver(debug=False):
    xs_authserver.app.run(host='0.0.0.0', port=5000, debug=debug)


def init_db():
    xs_authserver.init_db()


if __name__ == '__main__':
    description = "xs-authserver command utility"
    epilog = (
     "xs-authserver uses a config file specified by {env} environment "
     "variable. If {env} is not defined or the filename doesn't exist "
     "it will use default values."
        ).format(env=xs_authserver._ENV_VAR)
    parser = argparse.ArgumentParser(description=description, epilog=epilog)

    parser.add_argument('command', choices=['runserver', 'initdb'])

    parser.add_argument('--debug', dest='debug', action='store_true')
    parser.add_argument('--no-debug', dest='debug', action='store_false')
    parser.set_defaults(debug=False)

    args = parser.parse_args()

    if args.command == 'runserver':
        runserver(debug=args.debug)
    elif args.command == 'initdb':
        init_db()
