#!/usr/bin/python2.6
# Copyright (c) 2010 Adam Tauno Williams <awilliam@whitemice.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

import getopt, sys, os, pwd, grp
from pkg_resources import Requirement, resource_filename
from coils.core import initialize_COILS, Backend, AdministrativeContext

def usage():
    print """
    --help      Display this message.
    --initdb    Initialize the database
    --log=      Set the log filename; defaults to "/var/log/coils.log"
    --password= Set the administator password

    Default storage root is '/var/lib/opengroupware/org'
    """
    return

def main(argv):

    # Process command line arguements
    init_db    = False
    logfile    = "/var/log/coils.log"
    password   = None

    try:
        opts, args = getopt.getopt(argv,
                                   "hi:l:p:",
                                  ["help", "initdb", "log=", "password="])
    except getopt.GetoptError, e:
        print e
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit(0)
        elif (opt in ('-i', '--initdb')):
            init_db = True
        elif (opt in ('-l', '--log')):
            logfile = arg
        elif (opt in ('-p', '--password')):
            password = arg

    if (password is None):
        print 'ERROR: You must provide the initial administrative password when initializing the database!'
        sys.exit(1)

    if (init_db):
        ddl_file = resource_filename(Requirement.parse("OpenGroupware"),"coils/foundation/alchemy/postgresql.ddl")
        handle = open(ddl_file, 'rb')
        ddl_sql = handle.read()
        handle.close()

        initialize_COILS({'log_file': logfile})
        db = Backend.db_session()
        db.execute(ddl_sql)
        db.commit()
        db.close()

        ctx = AdministrativeContext()
        ctx.run_command('account::set-password', login='ogo', password=password)
        ctx.commit()


    sys.exit(0)


if __name__ == "__main__":
    main(sys.argv[1:])
