#!/usr/bin/python3

import sys, getopt, readline
import pdm.cli

def usage(out):
    out.write("usage: pdm-repl [-h] SOCKET\n")

opts, args = getopt.getopt(sys.argv[1:], "h")
for o, a in opts:
    if o == "-h":
        usage(sys.stdout)
        sys.exit(0)
if len(args) < 1:
    usage(sys.stderr)
    sys.exit(1)
try:
    cl = pdm.cli.replclient(args[0])
except Exception as e:
    sys.stderr.write("%s: %s\n" % (args[0], e))
    sys.exit(1)

buf = ""
while True:
    try:
        if buf != "":
            line = input("  ")
        else:
            line = input("% ")
    except EOFError:
        break
    if line == "":
        sys.stdout.write(cl.run(buf))
        buf = ""
    else:
        if buf == "":
            try:
                compile(line, "Nought", "eval")
            except:
                pass
            else:
                sys.stdout.write(cl.run(line))
                continue
        buf += line + "\n"
