#!/usr/bin/env python
from time import sleep
from sys import argv,exit
from os.path import exists,isfile
from getopt import getopt

from archmod.CHM import *

optlist, args = getopt(argv[1:], "p:x", ["help"])

optlist = dict(optlist)

if not args or optlist.has_key('--help'):
    print """arCHMage, version 0.0.7
usage: archmage <chmfile> <destdir> -- extracts <chmfile> into
                                       directory <destdir>
       archmage -p <port> <chmfile> -- acts as http server on port <port>,
                                       so you can read <chmfile> with
                                       your favourite browser
   """
    exit()

if not exists(args[0]):
    raise "NoSuchFile"

if len(args) < 2 and not optlist.has_key('-p'):
    print "give the directory name to extract to"
    exit()

source = isfile(args[0]) and CHMFile(args[0]) or CHMDir(args[0])

if optlist.has_key('-p'):
    CHMServer(source, port = int(optlist['-p'])).run()
else:
    source.extract(args[1])
