#!/usr/bin/python
"""
Contains the script that create a upy project 
"""
import os
import shutil
import upy

UPY_LOCATION = "%s/%s" % (os.path.dirname(upy.__file__),"bin/upy_project_set")


def overwrite_proj():
    """
    It asks to user if he want overwrite existing project
    """
    q = raw_input("Do you want overwrite it? (y/n): ")
    if q == "y":
        return True
    elif q == "n":
        return False
    else:
        print "Tap y or n"
        return overwrite_proj()

def create_project():
    """
    It creates upy project
    """
    name = raw_input("Insert project name: ")
    listing = []
    if os.path.exists(UPY_LOCATION):
        listing = os.listdir(UPY_LOCATION)
    
    create_proj = False
    if os.path.exists("%s" %(name)):
        print "Project already exists!"
        if overwrite_proj():
            shutil.rmtree(name)
            create_proj = True
    else:
        os.mkdir(name)
        create_proj = True
    if create_proj:
        try:
            os.makedirs("%s/templates/admin" % name)
            os.makedirs("%s/static" % name)
            print "Configuring project!"
            print "Copying files: "
            print listing
            for f in listing:
                if os.path.isdir("%s/%s" % (UPY_LOCATION,f)):
                    shutil.copytree("%s/%s" % (UPY_LOCATION,f),'%s/%s' % (name,f))
                elif f[-3:] != "pyc" or f[-3:] != "svn":
                    shutil.copy2('%s/%s' % (UPY_LOCATION,f), '%s/%s' % (name,f))
        except Exception, e:
            print e
            print "Something is going wrong!"
        
        else:
            print "Project configured!"  
        
            

create_project()