#!/usr/bin/env python
"""Bull environment setup script."""

import argparse
import os
import sys

APP_CODE = """
from bull import app, db
def get_app():
    return app

if __name__ == '__main__':
    app.config.from_object('config')
    with app.app_context():
        db.metadata.create_all(bind=db.engine)
    get_app().run(debug=True)
"""

CONFIG_CODE = """
from os.path import abspath, dirname, join

_cwd = dirname(abspath(__file__))
# Subject of the email sent after purchase 
# MAIL_SUBJECT = 

# Email address for the 'from' field of the generated email
# MAIL_FROM = 

# Email server address
# MAIL_SERVER = 

# Email server username
# MAIL_USERNAME = 

# Email server password
# MAIL_PASSWORD = 

# Email server port
# MAIL_PORT = 

# Use SSL for email? 
# MAIL_USE_SSL = 

# Website address, for use in Stripe purchases and in email
# SITE_ADDRESS = 

# Database URI for SQLAlchmey (Default: 'sqlite+pysqlite3:///sqlite3.db')
# SQLALCHEMY_DATABASE_URI = 'sqlite+pysqlite:///sqlite3.db'

# Stripe secret key to be used to process purchases
STRIPE_SECRET_KEY = ''

# Stripe public key to be used to process purchases
STRIPE_PUBLIC_KEY = ''
"""

def main(args):
    """Main entry point for script."""
    if args.environment:
        os.mkdir(args.environment)
        os.chdir(args.environment)
        with open('app.py', 'w') as output:
            output.write(APP_CODE)
        with open('config.py', 'w') as output:
            output.write(CONFIG_CODE)
        os.mkdir('files')
        print 'Environment created!'

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('environment', help='directory in which a new bull installation should be setup')
    sys.exit(main(parser.parse_args()))
