#!/usr/bin/awk -f

# Always output the os_prefix settings first
BEGIN {
	print "[all]";
	print "os_prefix=current/";
	print "";
	print "[tryboot]";
	print "os_prefix=new/";
	print "";
}

BEGIN {
	section = "[tryboot]";
	first_line = 1;
	watchdog_enabled = 0;
}

first_line==1 && /[^ ]/ {
	first_line = 0;
	if ($0 != "[all]") print "[all]";
	section = "[all]";
}

/^\[.*\]/ {
	section = $0;
}

/^dtparam=watchdog=on/ && section == "[all]" {
	watchdog_enabled = 1;
}

{ print; }

# We need to enable the watchdog in an [all] section after the os_prefix
# settings; if at the end of the file we're not in an [all] section, then
# make one
END {
	if (!watchdog_enabled) {
		if (section != "[all]") print "[all]";
		print "dtparam=watchdog=on";
	}
}
