#!/bin/sh -
# Copyright (C) 1996 Robert de Bath <rdebath@cix.compulink.co.uk>
# This file is part of the Linux-8086 C library and is distributed
# under the GNU Library General Public License.

main()
{
   rm -f .config.tmp
   ALLON=yes

   if [ "$ALLON" = yes -a -f .config.lst ]
   then grep '^[^:]*:+:' .config.lst > .config.tmp
        [ -s .config.tmp ] && ALLON=no
   fi

   if [ "$ALLON" = yes -a -f Config.dflt ]
   then grep '^[^:]*:+:' Config.dflt > .config.tmp
        [ -s .config.tmp ] && {
	   ALLON=no
	   grep -q '^kinclude:' .config.tmp >/dev/null 2>&1 || {
	      [ -d "$ELKSSRC/include" ] ||
	         echo 'kinclude:+:' >> .config.tmp
	   }
        }
   fi

   egrep -v '^#|^$' /dev/null */[Cc]onfig | \
   sed -e 's./.:.' -e 's/[	 ]*:[	 ]*/:/g' >> .config.tmp 2>/dev/null
   ls */Makefile | sed 's-/Makefile-:+:+-' >> .config.tmp
   sort .config.tmp > .config.lst

   unset_dups

   if [ ! -s .config.lst ]
   then echo 'No configuration options'
        exit 0
   fi

   CHANGED=0
   RUNNING=1
   [ "$DIST" != "" -o ! -t 1 -o ! -t 0 ] && {
      RUNNING=0
      echo 'Using default configuration'
   }
   while [ $RUNNING = 1 ]
   do
      display
      echo 
      echon 'Select config option to flip [or quit] >'
      read n
      v=""
      case "$n" in
      [qQ]* ) RUNNING=0
              ;;
      [0-9] )      v=$n ;;
      [0-9][0-9] ) v=$n ;;
      * )     echo '\007'
              ;;
      esac

      if [ "$v" != "" ]
      then set_option $v
      fi
   done
   
   if [ "$CHANGED" = 1 -a \( -f libc.a -o -f crt0.o \) ]
   then echo '
    You should now run a "make clean" to clean out the libc.a
'
        exit 1
   fi

   exit 0
}

display()
{
   clear 
   awk -F: < .config.lst '{
      if( $3 == "+" ) next;
      if( $2 == "+" )  { flags[$1] = 1; next; }

      printf("%2d) ", ++count);
      if( $1 in flags ) printf("(ON)  ");
      else              printf("(OFF) ");
      if( $2 == "Config" ) printf("  "); else printf("* ");
      printf("%s\n", $4);
   }'
}

unset_dups()
{
   awk -F: < .config.lst '{
      if( $2 == "+" && $3 == "+") { if( noco[$1] != 1 ) noco[$1] = 2; next; }
      if( $2 == "+" )  { flags[$1] = 1; next; }
      if( "'$ALLON'" == "yes" && $2 == "Config" ) flags[$1] = 1;

      if( $1 in flags )
      {
         if( $3 in gottype )
	    ;
	 else
	 {
            printf("%s:+:\n", $1);
            gottype[$3] = 1;
	 }
      }
      noco[$1] = 1;
      printf("%s\n", $0);
      } END {
      for(i in noco)
         if( noco[i] == 2 )
	     printf("%s:+:+\n", i);
   }' | sort > .config.tmp
   ALLON=no
   mv -f .config.tmp .config.lst
}

set_option()
{
   rm -f .config.tmp1
   awk -F: < .config.lst '{
      if( $2 == "+" && $3 == "+" ) { print $0; next; }
      if( $2 == "+" )  { flags[$1] = 1; next; }

      if( ++cnt == '$1' )
      {
         if( $1 in flags )
	    ;
	 else
            printf("%s:+:\n", $1) > ".config.tmp1";
         printf("%s\n", $0)       > ".config.tmp1";
      }
      else
      {
         if( $1 in flags )
            printf("%s:+:\n", $1);
         printf("%s\n", $0);
      }
   }' > .config.tmp2
   if [ -f .config.tmp1 ]
   then CHANGED=1
   else echo 'Cannot change that option!'
        sleep 2
   fi
   cat .config.tmp[12] > .config.lst
   rm .config.tmp[12]
   unset_dups
}

echon() { 
   [ "$ECHON" = "" ] && {
      if echo -n | grep -e -n >/dev/null
      then ECHON="echo "; ECHOT='\c'
      else ECHON="echo -n"; ECHOT=''
      fi
   }
   $ECHON "$@""$ECHOT"
}

main
