#!/usr/bin/env python
import os, sys, shutil, time, string


def cmd(st):
    c = os.path.join(twistedBinDir,'') + st
    print "Running Command: %s" % repr(c)
    return os.system(c)

def browse(url):
    scmd(webbrowser+" "+url)
    if block:
        print "Hit enter to continue:"
        raw_input()

def scmd(st):
    print 'Running System Command: %s' % repr(st)
    return os.system(st)

def message(*m):
    print '/------'
    print '|####'
    for line in m:
        print '|####', line
    print '|####'
    print '\------'

def twistdf(f):
    cmd("twistd -f %s.tap" %f)
    time.sleep(0.5)

def twistdg(g):
    cmd("twistd -g %s" %g)
    time.sleep(0.5)

def killit():
    scmd("kill `cat twistd.pid`")
    # Give it a sec to come down.
    time.sleep(0.5)

def pause():
    print "Hit enter to continue."
    raw_input()

def basicWebTest():
    message("Running Basic Web Test")
    cmd("mktap web")
    twistdf("web")
    message("You should see a rather complex bunch of widgetry now.")
    browse("http://localhost:8080/")
    killit()

def staticWebTest():
    message("Running Static Web Test")
    cmd("mktap web --static %s/../static" % twistedBinDir)
    twistdf("web")
    message("You should see an 'it worked' page now.",
            "(depending on your browser, you may need to reload)")
    browse("http://localhost:8080/")
    message("This is Python CGI test output.")
    browse("http://localhost:8080/test.cgi")
    killit()


def distWebTest():
    message("Running Distributed Web Test")
    # make directories to stage the test
    scmd("mkdir Personal")
    scmd("mkdir User")
    # make & start personal server
    os.chdir("Personal")
    cmd("mktap web --personal")
    twistdf("web")
    os.chdir("..")
    # make & start the user server
    os.chdir("User")
    cmd("mktap web --user")
    twistdf("web")
    os.chdir("..")
    # browse a dead web page
    message("This should say 'Unable to connect to distributed server'",
            "If it doesn't finish loading, it's broken.  Reload a few times..")
    browse("http://localhost:8080/nobody.twistd")
    # browse a live web page
    message("This should be a bunch-of-widgets test page.")
    browse("http://localhost:8080/%s.twistd" % username)
    # clean up
    os.chdir("User")
    killit()
    os.chdir("../Personal")
    killit()
    os.chdir("..")
    shutil.rmtree("User")
    shutil.rmtree("Personal")

def runTelnetTest():
    message("Running Telnet Test")
    cmd("mktap telnet -p 8023 -u username -w password")
    twistdf("telnet")
    message("Log in with the username 'username', password 'password'.",
            "You should be able to execute python code.",
            "Log out with '^]close'")
    scmd("telnet localhost 8023")
    killit()

def runManholeTest():
    message("Running Manhole Test")
    cmd("mktap manhole -u username -w password")
    twistdf("manhole")
    message("Log in with the username 'username', password 'password'.",
            "and bask in the l33tness of direct manipulation.")
    cmd("manhole")
    killit()

def runWordsTest():
    message("Running Words Test")
    cmd("mktap words")
    twistdf("words")
    message("Create yourself an account, username 'test' password 'testing'.")
    browse("http://localhost:8080/create")
    message("You will have to '/msg *login* testing' to log in.")
    scmd(ircclient+" test localhost")
    if block:
        print "Hit enter to continue:"
        raw_input()
    message("Now let's test the 'im' interface.")
    cmd("im")
    message("And t-im...")
    cmd("t-im")
    killit()

def runRealityTest():
    message("Running Reality Test")
    for mapname, loginname, password in [('TRDemo', 'guest', 'guest'),
                                         ('Inheritance', 'Damien', 'admin'),
                                         ('Divunal', 'guest', 'guest')]:
        if os.path.exists(mapname):
            twistdg(mapname)
            message("Log in now, username %s password %s" %
                    (repr(loginname), repr(password)))
            cmd("faucet")
            message("Now again, with the TK interface.")
            cmd("faucet --toolkit tk")
            message("Log in again on the telnet interface.")
            scmd("telnet localhost 4040")
            message("Now take a look at the website, after logging in")
            browse("http://localhost:8080/")
            killit()
        else:
            message("reality map %s not found, skipping" % mapname)

def runExampleTest():
    examplesDir = twistedBinDir+'/../doc/examples'
    os.environ['PYTHONPATH'] = '%s:%s' % (os.environ.get('PYTHONPATH') or '',
                                          examplesDir)
    scmd("python %s/pbecho.py" % examplesDir)
    twistdf("pbecho-start")
    message("You should see a 'hello world'")
    scmd("python %s/pbechoclient.py" % examplesDir)
    pause()
    killit()

def runMailTest():
    message("Starting mail test")
    os.mkdir("dump")
    os.mkdir("dump2")
    scmd("mktap mail --domain foo.bar=dump --user postmaster=postmaster")
    scmd("mktap --append mail.tap mail --relay 127.0.0.1,8025=dump2"
         "      --smtp 8026 --pop 8111")
    twistdf("mail")
    import smtplib, poplib
    s = smtplib.SMTP('127.0.0.1', 8026)
    s.sendmail("m@moshez.org", ['postmaster@foo.bar'], '''\
Subject: How are you gentlemen?

All your base are belong to us
''')
    s.quit()
    time.sleep(10)
    p = poplib.POP3('127.0.0.1', 8110)
    p.apop('postmaster@foo.bar', 'postmaster')
    print string.join(p.retr(1)[1], '\n')
    p.quit()
    killit()


def runAllTests():
    message("Starting test.")
    basicWebTest()
    staticWebTest()
    distWebTest()
    runManholeTest()
    runTelnetTest()
    runWordsTest()
    runRealityTest()
    runExampleTest()
    runMailTest()
    message('All tests run.')


twistedBinDir = os.path.dirname(sys.argv[0]) or '.'
try:
    block = sys.argv[1] == "-b"
except IndexError:
    block = None
try:
    webbrowser = os.environ['WEBBROWSER']
    username = os.environ['USER']
    ircclient = os.environ['IRCCLIENT']
except KeyError:
    message("Required Environment Variables:",
            "  * WEBBROWSER: a command which will run a web browser.",
            "                (If this doesn't block until the window is closed,",
            "                 pass '-b' as an argument to the script.)",
            "  * IRCCLIENT: an IRC client in the style of ircii (use -b in the",
            "               same situation as above)",
            "  * USER: your UNIX username.")
else:
    runAllTests()
