#!/usr/bin/env python
import os, sys
from optparse import OptionParser

import kforge.cli.data

if __name__ == '__main__':
    usage  = '''usage: %prog [path]

Produce a KForge configuration file template.

If path provided then write template to file at path, otherwise print to
stdout.
    '''
    parser = OptionParser(usage)

    parser.add_option(
        '--master-dir',
        dest='masterDir',
        help="""The prefix for other paths in the configuration file. You
don't need to use this, but if you don't you must edit the generated file
so that the other paths have suitable values.""")

    parser.add_option(
        '--service-name',
        dest='serviceName',
        help="The service name to be used.")

    parser.add_option(
        '--environment-timezone',
        dest='environmentTimezone',
        help="The timezone to be used.")

    parser.add_option(
        '--project-data-dir',
        dest='projectDataDir',
        help="""The folder to be used for project service data.
Please note that it can be worth setting this path outside of
any master folder (--master-dir). New versions of KForge can be installed
separately to previous installations but will need to use the same
project service data folder, which must not be moved once created.
""")

    parser.add_option(
        '--db-user',
        dest='dbUser',
        help="The database user to be used.")

    parser.add_option(
        '--db-pass',
        dest='dbPass',
        help="The database password to be used.")

    parser.add_option(
        '--db-name',
        dest='dbName',
        help="The database instance to be used.")

    parser.add_option(
        '--db-type',
        dest='dbType',
        help="""The database type to be used. The choices are 'sqlite',
'postgres' or 'mysql'. The default is 'sqlite'.""")

    parser.add_option(
        '--db-host',
        dest='dbHost',
        help="The database host to be used.")

    parser.add_option(
        '--virtualenv-bin-dir',
        dest='virtualenvBinDir',
        help="""The virtual environment scripts folder.""")

    parser.add_option(
        '--trac-admin',
        dest='tracAdmin',
        help="""The path to your trac-admin script.""")

    parser.add_option(
        '--enable-memoization',
        dest='enableMemoization',
        action='store_true', 
        help="""Enable memoization in the access controller.""")

    (options, args) = parser.parse_args()
    if len(args) > 1:
        print 'ERROR: you have supplied too many arguments\n'
        parser.print_help()
        sys.exit(1)
    conf = kforge.cli.data.get_config_template()
    if options.masterDir:
        conf = conf.replace('master_dir = /path/to/kforge', 'master_dir = %s' % os.path.abspath(options.masterDir))
    if options.serviceName:
        conf = conf.replace('service_name = KForge', 'service_name = %s' % options.serviceName)
    if options.environmentTimezone:
        conf = conf.replace('timezone = Europe/London', 'timezone = %s' % options.environmentTimezone)
    if options.projectDataDir:
        conf = conf.replace('project_data_dir = %(master_dir)s/var/project', 'project_data_dir = %s' % os.path.abspath(options.projectDataDir))
    if options.dbType:
        conf = conf.replace('#type = sqlite', 'type = %s' % options.dbType)
    if options.dbName:
        conf = conf.replace('#name = %(master_dir)s/var/sqlite.db', 'name = %s' % options.dbName)
    if options.dbUser:
        conf = conf.replace('#user = kforge', 'user = %s' % options.dbUser)
    if options.dbPass:
        conf = conf.replace('#pass =', 'pass = %s' % options.dbPass)
    if options.dbHost:
        conf = conf.replace('#host =', 'host = %s' % options.dbHost)
    if options.virtualenvBinDir:
        conf = conf.replace('#bin_dir = %(master_dir)s/bin', 'bin_dir = %s' % options.virtualenvBinDir)
    if options.tracAdmin:
        conf = conf.replace('#admin_script = trac-admin', 'admin_script = %s' % options.tracAdmin)
    if options.enableMemoization:
        conf = conf.replace('#enabled = on', 'enabled = on') # Warning: this would enable other things too.
    if len(args) == 0:
        print conf
    elif len(args) == 1:
        ff = file(args[0], 'w')
        ff.write(conf)
        ff.close()

