#if at least on TRX in sector is Enabled then remove current cell mapping and build whole cell mapping based on SIR data

if ( objects_count "SECTOR_PROPERTIES" ) == "0" || ( objects_count "TRX_PROPERTIES" ) == "0" || ( objects_count "IP_ADDRESSES" ) == "0"
    abort_file

for_objects "SECTOR_PROPERTIES"
    begin
        define "sectorPropertiesDn" $current_dist_name
        if ( param_exist sectorPropertiesDn "localSectorId" )
            begin
                define "localSectorId" get_param sectorPropertiesDn "localSectorId"
    
                define "removeChannels" "false"

                for_list sectorPropertiesDn "includedObjects"
                    begin
                        define "antenna" get_param sectorPropertiesDn "includedObjects." JOIN $current_index

                        for_objects "TRX_PROPERTIES"
                            begin
                                define "trxPropertiesDn" $current_dist_name

                                if ( param_exist trxPropertiesDn "objectName" )
                                    begin
                                        define "objectName" get_param trxPropertiesDn "objectName"
                                        if ( objectName ) == antenna
                                            begin
                                                define "objectState" ""
                                                if ( param_exist trxPropertiesDn "objectState" )
                                                    begin
                                                        define "objectState" get_param trxPropertiesDn "objectState"
                                                        if ( objectState ) == "Enabled"
                                                            begin
                                                                define "removeChannels" "true"
                                                            end
                                                    end
                                            end
                                    end

                            end
                    end

                if ( removeChannels ) == "true"
                    begin

                        for_objects "MRBTS/MNL/MNLENT/CELLMAPPING/LCELC/CHANNELGROUP/CHANNEL"
                            begin
                                if ( $current_LCELC ) == localSectorId
                                    del_object $current_dist_name
                            end

                        for_objects "MRBTS/MNL/MNLENT/CELLMAPPING/LCELC/CHANNELGROUP"
                            begin
                                if ( $current_LCELC ) == localSectorId
                                    del_object $current_dist_name
                            end

                    end
            end
    end

#Needed because LCELC-id changes in CELLMAPPING: SCFD08-MNL-001_000.txt

define "mrbtsId" 1
for_objects "BCF/MRBTS/BTSSCC/BTSSCG/BTSNE"
    begin
        if ( param_exist $current_dist_name "installationNotes" )
            begin
                define "value" get_param $current_dist_name "installationNotes"
                define "tempValue" replace value "(\D+)" "" flags "g"
                if ( tempValue ) != "" && ( tempValue >= 0 ) && ( tempValue <= 1048575 )
                    begin
                        define "mrbtsId" tempValue
                    end
            end
    end

define "lcelcToCellMappingDn" "MRBTS-" JOIN mrbtsId
add_list lcelcToCellMappingDn "lcelcToCellMapping"

define "indexOnList" 0
define "cellmapping_lcelcId" 0

for_objects "BCF/MRBTS/BTSSCC/LCELC"
    begin
        define "cellmapping_lcelcId" cellmapping_lcelcId + 1

        add_complex_param lcelcToCellMappingDn "lcelcToCellMapping." JOIN indexOnList
        add_param lcelcToCellMappingDn "lcelcToCellMapping." JOIN indexOnList  JOIN ".lcelcId" $current_LCELC
        add_param lcelcToCellMappingDn "lcelcToCellMapping." JOIN indexOnList  JOIN ".cellmapping_lcelcId" cellmapping_lcelcId

        define "indexOnList" indexOnList + 1
    end

#temporary mapping - defines channelGroupId for TRXs in a sector based on arfn values

