#!/usr/bin/env python

"""Read in a file (or stdin) using one encoding and write it out in another.
"""

__author__ = "Christopher Arndt <chris.arndt@web.de>
__version__ = "1.0"
__date__ = "20.10.2002"

def get_options(args):
    import getopt

    global options

    options = {
      'inenc': 'latin1',
      'outenc': 'utf-8',
      'replace': 0
    }

    try:
        opts, args = getopt.getopt(sys.argv[1:], 'ri:o:', 
        ['--replace', '--output-encoding', '--input-encoding'])
    except getopt.GetoptError, msg:
        sys.stderr.write("Error parsing options: %s\n" % msg)
        sys.exit(1)

    for o,a in opts:
        if o in ['-o', '--output-encoding']:
            options['outenc'] = a
        elif o in ['-i', '--input-encoding']:
            options['inenc'] = a
        elif o in ['-r', '--replace']:
            options['replace'] = 1
        elif o in ['-h', '--help']:
            usage()
            sys.exit(0)
        elif o in ['-V', '--version']:
            warn(__progname__, __version__)
            sys.exit(0)
    return args

def main(args):
    global options
    
    args = get_options(args)
    
    if len(args) > 0:
        infile = args.pop(0)
        if not options['replace']:
           if len(args) > 0:
               outfile = args.pop(0)
           else:
               outfile = "-"
        else:
            outfile = infile
    else:
        infile = outfile = "-"

    
    if infile == "-":
        f = sys.stdin
    else:
        try:
            f = open(infile)
        except (IOError, OSError):
            warn("Could not open input file!")
            sys.exit(1)
    
    # read it's lines into a list
    t = f.read()
    
    if f is not sys.stdin:
        f.close()

    t = unicode(t, options['inenc'])

    if outfile == "-":
        out = sys.stdout
    else:
        try:
            out = open(outfile, 'w')
        except (IOError, OSError):
            warn("Could not open output file!")
            sys.exit(1)
    
    out.write(t.encode(options['outenc']))
        
    if out is not sys.stdout:
        out.close()
    
    
if __name__ == '__main__':
    import sys
    main(sys.argv[1:])