#! /usr/bin/python3
import sys
import os

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

from accesscontrolcli import AccessControlCliManager

def usage():
	puts("Usage")
	with indent(4):
		puts("lliurex-access-control-cli [FLAGS...] ACTION")
		puts("Actions")
		with indent(4):	
			puts("currentconfig [groups|users|center|all] : Shows information about access control for groups, users, center code or all  ")
			puts("lockgroup <GROUP> <GROUP>...: Lock access to computer for the indicated groups")
			puts("unlockgroup <GROUP> <GROUP>...: Unlock access to computer for the indicated groups")
			puts("disablecontrolgroup: Disable access control by group")
			puts("enablecontrolgroup: Activate access control by groups if there is a previous configuration with locked groups")
			puts("lockuser <USERNAME> <USENAME>...: Lock access to computer for the indicated usernames")
			puts("unlockuser <USERNAME> <USERNAME>...: Unlock access to computer for the indicated usernames")
			puts("disablecontroluser: Disable access control by user")
			puts("enablecontroluser: Activate access control by users if there is a previous configuration with locked users")
			puts("removeuserfromlist <USERNAME> <USERNAME>...: Removed the indicated usernames from the user list")
			puts("removeuserlist: Remove the user list")
			puts("setcenter <CENTER CODE>: Configure center code to lock access to computer for users who do not belong to it")
			puts("removecenter: Remove center code to unlock access to compurter for users who do not belong to it")
			puts("disablecontrolcenter: Disable access control by center")
			puts("enablecontrolcenter: Enable access control by center if there is a previous configuration with center code")
		puts("Flags")
		with indent(4):
			puts("-h --help : Show help")
			puts("-u --unattended: Run the actions without prompting for user confirmation")
			puts("-sa --skip-admin: Prevent local admin lockout. Only valid in combination with unattended option")

	sys.exit(1)

#def usage	
		
if __name__ == '__main__':

	if os.geteuid() != 0:
		print("   [Access-Control]: You need be root!")
		sys.exit(1)

	args = arguments.Args().copy
	mode=False
	skipAdmin=False
	debug=False
	infoOption=None
	groupsSelected=[]
	usersSelected=[]
	cdcCode=""

	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(["-sa","--skip-admin"]):
		skipAdmin=True
		index= args.first(["-sa","--skip-admin"])
		args.pop(index)

	action = args.pop(0)
	
	if action=="currentconfig":
		if args.get(0)=="all":
			infoOption="all"
		elif args.get(0)=="groups":
			infoOption="groups"
		elif args.get(0)=="users":
			infoOption="users"
		elif args.get(0)=="center":
			infoOption="center"
		else:
			usage()
	elif action=="lockgroup" or action=="unlockgroup":
		if len(args)>0:
			for item in args.all:
				groupsSelected.append(item.lower())
		else:
			usage()
	elif action=="lockuser" or action=="unlockuser":
		if len(args)>0:
			for item in args.all:
				usersSelected.append(item.lower())
		else:
			usage()
	elif action=="removeuserfromlist":
		if len(args)>0:
			for item in args.all:
				usersSelected.append(item.lower())
		else:
			usage()
	elif action=="setcenter":
		if len(args)>0:
			cdcCode=args.all[0]

	ac=AccessControlCliManager(mode,skipAdmin)
	
	if action == "currentconfig":
		sys.exit(ac.showCurrentConfig(infoOption))
	elif action=="lockgroup":
		sys.exit(ac.lockGroup(groupsSelected))
	elif action=="unlockgroup":
		sys.exit(ac.unlockGroup(groupsSelected))
	elif action=="disablecontrolgroup":
		sys.exit(ac.disableControlGroup())
	elif action=="enablecontrolgroup":
		sys.exit(ac.enableControlGroup())
	elif action=="lockuser":
		sys.exit(ac.lockUser(usersSelected))
	elif action=="unlockuser":
		sys.exit(ac.unlockUser(usersSelected))
	elif action=="disablecontroluser":
		sys.exit(ac.disableControlUser())
	elif action=="enablecontroluser":
		sys.exit(ac.enableControlUser())
	elif action=="removeuserfromlist":
		sys.exit(ac.removeUserFromList(usersSelected))
	elif action=="removeuserlist":
		sys.exit(ac.removeUserList())
	elif action=="setcenter":
		sys.exit(ac.setCenter(cdcCode))
	elif action=="removecenter":
		sys.exit(ac.removeCenter())
	elif action=="disablecontrolcenter":
		sys.exit(ac.disableControlCenter())
	elif action=="enablecontrolcenter":
		sys.exit(ac.enableControlCenter())
	else:
		usage()