#!/usr/bin/env python3
from orgblog import OrgBlogApp
from orgblog import Config
from optparse import OptionParser

import os
import shutil

optparser = OptionParser()

def copy_defaults(src, dest):
    if os.path.isdir(dest):
        raise Exception("Cowardly refusing to copy to " + dest + " because it already exists.")
    else:
        print("Installing default files to " + dest)
        shutil.copytree(src, dest)

optparser.add_option("--install-default-conf",
                     "--install-default-config",
                     action="store_true",
                     dest="install_default_conf")
optparser.add_option("--install-default-templates",
                     action="store_true",
                     dest="install_default_templates")
optparser.add_option("--install-test-posts",
                     action="store_true",
                     dest="install_test_posts")
optparser.add_option("--static",
                     dest="static",
                     action="store_true",
                     help="Generate site statically instead of serving it.")
optparser.add_option("--config",
                     dest="config_file",
                     help="Specify a config file in a non-standard location")

(options, args) = optparser.parse_args()    

if options.config_file:
    config = Config(filename=options.config_file)
else:
    config = Config()

if options.install_default_conf:
    config.install_default()

if options.install_default_templates:
    #actual template files
    source = os.path.join(os.path.dirname(__file__), "templates")
    destination = config.lookup("template_directory")
    copy_defaults(source, destination)

    #static files
    source = os.path.join(os.path.dirname(__file__), "static")
    destination = config.lookup("static_directory")
    copy_defaults(source, destination)

    
if options.install_test_posts:
    source = os.path.join(os.path.dirname(__file__), "collections")
    destination = Config.lookup("collections_directory")
    copy_defaults(source, destination)

    
app = OrgBlogApp(config=config)

if options.static:
    app.generate()
else:
    app.run()

    
