#!/usr/bin/env python
# -----------------------------------------------------------------------
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
# -----------------------------------------------------------------------


import os
import sys
import getopt

from ducc_util import DuccUtil
from properties import Properties
from ducc import Ducc

class PropsDiff(DuccUtil):

    def usage(self, msg):
        if (msg != None):
            if ( msg[0] != None ):
                msg = ' '.join(msg)
            print msg
              
        print "Usage:"
        print "   ducc_props_diff [other-props-file]"
        print ''
        print '   This script compares the installed ducc.properties against another ducc.properties'
        sys.exit(1)

    def main(self, argv):

        if ( (len(argv) != 1) or (argv[0] == '-h') or (argv[0] == '-?') ):
            self.usage(None)
        
        diffs = Properties()

        foreign = Properties();
        try:
            foreign.load(argv[0])
        except:
            print "Cannot load", argv[0]
            sys.exit(1)

        local = self.ducc_properties
        
        # Iterate
        # If a thing is in both maps, delete it from the maps
        # and put it into the diffmap for printing
        for ( k, v ) in foreign.items():
            lv = local.get(k)
            if ( lv != None ):
                if ( v != lv ):                    
                    diffs.put(k, (v, lv))
                local.delete(k)
                foreign.delete(k)

        print '--------------------------------------------------------------------------------'
        if ( len(foreign) == 0 ):
            print "Every property in", argv[0], "is in ducc.properties"
        else:
            print "These items are in", argv[0], "only"
            for ( k, v ) in foreign.items():
                print '  ', k, v
        print '--------------------------------------------------------------------------------'
        print ''

        if ( local.items == 0 ):
            print "Every property in ducc.properties is in", argv[0]
        else:
            print "These items are in ducc.properties only"
            for ( k, v ) in local.items():
                print '  ', k, v
        print '--------------------------------------------------------------------------------'
        print ''

        print "These are in both maps with different values"
        for ( k, v ) in diffs.items():
            print k
            print '   installed : ', v[1]
            print '   compare to: ', v[0]
            print ''

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