#!/usr/bin/env python
#
# vim: set ts=4 sw=4 sts=0 et :

from __future__ import print_function

import pexpect, os, sys, getopt, time, re


def usage(code):
    print('Usage: wait-ios-serial [-i prompt] -s server [-p port] [-i prompt]\n', file=sys.stderr)
    sys.exit(code)

def main(argv):
    # Options
    server = 'localhost'
    port = 23
    tmout = 300 # Total timeout
    cycle = 10  # Cycle timeout
    input = '>'
    username = 'cisco'
    password = 'cisco'
    debug = 0

    # Local vars
    tries = 0

    # Cmdline parsing
    try:
        opts, args = getopt.getopt(argv,"dp:s:t:",["debug","port=","server=","timeout="])
    except getopt.GetoptError:
        usage(2)
    for opt, arg in opts:
        if opt in ('-d','--debug'):
            debug += 1
        if opt in ('-p','--port'):
            port = arg
        if opt in ('-s','--server'):
            server = arg
        if opt in ('-t','--timeout'):
            tmout = int(arg)
    if server == '':
        usage(2)
    inputs = args
    if len(inputs) < 1:
        usage(2)

    if debug:
        print('\nDebug level: %d' % debug, file=sys.stderr)

    # The meat
    child = pexpect.spawn('telnet %s %s' % (server,port), timeout=tmout)

    if debug > 0:
        child.logfile = sys.stdout
    try:
        child.expect("Escape character is .*", timeout=10)
    except pexpect.EOF as e:
        exit(1)

    # Wait for the system to boot
    first = last = time.time()
    current = 0
    expr = re.compile(inputs[current])
    done = False
    while not done:
        try:
            line = child.readline()
            line = re.sub('\r','',line)
            line = re.sub('\n','',line)
            if len(line) == 0:
                continue
            if debug > 1:
                print('\nLine is length %d, text is "%s"' % (len(line), line), file=sys.stderr)
            if expr.match(line):
                current += 1
                if current == len(inputs):
                    print('\nLast match found, exiting!', file=sys.stderr)
                    done = True
                    continue
                print('\nMatch found, next is: %s' % inputs[current], file=sys.stderr)
                expr = re.compile(inputs[current])

        except pexpect.TIMEOUT as e:
            if time.time() - first > tmout:
                print('\nTimeout of %d seconds exceeded, giving up.' % tmout, file=sys.stderr)
                exit(1)

        except:
            print('Exception: %s' % sys.exc_info()[0], file=sys.stderr)
            exit(2)

if __name__ == "__main__":
    main(sys.argv[1:])

