#!/usr/bin/env python3
"""
   Main entry point for graffle2svg
"""
#Copyright (c) 2009, Tim Wintle
#Copyright (c) 2015, Tim Wintle, Stephane Galland
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
#    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#    * Neither the name of the project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import sys

from graffle2svg.main import *


def get_options():
    from optparse import OptionParser
    usage = """
       %prog [options] SOURCE DESTINATION
   or: %prog [options] DESTINATION
   or: %prog [options] --display SOURCE
   or: %prog [options] --display
   or: %prog [options] --stdout SOURCE
   or: %prog [options] --stdout"""
    
    parser = OptionParser(usage=usage)
    parser.add_option("-c", "--stdout", dest="stdout", 
                        help="write converted SVG to stdout", 
                        action="store_true")
    parser.add_option("-d", "--display", dest="display", 
                        help="display the file using default svg viewer. Output is ignored", 
                        action="store_true")
    parser.add_option("-e", "--extract", dest="area", action="store", type="string",
                      help="extract the area given by ((x1,y1),(x2,y2))")
    parser.add_option("-p", "--page", dest="page", 
                        help="for multi-page documents, page number to extract")
    parser.add_option("-v", "--verbose", dest="verbose", 
                        help="verbose", 
                        action="store_true")
                        
    
    (options, args) = parser.parse_args()
    # set up rest of options
    optsdict = {}
    optsdict["stdin"] = False
    
    if options.stdout == True:
        if len(args) > 1:
            parser.error("Too many arguments")
        elif len(args) == 0:
            optsdict["stdin"] = True
        else:
            optsdict["infile"] = args[0]
    else:
        if len(args) > 2:
            parser.error("Too many arguments")
        elif len(args) == 0 and not (options.display == True):
            parser.error("Too few arguments")
        elif len(args) == 1:
            if options.display == True:
                # don't accept outfile for display
                optsdict["infile"] = args[0]
            else:
                #stdin
                optsdict["stdin"] = True
                optsdict["outfile"] = args[0]
        elif len(args) == 2:
            optsdict["infile"] = args[0]
            optsdict["outfile"] = args[1]

        if options.area:
            try:
                optsdict["area"] = eval(options.area)
            except:
                raise Exception('area not correct')
        else:
            optsdict["area"] = None
    return(optsdict, options)
    

if __name__ == "__main__":

    optsdict, options = get_options()
    
    import sys, tempfile
    import subprocess, os
    
    gp = GraffleParser()
    gi = GraffleInterpreter()
    svgTarget = TargetSvg()
    gi.setTarget(svgTarget)
    
    graffle_data = ""
    if optsdict["stdin"]:
        graffle_data = sys.stdin.read()
        gi.dict = GraffleParser().walkGraffle(graffle_data)
    else:
        import graffle2svg.filepack as filepack
        grafflefilepack = filepack.GraffleFilePack(optsdict["infile"])
        gi.dict = GraffleParser().walkGraffleFile(grafflefilepack.fileObject)
        grafflefilepack.close()

    try:        
        page = int(options.page)
        gi.extractPage(page=page, bounding_box=optsdict["area"])
    except:
        gi.extractPage()
    
    
    if options.display == True:
        # write a temp file and open that
        outfile, filename = tempfile.mkstemp(suffix=".svg")
        f = open(filename,"wb")
        f.write(gi.target.svg)
        f.close()
        if os.name == 'mac':
            subprocess.call(('open', filename))
        elif os.name == 'nt':
            subprocess.call(('start', filename))
        elif os.name == "posix":
            subprocess.call(('xdg-open', filename))

    elif options.stdout == True:
        #print to stdout
        sys.stdout.buffer.write(gi.target.svg)
    else:
        f = open(optsdict["outfile"],"wb")
        f.write(gi.target.svg)
        f.close()
