#! /bin/sh
############################################################################
# Configure
#
#   Copyright (C) 2008, 2011 Gregory Nutt. All rights reserved.
#   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
#    used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################

Usage="                                                \n
+----------------                                      \n
| USAGE:                                               \n
|   ./Configure [--debug] [--help]                     \n
|                                                      \n
| OPTIONS:                                             \n
|   --debug                                            \n
|       Prints what script is doing                    \n
|                                                      \n
|   --reconfig                                         \n
|       Just regenerate the Make.config file           \n
|                                                      \n
|   --help                                             \n
|       Prints this block.                             \n
+----------------                                      \n
"

# readans prompt default
#
function readans () {
    echo -n "$1 ($2): "
    IFS='@' read ans || exit 1
    [ -z "$ans" ] && ans=$2
}

# readyn prompt default
#
function readyn () {
    while :; do
	readans "$1 [Y/N]" $2
	case "$ans" in
	[yY] | [yY]es )
	    ans=y
	    break ;;
	[nN] | [nN]o )
	    ans=n
	    break ;;
	* )
	    echo "Please answer Y or N"
	    ;;
	esac
    done
}

# Process command line arguments
#
REGEN="NO"
while [ $# -gt 0 ] ; do
   case "$1" in
       --debug )
	   set -x
	   Pause="read press return to continue"
	   ;;
       --regen )
	   REGEN="YES"
	   ;;
       *)
	   echo $Usage
	   exit -1
	   ;;
   esac
   shift
done

# Setup configurations files
#
CONFIGFILE=./.config
MAKECONFIG=Make.config
TMPCONFIGFILE=TMP.config
TMPMAKECONFIG=TMP.Make.config

# Remove temporary configuration files in case we are restarting
# after control-C
#
rm -f $TMPCONFIGFILE $TMPMAKECONFIG

# Get information about the configuration settings
#
source ./config.info

# Set up default values for all configuration settings
#
for i in $CONFIGS
do
  config_name=$(echo $i|cut -d':' -f1)
  default_value=$(echo $i|cut -d':' -f2)
  eval $config_name=$default_value
done

# If we have been previously configured, then there should be both
# .config and Make.config files.  Source the .config file, overwriting
# the defaults that we set up above.
#
if [ -e ${CONFIGFILE} ] ; then
    source ${CONFIGFILE}
else
    # We can't regenerate the Make.config file if there is no
    # .config file
    #
    REGEN="NO"
fi

# Output the new Config file and Makefile fragment headers
#
SEPARATOR="# ----------------------------------------------------------------------"

if [ "${REGEN}" != "YES" ]; then
    echo "#!/bin/sh" >${TMPCONFIGFILE}
    echo "#" >>${TMPCONFIGFILE}
    echo "# Auto generated by Configure.  Do not edit" >>${TMPCONFIGFILE}
    echo "#" >>${TMPCONFIGFILE}
fi

echo $SEPARATOR >${TMPMAKECONFIG}
echo "# Make.config" >>${TMPMAKECONFIG}
echo "#" >>${TMPMAKECONFIG}
echo "# This file controls the configuration of the compiler" >>${TMPMAKECONFIG}
echo "# Auto generated by Configure.  Do not edit" >>${TMPMAKECONFIG}
echo $SEPARATOR >>${TMPMAKECONFIG}
echo "#"  >>${TMPMAKECONFIG}

# Prompt for the setting of each configuration variable
for i in $CONFIGS
do
  config_name=$(echo $i|cut -d':' -f1)
  eval desc=\$${config_name}_INFO
  eval config_value=\$$config_name

  if [ "${REGEN}" != "YES" ]; then
      readyn "$desc" $config_value
      eval config_value=$ans
      echo "$config_name=$config_value" >>${TMPCONFIGFILE}
  fi

  echo $SEPARATOR >>${TMPMAKECONFIG}
  echo "# $desc" >>${TMPMAKECONFIG}
  echo $SEPARATOR >>${TMPMAKECONFIG}
  echo "$config_name	= $config_value" >>${TMPMAKECONFIG}
  echo "" >>${TMPMAKECONFIG}
done

# Make the changes permanent
#
if [ "${REGEN}" != "YES" ]; then
    mv -f $TMPCONFIGFILE $CONFIGFILE
    chmod 755 $CONFIGFILE
    echo "Config script \"${CONFIGFILE}\" created"
fi

mv -f $TMPMAKECONFIG $MAKECONFIG
chmod 644 $MAKECONFIG
echo "Make fragment \"${MAKECONFIG}\" created"
