#! /usr/bin/env python

# x2utf8 --- Convert stdin to UTF-8 using a simple heuristic to try to
#            guess the input character set and encoding
#
# Copyright (c) 2004 Florent Rougon
#
# 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; version 2 dated June, 1991.
#
# 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; see the file COPYING. If not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

import sys

if sys.hexversion < 0x02020000:
    sys.exit("This script requires Python 2.2 or greater. Sorry.")


def main():
    input = sys.stdin.read()

    try:
        u = input.decode("utf_8")
    except UnicodeError:
        try:
            u = input.decode("iso8859_1")
        except UnicodeError, v:
            sys.exit("Error while trying to decode the input as "
                     "ISO 8859-1: %s" % v)

    try:
        sys.stdout.write(u.encode("utf_8"))
    except UnicodeError, v:
        sys.exit("Error while trying to encode the data as "
                 "UTF-8: %s" % v)

    sys.exit(0)


if __name__ == "__main__": main()
