#!/usr/bin/env python
"""
    zerodoc command line wrapper

    See zerodoc.parse.py for details on the zerodoc library

    Copyright (C) 2012 Pablo Martin <pablo at odkq.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
import sys
import zerodoc.html
import zerodoc.rst
import zerodoc.parse
import json
import optparse

def parse_file_into(i, o, f, fopt):
    ''' Transform the zerodoc input and write the result into another
        file '''
    s = i.read()
    d, errstr = zerodoc.parse.parse(s)
    if errstr != None:
        return errstr
    if f == 'html':
         o.write(zerodoc.html.write(d, fopt))
    elif f == 'confluence':
         o.write(zerodoc.confluence.write(d, fopt))
    elif f == 'json':
        o.write(json.dumps(d, indent=2))
    elif f == 'rst':
         o.write(zerodoc.rst.write(d, fopt))
    else:
         return 'unknown format \'' + f + '\' requested.'
    return None

usage = '''
Usage: zerodoc [-f <format>] [-o <format options>] [-i <inputfile>] [-O <outputfile>]
       formats: html json manpage rest confluence markdown
       if format is ommited, html is select by default
       if input file is ommited, stdin is used
       if output file is ommited, stdout is used
'''
if __name__=="__main__":
    parser = optparse.OptionParser()
    parser.add_option("-f", "--format", dest="format", default="html",
                      help="Output format. If ommited, 'html'")
    parser.add_option("-o", "--options", dest="options",
        default="html5 datauri svg " + \
            "css:/usr/share/zerodoc/stylesheets/default.css",
                      help="Options for format renderer")
    parser.add_option("-i", "--input", dest="input", default="stdin",
                      help="Use <filename> as input file. If ommited, use stdin.", metavar="FILE")
    parser.add_option("-O", "--output", dest="output", default="stdout",
                      help="Use <filename> as output file. If ommited, use stdout.", metavar="FILE")
    (o, a) = parser.parse_args()
    fopts = None
    if o.options:
        fopts = o.options.split(' ')
    if o.input == 'stdin':
        fin = sys.stdin
    else:
    	fin = open(o.input, 'r')
    if o.output  == 'stdout':
        fout = sys.stdout
    else:
    	fout = open(o.output, 'w')
    r = parse_file_into(fin, fout, o.format, fopts)
    fin.close()
    fout.close()
    if r != None:
        print 'ERROR: ' + r
        sys.exit(1)
    sys.exit(0)

