#!/usr/bin/env python
import sys
import os.path
from argparse import ArgumentParser

import seesaw
seesaw.runner_type = "Standalone"

from seesaw.runner import *
from seesaw.web import SeesawConnection, start_runner_server

def load_pipeline(pipeline_path, context):
  dirname, basename = os.path.split(pipeline_path)
  if dirname == "":
    dirname = "."

  with open(pipeline_path) as f:
    pipeline_str = f.read()

  local_context = context
  global_context = context
  curdir = os.getcwd()
  try:
    os.chdir(dirname)
    exec pipeline_str in local_context, global_context
  finally:
    os.chdir(curdir)
  return ( local_context["project"], local_context["pipeline"] )

def main():
  parser = ArgumentParser(description="Run the pipeline")
  parser.add_argument("pipeline", metavar="PIPELINE", type=str,
                      help="the pipeline file")
  parser.add_argument("downloader", metavar="DOWNLOADER", type=str,
                      help="your username")
  parser.add_argument("--concurrent", dest="concurrent_items",
                      help="work on N items at a time (default: 1)",
                      metavar="N", type=int, default=1)
  parser.add_argument("--max-items", dest="max_items",
                      help="stop after completing N items",
                      metavar="N", type=int, default=None)
  parser.add_argument("--stop-file", dest="stop_file",
                      help="the STOP file to be monitored (default: STOP)",
                      metavar="FILE", type=str, default="STOP")
  parser.add_argument("--disable-web-server", dest="enable_web_server",
                      help="disable the web interface",
                      action="store_false")
  parser.add_argument("--keep-data", dest="keep_data",
                      help="do not remove data of finished items",
                      action="store_true")
  parser.add_argument("--address", dest="address",
                      help="the IP address of the web interface (default: 0.0.0.0)",
                      metavar="HOST", type=str, default="0.0.0.0")
  parser.add_argument("--port", dest="port_number",
                      help="the port number for the web interface (default: 8001)",
                      metavar="PORT", type=int, default=8001)
  parser.add_argument("--http-username", dest="http_username",
                      help="username for the web interface (default: admin)",
                      metavar="USERNAME", type=str), # default is set in start_runner_server
  parser.add_argument("--http-password", dest="http_password",
                      help="password for the web interface",
                      metavar="PASSWORD", type=str),
  args = parser.parse_args()

  (project, pipeline) = load_pipeline(args.pipeline, { "downloader": args.downloader })

  print "*" * 74
  print "*%-072s*" % " "
  print "*%-072s*" % ("   ArchiveTeam Seesaw kit - %s" % seesaw.__version__)
  print "*%-072s*" % " "
  print "*" * 74
  print
  print "Initializing pipeline for '%s'" % (project.title)
  print
  print pipeline
  print
  print "-" * 74
  print

  runner = SimpleRunner(
      pipeline,
      stop_file=args.stop_file,
      concurrent_items=args.concurrent_items,
      max_items=args.max_items,
      keep_data=args.keep_data)

  if args.enable_web_server:
    print "Starting the web interface on %s:%d..." % (args.address, args.port_number)
    print
    print "-" * 74
    print
    start_runner_server(project, runner,
                        bind_address=args.address,
                        port_number=args.port_number,
                        http_username=args.http_username,
                        http_password=args.http_password)

  print "Run 'touch %s' to stop downloading." % args.stop_file
  print
  runner.start()


if __name__ == "__main__":
  main()

