#!/usr/bin/python3
###############################################################################
# IPFire.org    - An Open Source Firewall Solution                            #
# Copyright (C) - IPFire Development Team <info@ipfire.org>                   #
###############################################################################

# When "make oldconfig" is run for an architecture that is not the one of the
# host, the compiler cannot tell which features the target compiler would
# support and Kconfig drops every option that depends on them.
#
# This script takes the configuration as it is currently stored, the
# configuration that has been passed to "make oldconfig" and its result. It
# prints the result and puts back all options that have been stored before
# and have been dropped. For the same reason, options that have been stored
# before keep their value if "make oldconfig" has changed it, because a choice
# falls back to another option if the selected one is not available.
#
# Optionally, the stored configuration and the result of the architecture of
# the host can be passed. Whatever has been dropped there is really gone and
# will not be put back, and whatever has got the same value there is correct.

import re
import sys

def parse(line):
	m = re.match("^# (.*) is not set$", line)
	if m:
		return m.group(1)

	m = re.match("^(.*)=(.*)$", line)
	if m:
		return m.group(1)

def read(filename):
	with open(filename) as f:
		return [line.rstrip() for line in f.readlines()]

def options(lines):
	return dict((option, line) for option, line in ((parse(line), line) for line in lines) if option)

stored, config_in, config_out = sys.argv[1:4]

# Collect all options that have been stored
stored = options(read(stored))

# Read the result
lines = read(config_out)
result = options(lines)

# Collect all options that have been dropped on the host
native = {}
gone   = set()
if len(sys.argv) > 4:
	native_stored, native_out = sys.argv[4:6]

	native = options(read(native_out))
	gone   = set(options(read(native_stored))) - set(native)

# Find all options that have been dropped and remember after which option
# they have to be put back, so that they stay in their section
restore = {}
revert  = {}
anchor  = None

for line in read(config_in):
	option = parse(line)
	if not option:
		continue

	if option in result:
		anchor = option

		# Keep the value unless it has changed on the host in the same way
		if option in stored and not result[option] in (line, native.get(option)):
			revert[option] = line

		continue

	# Ignore all options CONFIG_HAVE_ because we cannot
	# set them anyway.
	if option.startswith("CONFIG_HAVE_"):
		continue

	# Options that have not been stored before do not exist
	# on this architecture
	if not option in stored:
		continue

	# Ignore anything that is gone on the host, too
	if option in gone:
		continue

	restore.setdefault(anchor, []).append(line)

for line in lines:
	print(revert.get(parse(line), line))

	for l in restore.pop(parse(line), []):
		print(l)

# Print anything that had nothing to be attached to
for anchor in restore:
	for l in restore[anchor]:
		print(l)