for_objects "SECTOR_PROPERTIES"
    begin
        define "channelGroupId" "1"
        define "sectorPropertiesDn" $current_dist_name

        for_list sectorPropertiesDn "includedObjects"
            begin
                define "arfn" ""
                define "antenna" get_param sectorPropertiesDn "includedObjects." JOIN $current_index

                for_objects "TRX_PROPERTIES"
                    begin
                        define "trxPropertiesDn" $current_dist_name
                        if ( param_exist trxPropertiesDn "objectName" )
                            begin
                                define "trx" get_param trxPropertiesDn "objectName"
                                if ( trx ) == antenna
                                    begin
                                        if ( param_exist trxPropertiesDn "arfn" )
                                            begin
                                                define "arfn" get_param trxPropertiesDn "arfn"

                                                if not ( param_exist sectorPropertiesDn "channelGroupsList" )
                                                    begin
                                                        add_list sectorPropertiesDn "channelGroupsList"
                                                        add_complex_param sectorPropertiesDn "channelGroupsList.0"
                                                        add_param sectorPropertiesDn "channelGroupsList.0.arfn" arfn
                                                        add_param sectorPropertiesDn "channelGroupsList.0.antenna" trx
                                                        add_param sectorPropertiesDn "channelGroupsList.0.channelGroupId" channelGroupId
                                                    end
                                                else
                                                    begin
                                                        define "index" "0"
                                                        define "foundOnList" "false"
                                                        for_list sectorPropertiesDn "channelGroupsList"
                                                            begin
                                                                define "arfnOnList" get_param sectorPropertiesDn "channelGroupsList." JOIN $current_index JOIN ".arfn"
                                                                if ( arfnOnList ) == arfn
                                                                    begin
                                                                        if ( param_exist sectorPropertiesDn "channelGroupsList." JOIN $current_index JOIN ".channelGroupId" )
                                                                            begin
                                                                                define "channelGroupId" get_param sectorPropertiesDn "channelGroupsList." JOIN $current_index JOIN ".channelGroupId"
                                                                                define "foundOnList" "true"
                                                                            end
                                                                    end
                                                                define "index" index + 1
                                                            end

                                                        if ( foundOnList ) == "false"
                                                            begin
                                                                define "channelGroupId" channelGroupId + 1
                                                            end

                                                        add_complex_param sectorPropertiesDn "channelGroupsList." JOIN index
                                                        add_param sectorPropertiesDn "channelGroupsList." JOIN index JOIN ".arfn" arfn
                                                        add_param sectorPropertiesDn "channelGroupsList." JOIN index JOIN ".antenna" trx
                                                        add_param sectorPropertiesDn "channelGroupsList." JOIN index JOIN ".channelGroupId" channelGroupId
                                                    end
                                            end
                                    end
                            end
                    end
            end
    end

