#!/usr/bin/env julia


using Weave
using ArgParse

ap = ArgParseSettings("Weave Julia documents using Weave.jl",
                version = string(Pkg.installed("Weave")),
                add_version = true)

@add_arg_table ap begin
    "source"
        nargs = '+'
        help = "source document(s)"
        required = true
    "--doctype"
        default = :auto
        help = "output format"
    "--plotlib"
        arg_type = String
        default = "Gadfly"
        help = "output format"
    "--informat"
        default = :auto
        help = "output format"
    "--out_path"
        arg_type = String
        default = ":doc"
        help = "output directory"
    "--fig_path"
        arg_type = String
        default = "figures"
        help = "figure output directory"
    "--fig_ext"
        default = nothing
        help = "figure file format"
end

args = ArgParse.parse_args(ap)
source = args["source"]
delete!(args, "source")
args_col = []

args["plotlib"] == "nothing" && (args["plotlib"] = nothing)
#Check for special values of out_path

#args["informat"] == ":auto" && (args["informat"] = :auto)
#args["doctype"] == ":auto" && (args["informat"] = :auto)

if args["out_path"] == ":doc"
    args["out_path"] = :doc
elseif args["out_path"] == ":pwd"
    args["out_path"] = :pwd
end

for (key, val) in args
    push!(args_col, (parse(key), val))
end

for s=source
    weave(s; args_col...)
end
