#! /usr/bin/python3
import sys

from clint import arguments
from clint.textui import puts, indent, colored

from epicli import EPIC

def usage():
	puts("Usage")
	with indent(4):
		puts("epic [FLAGS...] ACTION")
		puts("Actions")
		with indent(4):	
			puts("showlist : Shows alls epi files avaliables to be installed with EPIC")
			puts("showinfo PATH or NAME APP_EPI_FILE : Shows information about APP_EPI_FILE")
			puts("install PATH or NAME APP_EPI_FILE <optional all> or <optional pkg1> <optional pkg2>...")
			puts("uninstall PATH or NAME APP_EPÌ_FILE <optional all> or <optional pkg1> <optional pkg2>...")

		puts("Flags")
		with indent(4):
			puts("-h --help : Show help")
			puts("-u --unattended: Run the install/uninstall without prompting for user confirmation")
			puts("-nc --nocheck: Run the install/uninstall without previous system checking")
			puts("-d --debug: Run the install/uninstall in debug mode")
	sys.exit(1)

#def usage	
		
if __name__ == '__main__':
	args = arguments.Args().copy
	mode=False
	nocheck=False
	debug=False
	app_file=None
	pkgsToInstall=[]

	if args.contains(["-h","--help"]) or len(args.all) == 0 :
		usage()		
	if args.contains(["-u","--unattended"]):
		mode=True
		index = args.first(["-u","--unattended"])
		args.pop(index)
	if args.contains(["-nc","--nocheck"]):
		nocheck=True
		index = args.first(["-nc","--nocheck"])
		args.pop(index)
	if args.contains(["-d","--debug"]):
		debug=True
		index = args.first(["-d","--debug"])
		args.pop(index)
	
	action = args.pop(0)
	if action!="showlist":
		app_file=args.get(0)
		args.pop(0)
		if len(args.all)>0:
			for item in args.all:
				pkgsToInstall.append(item)
		if app_file ==None:
			usage()

	epic = EPIC(app_file,pkgsToInstall,debug)
	if action=="showlist":
		sys.exit(epic.listEpi())
	if action == "showinfo":
		sys.exit(epic.showInfo())
	elif action=="install":
		sys.exit(epic.install(mode,nocheck))	
	elif action=="uninstall":
		sys.exit(epic.uninstall(mode,nocheck))
	else:
		usage()		
