#!/bin/sh
#//////////////////////////////////////////////////////////////////////////////
#
# Copyright (c) 2007,2008 Daniel Adler <dadler@uni-goettingen.de>, 
#                         Tassilo Philipp <tphilipp@potion-studios.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
#//////////////////////////////////////////////////////////////////////////////


# --- output error message ---------------------------------------------------

error() 
{
  echo "error: $*"
  exit 1
}

# --- output warning message -------------------------------------------------

warning()
{
  echo "warning: $*"
}

# --- output info message ----------------------------------------------------

info()
{
  echo "$*"
}

# --- print usage ------------------------------------------------------------

usage()
{
  echo "dyncall configuration shell-script"
  echo "Usage:"
  echo "  configure [ options ]"
  echo
  echo "Options:"
  echo "  --help"
  echo "   -h                 print this page"
  echo
  echo "  --prefix <path>     set installation prefix (defaults '/usr/local')"
  echo
  echo "  --target-x86        build for x86 architecture platform"
  echo "  --target-x64        build for x64 architecture platform"
  echo "  --target-psp        build for Playstation Portable platform (PSPSDK)"
  echo "  --target-nds        build for Nintendo DS platform (devkitPro)"
  echo
  echo "  --tool-gcc          use GNU Compiler Collection"
  echo "  --tool-msvc         use Microsoft Visual C++"
  echo 
  echo "  --asm-as            use GNU Assembler"
  echo "  --asm-nasm          use NASM Assembler"
  echo "  --asm-ml            use Microsoft Macro Assembler"
  echo
  echo "  --config-release    build release version (default)"
  echo "  --config-debug      build debug version"
  exit 0
}

# --- installation prefix directory ------------------------------------------

INSTALL_DIR="/usr/local"

# --- guess operating system -------------------------------------------------

BUILD_OS=""
guess_os()
{
  OS=`uname -s`
  if [ "$OS" = "WindowsNT" ]; then
    BUILD_OS="windows"
  elif [ "$OS" = "Darwin" ]; then
    BUILD_OS="darwin"
  elif [ "$OS" = "CYGWIN_NT-5.1" ]; then
    BUILD_OS="windows"
  elif [ "$OS" = "Linux" ]; then
    BUILD_OS="linux"
  elif [ "$OS" = "SunOS" ]; then
    BUILD_OS="sunos"
  elif [ "$OS" = "OpenBSD" ]; then
    BUILD_OS="openbsd"
  elif [ "$OS" = "FreeBSD" ]; then
    BUILD_OS="freebsd"
  elif [ "$OS" = "NetBSD" ]; then
    BUILD_OS="netbsd"
  elif [ "$OS" = "DragonFly" ]; then
    BUILD_OS="dragonfly"
  fi
  info "guess operating system $BUILD_OS"
}

# --- guess architecture -----------------------------------------------------

BUILD_ARCH=""
guess_arch()
{
  ARCH=`uname -m`
  if [ "$ARCH" = "Power Macintosh" ]; then
    BUILD_ARCH="ppc32"
  elif [ "$ARCH" = "x86" ] || [ "$ARCH" = "i386" ] || [ "$ARCH" = "i486" ] || [ "$ARCH" = "i586" ] || [ "$ARCH" = "i686" ] || [ "$ARCH" = "i86pc" ]; then
    BUILD_ARCH="x86"
  elif [ "$ARCH" = "amd64" ] || [ "$ARCH" = "x86_64" ] || [ "$ARCH" = "x64" ]; then
    BUILD_ARCH="x64"
  fi
  info "guess arch $BUILD_ARCH"
}

# --- guess tool chain -------------------------------------------------------

BUILD_TOOL=""
guess_tool()
{
  BUILD_TOOL="gcc"
  info "guess tool $BUILD_TOOL"
}

# --- guess assembler --------------------------------------------------------

BUILD_ASM=""
guess_asm()
{
  if [ "$BUILD_TOOL" = "gcc" ]; then
    BUILD_ASM="as"
  elif [ "$BUILD_TOOL" = "msvc" ]; then
    BUILD_ASM="ml"
  fi
  info "guess assembler $BUILD_ASM"
}

# --- process arguments ------------------------------------------------------

while [ 0 -lt $# ]; do
  OPT=$1
  shift 1
  case $OPT in
    --help|-h)
      usage
      ;;
    --prefix)
      INSTALL_DIR=$1
      shift 1
      ;;
    --target-x86)
      BUILD_ARCH=x86
      ;;
    --target-x64)
      BUILD_ARCH=x64
      ;;
    --target-ppc32)
      BUILD_ARCH=ppc32
      ;;
    --target-psp) 
      BUILD_OS=psp
      BUILD_ARCH=mips32
      BUILD_TOOL=gcc
      ;;
    --target-nds)
      BUILD_OS=nds
      BUILD_ARCH=arm9e
      BUILD_TOOL=gcc
      ;;
    --tool-gcc)
      BUILD_TOOL=gcc
      ;;
    --tool-msvc)
      BUILD_TOOL=msvc
      ;;
    --asm-as)
      BUILD_ASM=as
      ;;
    --asm-nasm)
      BUILD_ASM=nasm
      ;;
    --asm-ml)
      BUILD_ASM=ml
      ;;
    --config-release)
      BUILD_CONFIG=release
      ;;
    --config-debug)
      BUILD_CONFIG=debug
      ;;
    *)
      warning "unknown option $OPT"
      ;;
  esac
done

# --- guess variables --------------------------------------------------------

info "running configure.."

if [ -z "$BUILD_OS" ]; then 
  guess_os
  if [ -z "$BUILD_OS" ]; then
    error "unsupported operating system $OS"
  fi
fi

if [ -z "$BUILD_ARCH" ]; then
  guess_arch 
  if [ -z "$BUILD_ARCH" ]; then
    error "unsupported architecture $ARCH"
  fi
fi

if [ -z "$BUILD_TOOL" ]; then
  guess_tool
  if [ -z "$BUILD_TOOL" ]; then
    error "no tool"
  fi
fi

if [ -z "$BUILD_ASM" ]; then
  guess_asm
  if [ -z "$BUILD_ASM" ]; then
    error " no assembler tool"
  fi
fi

if [ -z "$BUILD_CONFIG" ]; then
  BUILD_CONFIG=release
fi

# --- write ConfigVars files ------------------------------------------------

writeVars()
{
  echo "BUILD_OS=$BUILD_OS" >$1
  echo "BUILD_ARCH=$BUILD_ARCH" >>$1
  echo "BUILD_TOOL=$BUILD_TOOL" >>$1
  echo "BUILD_ASM=$BUILD_ASM" >>$1
  echo "BUILD_CONFIG=$BUILD_CONFIG" >>$1
  echo "INSTALL_DIR=$INSTALL_DIR" >>$1
  echo "" >>$1
}

writeVars ConfigVars
info "done"
