#!/usr/bin/env python
import os
import sys
import tarfile
import tempfile
import shutil
import fileinput
import dispatch

PACKAGE_DIR = os.path.dirname(dispatch.__file__)

def rename_path(base, before, after):
    os.rename(os.path.join(base, before), os.path.join(base, after))

def replace_in_file(path, before, after):
    lines = []
    with open(path) as infile:
        for line in infile:
            lines.append(line.replace(before, after))
    with open(path, 'w') as outfile:
        for line in lines:
            outfile.write(line)

def start_project(name):
    template_path = os.path.join(PACKAGE_DIR, 'core/management/templates/project.tar.gz')

    # Use temporary directory to prepare new project files
    tmp_dir = tempfile.mkdtemp()

    # Open and extract the project template tar archive
    tar = tarfile.open(template_path)
    tar.extractall(tmp_dir)

    # Insert project name into manage.py
    replace_in_file(os.path.join(tmp_dir, 'project/manage.py'), '{{project}}', name)

    # Rename default directories using new project name
    rename_path(tmp_dir, 'project/project', 'project/%s' % name)
    rename_path(tmp_dir, 'project', name)

    # Move temporary directory into current working directory
    shutil.move(
        os.path.join(tmp_dir, name),
        os.path.join(os.getcwd(), name)
    )

commands = {
    'startproject': start_project
}

if __name__ == "__main__":

    command_name = sys.argv[1]
    args = sys.argv[2:]

    # Attempt to find command function
    try:
        func = commands[command_name]
    except KeyError:
        print "%s isn't a valid command" % command_name
        exit()

    # Execute the function
    func(*args)
