#!/usr/bin/env python3
# Copyright (C) 2008-20 Dr. Ralf Schlatterbeck Open Source Consulting.
# Reichergasse 131, A-3411 Weidling.
# Web: http://www.runtux.com Email: office@runtux.com
# All rights reserved
# ****************************************************************************
#
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as
# published by the Free Software Foundation; either version 2 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# ****************************************************************************

import sys
from argparse           import ArgumentParser
from io                 import BytesIO
from ooopy.OOoPy        import OOoPy
from ooopy.Transformer  import Transformer
import ooopy.Transforms as     Transforms

if __name__ == '__main__' :
    parser = ArgumentParser ()
    parser.add_argument \
        ( "values"
        , help    = "Assignments of the form name=value for field replacement"
        , nargs   = '*'
        )
    parser.add_argument \
        ( "-i", "--input-file"
        , dest    = "input_file"
        , help    = "Input file (defaults to stdin)"
        , default = None
        )
    parser.add_argument \
        ( "-o", "--output-file"
        , dest    = "output_file"
        , help    = "Output file (defaults to stdout)"
        , default = None
        )
    args = parser.parse_args ()
    fields  = dict (arg.split ('=', 1) for arg in args.values)
    infile  = args.input_file
    outfile = args.output_file
    if infile is None :
        infile = BytesIO (sys.stdin.read ())
    if outfile is None :
        outfile = BytesIO ()
    o = OOoPy (infile = infile, outfile = outfile)
    t = Transformer \
        ( o.mimetype
        , Transforms.Editinfo      ()
        , Transforms.Field_Replace (replace = fields)
        , Transforms.Fix_OOo_Tag   ()
        )
    t.transform (o)
    o.close ()
    if args.output_file is None :
        sys.stdout.write (outfile.getvalue ())
