#!/usr/bin/env python
import settings

import argparse
import subprocess

# Commands
def deploy():
	print "Deploying application..."
	run("appcfg.py update .")

def new():
	print "Creating structure template..."
	run("mkdir templates")
	run("mkdir handlers")
	run("mkdir entities")
	run("mkdir lib")
	run("mkdir static")
	run("mkdir static/css")
	run("mkdir static/img")
	run("mkdir static/js")
	run("curl https://raw.github.com/djwatt5/jhpy/master/downloads/jhpy.py -o lib/jhpy.py -s")
	run("curl https://raw.github.com/djwatt5/jhpy/master/downloads/index.py -o handlers/index.py -s")
	run("touch entities/__init__.py")
	run("touch handlers/__init__.py")
	run("touch lib/__init__.py")
	run("curl https://raw.github.com/djwatt5/jhpy/master/downloads/main.py -o main.py -s")
	run("curl https://raw.github.com/djwatt5/jhpy/master/downloads/app.yaml -o app.yaml -s")
	print "Finished!"

def quick():
	print "Creating quick template..."
	run("curl https://raw.github.com/djwatt5/jhpy/master/downloads/jhpy.py -o jhpy.py -s")
	run("curl https://raw.github.com/djwatt5/jhpy/master/downloads/main-q.py -o main.py -s")
	run("curl https://raw.github.com/djwatt5/jhpy/master/downloads/app.yaml -o app.yaml -s")
	run("mkdir templates")
	run("mkdir static")
	run("mkdir static/css")
	run("mkdir static/img")
	run("mkdir static/js")
	print "Finished!"

def test(p):
	print "Testing at port %s..." % p
	run("clear")
	run("dev_appserver.py . --port=%s" % p)

def version():
	print settings.version

def default():
	print "Type `jhpy -h` to see all the options available."

# Console stuff
parser = argparse.ArgumentParser()

parser.add_argument('-d', '--deploy',
					default=False,
					action="store_true",
					help="deploy your application to google")
parser.add_argument('-n', '--new',
					default=False,
					action="store_true",
					help="create a new structure template")
parser.add_argument('-q', '--quick',
					default=False,
					action="store_true",
					help="create a new quick template")
parser.add_argument('-t', '--test',
					default=False,
					help="start localhost server on port TEST")
parser.add_argument('-v', '--version',
					default=False,
					action="store_true",
					help="get the installed version of jhpy")

# Methods
def run(c):
	command = c.split()
	subprocess.call(command)

# Wrap up
args = parser.parse_args()

if args.deploy:
	deploy()
elif args.new:
	new()
elif args.quick:
	quick()
elif args.test:
	test(args.test)
elif args.version:
	version()
else:
	default()
