#!/bin/sh

# miniconf.sh copyright 2005 by Rob Landley <rob@landley.net>
# Licensed under the GNU General Public License version 2.

if [ $# -ne 1 ] || [ ! -f "$1" ]
then
  echo "Usage: miniconf.sh configfile" 
fi

if [ "$1" == ".config" ]
then
  echo "It overwrites .config, rename it and try again."
  exit 1
fi

cp $1 mini.conf
echo "Calculating mini.conf..."

LENGTH=`cat $1 | wc -l`

# Loop through all lines in the file 
I=1
while true
do
  if [ $I -gt $LENGTH ]
  then
    exit
  fi
  sed -n "${I}!p" mini.conf > .config.test
  # Do a config with this file
  make allnoconfig KCONFIG_ALLCONFIG=.config.test > /dev/null

  # Compare, skipping first 5 lines which contain changeable date.
  D=`diff .config $1 | wc -l`
  if [ $D -eq 4 ]
  then
    mv .config.test mini.conf
    LENGTH=$[$LENGTH-1]
  else
    I=$[$I + 1]
  fi
  echo -n -e $I/$LENGTH lines `cat mini.conf | wc -c` bytes "\r"
done
echo