for_objects "TRX_PROPERTIES"
    begin
        define "trxPropertiesDn" $current_dist_name

        define "arfn" ""
        if ( param_exist trxPropertiesDn "arfn" )
            define "arfn" get_param trxPropertiesDn "arfn"

        define "objectState" ""
        if ( param_exist trxPropertiesDn "objectState" )
            define "objectState" get_param trxPropertiesDn "objectState"

        if ( objectState ) == "Enabled"
            begin
                define "objectName" ""
                if ( param_exist trxPropertiesDn "objectName" )
                    define "objectName" get_param trxPropertiesDn "objectName"

                for_objects "SECTOR_PROPERTIES"
                    begin
                        define "sectorPropertiesDn" $current_dist_name

                        for_list sectorPropertiesDn "includedObjects"
                            begin
                                define "channelId" "1"
                                define "channelGroupId" "1"

                                define "index" $current_index
                                define "includedObjectsWithIndex" "includedObjects." JOIN index
                                define "antenna" get_param sectorPropertiesDn includedObjectsWithIndex

                                if ( antenna ) == objectName
                                    begin
                                        if ( param_exist sectorPropertiesDn "localSectorId" )
                                            begin
                                                define "localSectorId" get_param sectorPropertiesDn "localSectorId"

                                                define "cellmapping_lcelcId" "1"
                                                for_list lcelcToCellMappingDn "lcelcToCellMapping"            
                                                    begin
                                                        define "lcelcToCellMappingPrefix" "lcelcToCellMapping." JOIN $current_index
                                                        if ( param_exist lcelcToCellMappingDn lcelcToCellMappingPrefix JOIN ".lcelcId" ) &&
                                                            ( param_exist lcelcToCellMappingDn lcelcToCellMappingPrefix JOIN ".cellmapping_lcelcId" )
                                                            begin
                                                                if ( get_param lcelcToCellMappingDn lcelcToCellMappingPrefix JOIN ".lcelcId" ) == localSectorId
                                                                    begin
                                                                        define "cellmapping_lcelcId" get_param lcelcToCellMappingDn lcelcToCellMappingPrefix JOIN ".cellmapping_lcelcId"
                                                                    end
                                                            end
                                                    end

                                                for_list trxPropertiesDn "associatedAntennas"
                                                    begin
                                                        define "ind" $current_index
                                                        define "associatedAntennasWithIndex" "associatedAntennas." JOIN ind JOIN "."
                                                        if ( param_exist trxPropertiesDn associatedAntennasWithIndex JOIN "direction" ) &&
                                                            ( param_exist trxPropertiesDn associatedAntennasWithIndex JOIN "radio" )
                                                            begin
                                                                define "direction" get_param trxPropertiesDn associatedAntennasWithIndex JOIN "direction"

                                                                define "radio" get_param trxPropertiesDn associatedAntennasWithIndex JOIN "radio"
                                                                define "radioCopyForAnt" radio
                                                                define "antIdFromTxProperties" replace radioCopyForAnt "(.*(ANT\d).*)" "$2" flags "g" 

                                                                define "radioCopyForLinkId" radio
                                                                define "value" replace radioCopyForLinkId "(.*(\d\.\d).*)" "$2" flags "g" 
                                                                define "linkIdFromTxProperties" replace value "((\d).*)" "$2" flags "g" 

                                                                define "radioCopyForPosInChain" radio
                                                                define "temp" replace radioCopyForPosInChain "(.*(\d\.\d).*)" "$2" flags "g" 
                                                                define "positionInChainFromTxProperties" replace temp "(.*(\d))" "$2" flags "g" 

                                                                for_objects "BCF/MRBTS/RMOD"
                                                                    begin
                                                                        define "rmodDn" $current_dist_name
                                                                        define "rmodId" $current_RMOD

                                                                        define "linkId" ""
                                                                        define "positionInChain" ""
                                                                        define "antlId" ""

                                                                        for_list rmodDn "connectionList"
                                                                            begin
                                                                                if ( param_exist rmodDn "connectionList." JOIN $current_index JOIN ".linkId" )
                                                                                    define "linkId" get_param rmodDn "connectionList." JOIN $current_index JOIN ".linkId"

                                                                                if ( param_exist rmodDn "connectionList." JOIN $current_index JOIN ".positionInChain" )
                                                                                    define "positionInChain" get_param rmodDn "connectionList." JOIN $current_index JOIN ".positionInChain"

                                                                                if ( linkId ) == linkIdFromTxProperties && ( positionInChain ) == positionInChainFromTxProperties
                                                                                    begin
                                                                                        for_objects "BCF/MRBTS/ANTL"    
                                                                                            begin
                                                                                                if ( param_exist $current_dist_name "rModId" ) && ( param_exist $current_dist_name "antId" )
                                                                                                    begin
                                                                                                        define "rModId" get_param $current_dist_name "rModId"
                                                                                                        define "antId" get_param $current_dist_name "antId"
                                                                                                        if ( rModId ) == rmodId && ( antId ) == antIdFromTxProperties
                                                                                                            begin
                                                                                                                define "antlId" $current_ANTL
                                                                                                            end
                                                                                                    end
                                                                                            end

                                                                                        for_objects "MRBTS/EQM/APEQM/RMOD/ANTL"
                                                                                            begin
                                                                                                define "antlDn" $current_dist_name
                                                                                                if ( $current_RMOD ) == rmodId && ( $current_ANTL ) == antlId
                                                                                                    begin
                                                                                                        define "antlDN" antlDn
                                                                                                    end
                                                                                            end
                                                                                    end
                                                                            end
                                                                    end

                                                                for_list sectorPropertiesDn "channelGroupsList"
                                                                    begin
                                                                        define "channelGroupsListWithIndex" "channelGroupsList." JOIN $current_index

                                                                        if ( param_exist sectorPropertiesDn channelGroupsListWithIndex JOIN ".arfn" )
                                                                            begin
                                                                                define "arfnFromList" get_param sectorPropertiesDn channelGroupsListWithIndex JOIN ".arfn"
                                                                                define "antennaFromList" get_param sectorPropertiesDn channelGroupsListWithIndex JOIN ".antenna"

                                                                                if ( arfn ) == arfnFromList && ( antenna ) == antennaFromList
                                                                                    begin
                                                                                        if ( param_exist sectorPropertiesDn channelGroupsListWithIndex JOIN ".channelGroupId" )
                                                                                            begin
                                                                                                define "channelGroupId" get_param sectorPropertiesDn channelGroupsListWithIndex JOIN ".channelGroupId"
                                                                                                define "channelId" "1"

                                                                                                for_objects "MRBTS/MNL/MNLENT/CELLMAPPING/LCELC/CHANNELGROUP/CHANNEL"
                                                                                                    begin
                                                                                                        if ( $current_LCELC ) == cellmapping_lcelcId && ( $current_CHANNELGROUP ) == channelGroupId
                                                                                                            begin
                                                                                                                define "channelId" channelId + 1
                                                                                                            end
                                                                                                    end

                                                                                                for_objects "MRBTS/MNL/MNLENT/CELLMAPPING/LCELC"
                                                                                                    begin

                                                                                                        if ( $current_LCELC ) == cellmapping_lcelcId
                                                                                                            begin
                                                                                                                define "cellMappingLCelcDn" $current_dist_name
                                                                                                                define "channelGroupDn" cellMappingLCelcDn JOIN "/CHANNELGROUP-" JOIN channelGroupId
                                                                                                                define "channelDn" channelGroupDn JOIN "/CHANNEL-" JOIN channelId

                                                                                                                if ( objects_count channelGroupDn ) == 0
                                                                                                                    begin
                                                                                                                        add_object channelGroupDn
                                                                                                                        set_operation channelGroupDn "create"
                                                                                                                    end

                                                                                                                if ( direction ) == "Tx/Rx"
                                                                                                                    begin
                                                                                                                        if ( objects_count channelDn ) == 0
                                                                                                                            begin
                                                                                                                                add_object channelDn
                                                                                                                                set_operation channelDn "create"
                                                                                                                                add_param channelDn "antlDN" antlDN
                                                                                                                                add_param channelDn "direction" "TX"
                                                                                                                            end

                                                                                                                        define "nextChannelId" channelId + 1
                                                                                                                        define "nextChannelDn" channelGroupDn JOIN "/CHANNEL-" JOIN nextChannelId

                                                                                                                        if ( objects_count nextChannelDn ) == 0
                                                                                                                            begin
                                                                                                                                add_object nextChannelDn
                                                                                                                                set_operation nextChannelDn "create"
                                                                                                                                add_param nextChannelDn "antlDN" antlDN
                                                                                                                                add_param nextChannelDn "direction" "RX"
                                                                                                                            end

                                                                                                                        define "channelId" nextChannelId + 1
                                                                                                                    end

                                                                                                                else if ( direction ) == "RxDiv"
                                                                                                                    begin
                                                                                                                        if ( objects_count channelDn ) == 0
                                                                                                                            begin
                                                                                                                                add_object channelDn
                                                                                                                                set_operation channelDn "create"
                                                                                                                                add_param channelDn "antlDN" antlDN
                                                                                                                                add_param channelDn "direction" "RX"
                                                                                                                            end

                                                                                                                        define "channelId" channelId + 1
                                                                                                                    end
                                                                                                            end

                                                                                                    end

                                                                                            end
                                                                                    end
                                                                            end
                                                                    end

                                                            end
                                                    end

                                            end
                                    end
                            end
                    end
            end
    end

