#!/usr/bin/python2.6
# Copyright (c) 2010 Adam Tauno Williams <awilliam@whitemice.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import pickle, pprint, getopt, sys, os, codecs
from coils.core               import *

def usage():
    print """
        Read and parse an OpenStep plist file
        --help            Display this message.
        --filename=       plist file to parse (required)
        --output=         Output data to a new plist file (optional)
        --encoding=       Specify the codepage of your plist files (default: ISO8859-1)
    """
    return

def main(argv):
    input_filename = None
    output_filename = None
    encoding = 'iso8859-1'
    try:
        opts, args = getopt.getopt(argv,
                                   "hf:o:e:",
                                    ["help", "filename=", "output=", "encoding="])
    except getopt.GetoptError, e:
        print e
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit(0)
        elif opt in ("-f", "--filename"):
            input_filename = arg
        elif opt in ("-o", "--output"):
            output_filename = arg
        elif opt in ("-e", "--encoding"):
            encoding = arg

    if (input_filename is None):
        usage()
        sys.exit(2)

    if (os.path.exists(input_filename)):
        handle = codecs.open(input_filename, 'rb', encoding=encoding)
        data = handle.read()
        handle.close()
        defaults = None
        defaults = PListParser().propertyListFromString(data)
        if (output_filename is None):
            pprint.pprint(defaults)
        else:
            writer = PListWriter()
            handle = codecs.open(output_filename, 'wb', encoding=encoding)
            data = writer.store(defaults)
            handle.write(data)
            handle.close()
    else:
        print 'File {0} not found'.format(input_filename)
        sys.exit(2)

if __name__ == "__main__":
    main(sys.argv[1:])
