#!/usr/bin/python3

from __future__ import print_function

import pty
import os
import re
import sys
import time
import getopt

BOSCO_KEY = os.path.expanduser("~/.ssh/bosco_key.rsa")
BOSCO_PASS = os.path.expanduser("~/.bosco/.pass")


def parse_opts(argv):
    optlist, args = getopt.getopt(argv, 'k:p:n', ['key=', 'pass=', 'nopass'])
    # --key, -k FILE : name of the file with private key used for ssh (def: ~/.ssh/bosco_key.rsa)
    # --pass, -p FILE :  name of the file with the passphrase for the private key (def: ~/.bosco/.pass)
    # --nopass, -n : if the private key requires no passphrase
    # TODO: add help, check for no arguments and consistent combinations
    global BOSCO_PASS
    global BOSCO_KEY
    for opt in optlist:
        if opt[0]=='--key' or  opt[0]=='-k':
            BOSCO_KEY = opt[1]
        elif opt[0]=='--nopass' or opt[0]=='-n':
            BOSCO_PASS = ""
        elif opt[0]=='--pass' or opt[0]=='-p':
            BOSCO_PASS = opt[1]
        else:
            # wrong option
            pass


def main():
    max_read = 1024
    
    # Start the pty
    pid, child = pty.fork()
    if pid == 0:
        os.execv ("/usr/bin/ssh-add", ['ssh-add'] + [ BOSCO_KEY ] )
    else:
        # Read from the input
        sent_pass = False
        while True:
            time.sleep(0.3)
            read = os.read(child, max_read).decode()

            # No authentication agent
            if re.search('.*authentication agent.*', read):
                sys.exit(-1)

            # No password required
            elif re.search('.*Identity added.*', read):
                sys.exit(0)

            # Send password
            elif re.search('.*passphrase.*', read):
                if sent_pass == True:
                    # Already sent pass, means it failed
                    sys.exit(-1)
                else:
                    sent_pass = True

                # Read in the password
                f = open(BOSCO_PASS)
                bosco_pass = f.read().encode()
                f.close()
                
                # Write the password out
                os.write(child, bosco_pass + b'\n')

                continue

            if len(read) == 0:
                break




if __name__ == "__main__":
    if sys.argv and len(sys.argv)>1:
        parse_opts(sys.argv[1:])
    main()

