#!/bin/bash
# This script is used to make the configure script: config-mnu.

MNU_CONFIG=.config
FEATURE_IN=configs/feature.in
CONFIG_MNU=config-mnu

prefix=/usr/local
CC=gcc
TARGET=i386-linux
HOST=i386-linux
CFLAGS=
LDFLAGS=

CONFIGURE_HELP=.config_help

if [ ! -f configure ]; then
  echo "configure not exist, execute autogen to make it"
  ./autogen.sh
fi

./configure --help > $CONFIGURE_HELP

echo "#!/bin/sh" > $CONFIG_MNU
echo "# Automatically generated by make menuconfig." >> $CONFIG_MNU
echo >> $CONFIG_MNU
echo "rm config.cache config.status -f" >> $CONFIG_MNU
echo >> $CONFIG_MNU

SAVEIFS=$IFS
IFS==

function check_def_yes () {
  if grep "$1" $CONFIGURE_HELP | grep "<default=yes>" > /dev/null; then
     return 0
  else
     return -1
  fi
}

function check_def_no () {
  if grep "$1" $CONFIGURE_HELP | grep "<default=no>" > /dev/null; then
     return 0
  else
     return -1
  fi
}

function check_exist () {
  if grep "$1" $MNU_CONFIG > /dev/null; then
     return 0
  else
     return -1
  fi
}

function check_value () {
if [ -s $MNU_CONFIG ]; then
  while read var_config value
  do

    if [ "$var_config" = "$1" ]; then
       if [ "$value" = "y" ]; then
         return 0
       else
         return -1
       fi
    fi

  done < $MNU_CONFIG
  return -1
fi
}

function get_value () {
if [ -s $2 ]; then
  while read var_config nowvalue

  do

    if [ "$var_config" = "$1" ]; then
       return 0
    fi

  done < $2
  return -1

fi
}

function get_mnu_value () {
  if get_value $1 $MNU_CONFIG; then
    return 0
  else
    return -1
  fi
}


if get_mnu_value "PREFIX"; then
    echo ""
    prefix=$nowvalue
    echo "install prefix = $prefix"
fi
if get_mnu_value "CC"; then
    CC=$nowvalue
    echo "compiler = $CC"
fi
if get_mnu_value "TARGET"; then
    TARGET=$nowvalue
    echo "target = $TARGET"
fi
if get_mnu_value "HOST"; then
    HOST=$nowvalue
    echo "host = $HOST"
fi


# set CFLAGS and LDFLAGS for uClinux
# -----------------------------------------------------------------
if check_value "CONFIG_OS_UCLINUX"; then

    CFLAGS="-Os -D__uClinux__ -fno-builtin"

if get_mnu_value "UC_KERNEL_HEADER"; then
    CFLAGS="$CFLAGS -I$nowvalue"
fi
if get_mnu_value "UCLIBC_HEADER"; then
    CFLAGS="$CFLAGS -I$nowvalue -I$nowvalue/../"
fi

    LDFLAGS="-Wl,-elf2flt -Wl,-move-rodata"
if get_mnu_value "UCLIBC_LIB"; then
    LDFLAGS="$LDFLAGS -L$nowvalue -L$nowvalue/../"
fi

if check_value "CONFIG_COMPILER_M68K_ELF"; then

if check_value "CONFIG_TARGET_M68K_XCOPILOT"; then
    CFLAGS="$CFLAGS -m68000 -mid-shared-library -mshared-library-id=0"
    LDFLAGS="$LDFLAGS -Wl,-shared-lib-id,0"
fi
if check_value "CONFIG_TARGET_M68K_68EZ328"; then
    CFLAGS="$CFLAGS -m68000 -mid-shared-library -mshared-library-id=0"
    LDFLAGS="$LDFLAGS -Wl,-shared-lib-id,0"
fi
if check_value "CONFIG_TARGET_M68K_CF5200"; then
    CFLAGS="$CFLAGS -m5200 -Wa,-S -Wa,-m5200 -msep-data"
fi
if check_value "CONFIG_TARGET_M68K_CF5307"; then
    CFLAGS="$CFLAGS -m5307 -Wa,-S -Wa,-m5307 -msep-data"
fi


if get_mnu_value "UCLIBC_LIB"; then
    LDFLAGS="$LDFLAGS -Wl,-R,$nowvalue/../libc.gdb"
fi

fi

if check_value "CONFIG_COMPILER_ARM_ELF"; then
    CFLAGS="$CFLAGS -D__PIC__ -fpic -msingle-pic-base"
fi

    LDFLAGS="$LDFLAGS -lc"

    echo "CFLAGS = $CFLAGS"
    echo "LDFLAGS = $LDFLAGS"

fi
# uClinux -----------------------------------------------------------

# set CFLAGS and LDFLAGS for eCos
# -----------------------------------------------------------------
if check_value "CONFIG_OS_ECOS"; then
    CFLAGS="$CFLAGS -D__ECOS -D__NOUNIX__ -DUNIX=1"
    LDFLAGS="$LDFLAGS -nostdlib"
fi
# -----------------------------------------------------------------

if get_mnu_value "ADD_CFLAGS"; then
    CFLAGS="$CFLAGS $nowvalue"
fi
if get_mnu_value "ADD_LDFLAGS"; then
    LDFLAGS="$LDFLAGS $nowvalue"
fi


    echo "CC=$CC \\" >> $CONFIG_MNU
    echo "CFLAGS=\"$CFLAGS\" \\" >> $CONFIG_MNU
    echo "LDFLAGS=\"$LDFLAGS\" \\" >> $CONFIG_MNU
    echo "./configure \\" >> $CONFIG_MNU
    echo "    --prefix=$prefix \\" >> $CONFIG_MNU
    echo "    --target=$TARGET \\" >> $CONFIG_MNU
    echo "    --host=$HOST \\" >> $CONFIG_MNU

if check_value "CONFIG_NO_SHARE"; then
    echo "    --disable-shared \\" >> $CONFIG_MNU
fi

if check_value "CONFIG_OS_LINUX"; then
    echo "    --with-osname=linux\\" >> $CONFIG_MNU
fi

if check_value "CONFIG_OS_UCLINUX"; then
    echo "    --with-osname=uclinux\\" >> $CONFIG_MNU
fi

if check_value "CONFIG_OS_ECOS"; then
    echo "    --with-osname=ecos\\" >> $CONFIG_MNU
fi

if check_value "CONFIG_OS_UCOSII"; then
    echo "    --with-osname=ucos2\\" >> $CONFIG_MNU
fi

if check_value "CONFIG_LF_FASHION"; then
    echo "    --enable-fashionlf\\" >> $CONFIG_MNU
fi

if check_value "CONFIG_LF_FLAT"; then
    echo "    --enable-flatlf\\" >> $CONFIG_MNU
fi

if check_value "CONFIG_LF_TINY"; then
    echo "    --enable-tinylf\\" >> $CONFIG_MNU
fi

if [ -s $FEATURE_IN ]; then
  while read var_config feature
  do

    if [ "$feature" != "" ]; then
    if check_exist $var_config; then

    if check_value $var_config; then
      if check_def_no $feature; then
      echo "    --enable-$feature \\" >> $CONFIG_MNU
      fi
    else
      if check_def_yes $feature; then
      echo "    --disable-$feature \\" >> $CONFIG_MNU
      fi
    fi

    fi
    fi

  done < $FEATURE_IN
fi

echo "    --build=i386-linux" >> $CONFIG_MNU 


rm $CONFIGURE_HELP
chmod +x $CONFIG_MNU

IFS=$SAVEIFS

