# CONFIG file is inserted above during installation

# clean out non-existing groups, badly formed lines etc.

if [ ! -s ${2-$ACTIVE} ] ; then
	echo "$0: Cannot locate active file ${2-$ACTIVE}"
	exit 3
fi

echo Working on your nn record file ... do not run nn until completed

cd
cd .nn
rm -f rc.bak1

trap 'mv rc.bak1 rc ; echo "No changes" ; exit 0' 1 2 3 13 14 15

mv rc rc.bak1

{
	cat ${2-$ACTIVE}
	echo EOA
	cat rc.bak1

} |
awk '
BEGIN{
	act=1
}
$1 == "EOA" {
	act=0
	next
}
NF==4 {
	if (act) { 
		X[$1] = 1
		L[$1] = $2+0 
		if (L[$1] == 0 || $3+0 == 0) F[$1] = 0; else F[$1] = $3-1
	}
	next
}
$1 ~ /^#/ {
	print $0
	next
}
NF == 3 && ($1 == "!" || $1 == "+" || $1 == "=") {
	if (X[$3] != 1) next
	S[$3] = $1
	if (L[$3] >= $2+0)
		N[$3] = $2+0
	else
		N[$3] = F[$3]
	next
}
END {
	for (g in N) printf("%s %06d %s\n", S[g], N[g], g)
}
' |
sort +2 > rc

exit 0
