#!/bin/bash


if ROOT=$(pwd) && HOST_SYSTEM=$(uname -s) && HOST_ARCH=$(uname -p)
then

 #
 # Rewrite uname values to a pair from these sets
 #
 #  HOST_SYSTEM := { MacOSX, Linux, Nt, Solaris, FreeBSD, NetBSD, Irix, Plan9 }
 #
 #  HOST_ARCH := { 386, mips, power, sparc }
 #
 case ${HOST_SYSTEM} in
  Windows*|CYGWIN*|MINGW*)
   HOST_SYSTEM="Nt"
   ;;
  Darwin)
   HOST_SYSTEM="MacOSX"
   ;;
  SunOS)
   HOST_SYSTEM="Solaris"
   ;;
 esac

 if [ "unknown" = "${HOST_ARCH}" ]
 then
  HOST_ARCH=$(uname -m)
 fi

 case ${HOST_ARCH} in
  ppc|powerpc)
   HOST_ARCH="power"
   ;;
  x86|x86_32|i386|i586|i686|486|586|686|x86_64)
   HOST_ARCH="386"
   ;;
 esac

 #
 # Initialize configuration
 #
 SYSHOST=${HOST_SYSTEM}
 SYSTARG=${HOST_SYSTEM}
 OBJTYPE=${HOST_ARCH}

 #
 # Optionally modify configuration
 #
 while [ -n "${1}" ]
 do
  case $1 in
   *clean)
    rm -f mkconfig env bin
    #1>&2 echo ok
    exit 0
    ;;
   --host)
    shift
    HOST_SYSTEM=$1
    shift
    SYSHOST=${HOST_SYSTEM}
    SYSTARG=${HOST_SYSTEM}
    ;;
   --target)
    shift
    SYSTARG=$1
    shift
    ;;
   --object)
    shift
    OBJTYPE=$1
    shift
    ;;
   --help|-help|-h|-?)
    cat<<EOF>&2

 Configuration options (in order)

   --host <Host system type>     (eg, MacOSX, Linux, Nt, Solaris, FreeBSD, NetBSD, Plan9)

   --target <Target system type> (eg, MacOSX, Linux, Nt, Solaris, FreeBSD, NetBSD, Irix, Plan9)

   --object <Target object type> (eg, 386, mips, power, sparc)

 Usage

   Configuration options must be employed in the order shown above.

   Running with no arguments should produce a valid result.  

   Please contribute to this project by ensuring that any found bugs
   have been reported via

      http://code.google.com/p/ken-cc/issues/list

   for your configuration

      HOST_SYSTEM=${HOST_SYSTEM}
      HOST_ARCH=${HOST_ARCH}

 Cleaning (or unconfiguring)

   Run 
      ./configure clean
   or
      ./configure --clean

   to remove the products of this script.

EOF
    exit 1
    ;;
  esac
 done
 #
 # Host bin 
 #
 BINDIR=${HOST_SYSTEM}/${HOST_ARCH}
 #
 # Target bin
 #
 OBJDIR=${SYSTARG}/${OBJTYPE}
 #
 # Sanity testing...
 #
 if [ -z "${SYSHOST}" ]||[ -z "${SYSTARG}" ]||[ -z "${OBJTYPE}" ]
 then
  cat<<EOF>&2
Input error
EOF
  exit 1

 elif [ ! -d "${BINDIR}" ]
 then
  cat<<EOF>&2
Host type selection error in one or both of
 HOST_SYSTEM '${HOST_SYSTEM}'
 HOST_ARCH '${HOST_ARCH}'
EOF
  exit 1

 elif [ ! -d "${OBJDIR}" ]
 then
  cat<<EOF>&2
Target type selection error in one or both of
 TARGET '${SYSTARG}'
 OBJECT '${OBJTYPE}'
EOF
  exit 1

 else
  #
  # Generate configuration
  #
  cat<<EOF>mkconfig
#
#	Set the following 4 variables.  The host system is the system where
#	the software will be built; the target system is where it will run.
#	They are almost always the same.

#	On Nt systems, the ROOT path MUST be of the form 'drive:/path'
ROOT=${ROOT}

#   build system OS type
SYSHOST=${SYSHOST}

#   target system OS type
SYSTARG=${SYSTARG}

#   target system object type
OBJTYPE=${OBJTYPE}

#
#	no changes required beyond this point
#
OBJDIR=\$SYSTARG/\$OBJTYPE

<\$ROOT/mkfiles/mkhost-\$SYSHOST			# variables appropriate for host system
<\$ROOT/mkfiles/mkfile-\$SYSTARG-\$OBJTYPE	# variables used to build target object type

EOF

 fi
 #
 # Report product
 #
 cat<<EOF
   Successfully configured and wrote 'mkconfig'
EOF

 #
 # Configure host binaries into 'env' path
 #
 if [ -e bin ]
 then 
  rm -f bin
 fi
 if ln -s ${BINDIR}/bin bin
 then
 cat<<EOF
   Successfully configured host path '$(pwd)/bin'
EOF
 else
  cat<<EOF>&2
Error linking host path 'ln -s ${BINDIR}/bin bin'
EOF
  exit 1
 fi
 #
 # Configure 'env' script
 #
 ewd=$(pwd)
 ebp=${ewd}/bin
 cat<<EOF>env
#
# Generated by ${ewd}/configure. See ./configure --help.
#
# Usage
#   Run 
#      . ./env
#   or
#      source ./env
#
#   in this directory 
#   to initialize this development environment
#
PATH=${ebp}:\$(echo \${PATH} | sed 's%${ebp}:%%g; s%:${ebp}%%g')
#
# Convenience for running commands outside 'mk'...
#
ROOT=${ROOT}
#
# Conveniences for sourcing and targeting commands as in 'mk'...
#
SYSTARG=${SYSTARG}
OBJTYPE=${OBJTYPE}
OBJDIR=\${SYSTARG}/\${OBJTYPE}

EOF

 cat<<EOF>&2

   Employ command ". ./env" in this directory to initialize usage,
   or PREPEND "$(pwd)/bin" to your PATH.

EOF
 exit 0
else
 cat<<EOF>&2

Error running commands 'pwd' or 'uname'.
Unable to configure.

EOF
 exit 1
fi
