#!/usr/bin/env /bin/bash
function remove_first()
{
	entry=${2/CONFIG_}
	start=$(grep -n "config $1" $2)
	start=${start/:*}
	[ "$start" ] || return 1
	stop=$(grep -n "^[a-z#].*" $2 | 
		while read line rest; do
			line=${line/:*}
			(( $line > $start )) && echo $line || continue
			break
		done)
	total=$(wc -l $2)
	total=${total/ *}

	[ "$total" ] || return 1
	[ "$stop" ] || stop=$total

	head -n $((start-1)) $2
	tail -n $((total-stop+1)) $2

	return 0
}

cp $2 .tmp-remove-kconfig
while remove_first $1 .tmp-remove-kconfig > .tmp-remove-kconfig.2; do
	mv .tmp-remove-kconfig.2 .tmp-remove-kconfig
done 

cat .tmp-remove-kconfig
rm .tmp-remove-kconfig