for_objects "IP_ADDRESSES"
	begin
		define "cuPlaneAddress" ""
		if ( param_exist $current_dist_name "BTSCUPlane_Address" ) define "cuPlaneAddress" get_param $current_dist_name "BTSCUPlane_Address"
		for_objects "MRBTS/TNLSVC/TNL/IPNO/IPIF/IPADDRESSV4"
			begin
				define "mplaneIpAddr" ""
				if ( param_exist $current_dist_name "localIpAddr" ) define "mplaneIpAddr" get_param $current_dist_name "localIpAddr"
			end
		if ( cuPlaneAddress != mplaneIpAddr )
			begin
				define "ipAddObjNum" objects_count "MRBTS/TNLSVC/TNL/IPNO/IPIF/IPADDRESSV4"
				for_objects "MRBTS/TNLSVC/TNL/IPNO/IPIF"
					begin
						define "newIpAddr" $current_dist_name JOIN "/IPADDRESSV4-" JOIN ( ipAddObjNum + 1 )
						add_object newIpAddr
						set_operation newIpAddr "create"
					end
		
				for_objects "MRBTS/TNLSVC/TNL/IPNO/IPIF/IPADDRESSV4"
					begin
						if ( not  param_exist $current_dist_name "localIpAddr" )
							begin
								add_param $current_dist_name "localIpAddr" cuPlaneAddress
								add_param $current_dist_name "ipAddressAllocationMethod" "MANUAL"
								add_param $current_dist_name "localIpPrefixLength" localIpPrefix
							end
						else 
							define "localIpPrefix" get_param $current_dist_name "localIpPrefixLength"		
					end
			end	
	end

	
for_objects "IPADDRESSV4"
	begin
		set_adaptation $current_dist_name "com.nokia.srbts.tnl"
	end

for_objects "MRBTS/MNL/MNLENT/CELLMAPPING/LCELC/CHANNELGROUP"
    begin
        set_adaptation $current_dist_name "com.nokia.srbts.mnl"
    end

for_objects "MRBTS/MNL/MNLENT/CELLMAPPING/LCELC/CHANNELGROUP/CHANNEL"
    begin
        set_adaptation $current_dist_name "com.nokia.srbts.mnl"
    end

del_param lcelcToCellMappingDn "lcelcToCellMapping"

for_objects "SECTOR_PROPERTIES"
    begin
        del_object $current_dist_name
    end

for_objects "TRX_PROPERTIES"
    begin
        del_object $current_dist_name
    end
	
for_objects "IP_ADDRESSES"
    begin
        del_object $current_dist_name
    end
