#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#
#    LinOTP - the open source solution for two factor authentication
#    Copyright (C) 2010 - 2019 KeyIdentity GmbH
#
#    This file is part of LinOTP server.
#
#    This program is free software: you can redistribute it and/or
#    modify it under the terms of the GNU Affero General Public
#    License, version 3, as published by the Free Software Foundation.
#
#    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 Affero General Public License for more details.
#
#    You should have received a copy of the
#               GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
#    E-mail: linotp@keyidentity.com
#    Contact: www.linotp.org
#    Support: www.keyidentity.com
#

VERSION = '0.1'
import os, sys
from getopt import getopt, GetoptError
import ConfigParser
from subprocess import call
from getpass import getpass
import socket

def usage():
    print  '''
    Parameter:
    -f <linotp.ini file>
    -h : help
    '''


def create_database(file):
    sql_dict = {}

    config_file = os.path.basename(file)
    config_path = os.path.abspath(os.path.dirname(file))
    config = ConfigParser.ConfigParser()
    config.read(file)
    config.set("DEFAULT", "here", config_path)
    sqlalchemy = config.get('app:main', "sqlalchemy.url")
    sqlaudit = config.get('DEFAULT', "linotpAudit.sql.url")

    if not sqlalchemy:
        print "ERROR: no sqlalchemy.url found!"
        sys.exit(2)
    if not sqlaudit:
        print "WARNING: no linotpAudit.sql.url found!"

    for i in [1, 2]:
        if i == 1:
            print "Creating LinOTP database"
            print "========================"
            sqlurl = sqlalchemy
        elif i == 2:
            print "Creating Audit database"
            print "======================="
            sqlurl = sqlaudit

        sql = sqlurl.split(":", 1)
        sql_dict["scheme"] = sql[0]

        sql = sql[1].split("@")
        (sql_dict["host"], sql_dict["database"]) = sql[1].split("/")
        (sql_dict["username"], sql_dict["userpass"]) = sql[0].strip("/").split(":")

        if sql_dict["scheme"].lower() not in ["mysql"]:
            print "ERROR: At the moment we only support generating mysql!"
            sys.exit(3)

        print sql_dict
        print

        print "Now enter a database user, who is allowed to create databases!"
        root_user = raw_input("username: ")
        root_pass = getpass("password: ")

        command = "mysql -h %s -u %s --password=%s -e \"create database %s\"" % (sql_dict["host"],
                                                                                root_user,
                                                                                root_pass,
                                                                                sql_dict["database"])
        r = call(command, shell=True)
        if r == 0:
            print "database created."

        if sql_dict["host"] == "localhost":
            client_host = "localhost"
        else:
            client_host = socket.gethostname()

        command = "mysql -h %s -u %s --password=%s -e \"grant all privileges on %s.* to '%s'@'%s' identified by '%s'\"" % (sql_dict["host"],
                                                                                                                         root_user,
                                                                                                                         root_pass,
                                                                                                                         sql_dict["database"],
                                                                                                                         sql_dict["username"],
                                                                                                                         client_host,
                                                                                                                         sql_dict["userpass"])
        r = call(command, shell=True)
        if r == 0:
            print "user created."

        print "-------------- done ---------------"



def main():

    file = ""
    try:
        opts, args = getopt(sys.argv[1:], "f:", ["file="])

    except GetoptError:
        print "There is an error in your parameter syntax:"
        usage()
        sys.exit(1)

    for opt, arg in opts:
        if opt in ('-f', '--file'):
            file = arg
        elif opt in ('-h', '--help'):
            usage()
            sys.exit(1)

    if file:
        create_database(file)
    else:
        usage()
        sys.exit(2)

if __name__ == '__main__':
    main()
