#!/usr/bin/env python
import sys, os, shutil, optparse
try:
    import pep381client
except ImportError:
    # See whether we are run out of a source tree
    module_path = os.path.join(os.path.dirname(sys.argv[0]), 
                               '..')
    if os.path.exists(module_path):
        sys.path.append(module_path)
    import pep381client

opts = optparse.OptionParser(usage="Usage: pep381run [OPTIONS] <targetdir>")
opts.add_option("-q", "--quiet", dest="quiet", action="store_true", default=False,
        help="Don't print status information")
options, args = opts.parse_args()

if len(args) != 1:
    opts.error("You have to specify a target directory")

targetdir = args[0]
if not os.path.exists(os.path.join(targetdir, 'web')):
    state = pep381client.Synchronization.initialize(targetdir)
else:
    state = pep381client.Synchronization.load(targetdir)

state.quiet = options.quiet

try:
    state.synchronize()
except KeyboardInterrupt:
    print "\nMirroring paused. Resume by restarting pep381run."
