#!/usr/bin/python3
import os
import readline
import shlex
import sys
import subprocess
import socket
import pwd
import grp
from collections import namedtuple

tuerSock = "/run/tuer.sock"

# use a histfile
histfile = os.path.join(os.path.expanduser("~"), ".tyshellhist")
try:
    readline.read_history_file(histfile)
except IOError:
    pass
import atexit
atexit.register(readline.write_history_file, histfile)

# available commands
def helpcmd(c):
	if (len(c) > 1):
		print(commands.get(c[1],(None,'Can\'t find help for command %s'%(c[1]))).helpstring)
	else:
		print("Available commands: %s" % ", ".join(sorted(commands.keys())))
		print("Use 'help command' to get more information on the command 'command'")

def extcmd(cmd):
	def run(c):
		ret = subprocess.call(cmd)
		if ret != 0:
			print("Command returned non-zero exit statis %d" % ret)
	return run

def sendcmd(addr, cmd):
	def run(c):
		print("206 Sending command %s..." % (cmd))
		s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
		s.connect(addr)
		s.settimeout(60.0)
		s.send(cmd.encode())
		data = s.recv(256)
		s.close()
		print(data.decode('utf-8'))
	return run

def exitcmd(c):
	print("Bye")
	return True

def whocmd(c):
	for n in grp.getgrnam("tuer").gr_mem:
		p = pwd.getpwnam(n)
		print (p.pw_name, " - ", p.pw_gecos)

def alias (cmds, aliases):
	for newname, oldname in aliases.items():
		cmds[newname] = cmds[oldname]
	return cmds

CmdEntry = namedtuple('CmdEntry','function helpstring')

commands = alias({
	'exit': CmdEntry(exitcmd, 'Quits this shell'),
	'help': CmdEntry(helpcmd, 'Helps you getting to know the available commands'),
	'open': CmdEntry(sendcmd(tuerSock, 'unlock'), 'Will try to unlock the apartment door'),
	'buzz': CmdEntry(sendcmd(tuerSock, 'buzz'), 'Will buzz the buzzer for the street door'),
	'who': CmdEntry(whocmd, 'Shows the list of people, who are allowed to control this system'),
},{
	# aliases
	'unlock': 'open',
})

def complete_command(cmd):
	'''returns a list of commands (as strings) starting with cmd'''
	return list(filter(lambda x: x.startswith(cmd), commands.keys()))
readline.set_completer(lambda cmd, num: (complete_command(cmd)+[None])[num]) # wrap complete_command for readline's weird completer API
readline.parse_and_bind("tab: complete") # run completion on tab

# input loop
print("Welcome to tyshell. Use help to see what you can do.")
while True:
	try:
		command = input("$ ")
	except EOFError:
		print()
		break
	command = shlex.split(command)
	if not len(command): continue
	# find suiting commands
	if command[0] in commands: # needed in case a complete command is a prefix of another one
		cmdoptions = [command[0]]
	else:
		cmdoptions = complete_command(command[0])
	# check how many we found
	if len(cmdoptions) == 0: # no commands fit prefix
		print("Command %s not found. Use help." % command[0])
	elif len(cmdoptions) == 1: # exactly one command fits (prefix)
		try:
			res = commands[cmdoptions[0]].function(command)
			if res: break
		except Exception as e:
			print("Error while executing %s: %s" % (command[0], str(e)))
	else: # multiple commands fit the prefix
		print("Ambiguous command prefix, please choose one of the following:")
		print("\t", " ".join(cmdoptions))
		# TODO: put current "command[0]" into the shell for the next command, but such that it is deletable with backspace