#! /bin/bash

#
#  Basic configuration
#

# Target platform
target=
toolchain=

# Build options
prefix=
cflags=

# Behavior
no_syscalls=
static_gray=
user_vram=

# Size limits
atexit_max=

# Output files
output="Makefile.cfg"

#
#  Help screen
#

help()
{
  cat << EOF
Configuration script for the gint library.
Usage: $0 [options...]

You should build out-of-tree by creating a build directory and configuring from
there.

Target selection:
  --target=fx9860g|fxcg50

  fx9860g covers all fx-9860G II-like monochromes models that support add-ins
  or can be flashed with an OS that does. This includes SH3 and SH4 machines.
  Default toolchain is 'sh-elf'.

  fxcg50 covers just the fx-CG 50; there is some unofficial compatibility with
  fx-CG 10/20. All of these are SH4-only.
  Default toolchain is 'sh-elf'.

Build options:
  --toolchain=TRIPLET  Build with a different toolchain
  --prefix=PREFIX      Install prefix (PREFIX/lib and PREFIX/include are used)
  --cflags=FLAGS       Additional compiler flags at end of command

Library options (disabled by default):
  --no-syscalls        Cut off all syscalls (this will break things)
                       Only use this option if you have a good idea of which.
  --static-gray        Allocate gray VRAMs in static RAM instead of the heap
                       May help when --no-syscalls is on.
  --user-vram          Allocate VRAM in user stack; takes 350k/512k [fxcg50]

Size limits:
  --atexit-max=NUM     Number of exit handlers in atexit()'s array [16]

Deprecated options (to be removed):
  -events-queue-size=<n>
                       Size of event queue (this mechanism is likely to
                       disappear in future versions) [64]
EOF
  exit 0
}

if [[ "$@" == "--help" ]]; then
  help
  exit 1
fi

if [[ -f make/Makefile ]]; then
  echo "error: you should configure from a build directory, like this:" >&2
  echo "  mkdir build && cd build && ../configure [options..]" >&2
  exit 1
fi

#
#  Parsing arguments
#

fail=false
for arg; do case "$arg" in
  -h | -? | --help)
    help;;

  --target=*)
    case ${arg#*=} in
      "fx9860g")
        target=fx9860g
        toolchain=${toolchain:-sh-elf};;
      "fxcg50")
        target=fxcg50
        toolchain=${toolchain:-sh-elf};;
      *)
        echo "error: invalid target '$target'"
        fail=true
    esac;;
  --toolchain=*)
    toolchain=${arg#*=};;

  --prefix=*)
    prefix=${arg#*=};;
  --cflags=*)
    cflags=${arg#*=};;

  --no-syscalls)
    no_syscalls=true;;
  --static-gray)
    static_gray=true;;
  --user-vram)
    user_vram=true;;

  --atexit-max=*)
    n=${arg#*=}
    if [[ $n =~ ([0-9])+ ]]; then
      atexit_max=$n
    else
      echo -e "error: -atexit-max expects an integer value"
      fail=true
    fi;;

  --atexit-max)
    echo "error: '$arg' expects a value (see '$0 --help')";
    fail=true;;

  -events-queue-size=*)
    echo "warning: support for '-events-queue-size' has been removed";;

  *)
    echo "error: unrecognized argument '$arg'";
    fail=true;;
esac; done

#
#  Checking mandatory arguments
#

if [[ -z "$target" ]]; then
  echo "error: no target specified! (see '$0 --help')"
  fail=true;
fi

if $fail; then
  echo "note: output file $output has not been changed."
  exit 1
fi

# If no prefix is specified, install to the GCC's build folder
if [[ -z "$prefix" ]]; then
  echo "No prefix specified, let's ask the compiler:"
  echo "  $toolchain-gcc --print-search-dirs | grep install | sed 's/install: //'"
  inst=$($toolchain-gcc --print-search-dirs | grep install | sed 's/install: //')

  if [[ $? != 0 ]]; then
    echo "Call returned $?, giving up."
  fi

  echo "Got '$inst'".

  if [[ ! -d $inst ]]; then
    echo "Directory does not exist (or is not a directory), giving up."
    exit 1
  fi

  echo ""
  prefix=$inst
fi

#
#  Output config
#

output_config()
{
  mod=${target/98/fx}
  echo "CONFIG.TARGET = ${mod:2:2}"
  echo "CONFIG.TARGET.LONG = $target"

  [[ $prefix    ]] && echo "PREFIX = $prefix"
  [[ $toolchain ]] && echo "CONFIG.TOOLCHAIN = $toolchain"
  [[ $cflags    ]] && echo "CONFIG.CFLAGS = $cflags"

  echo -n "CONFIG.MACROS ="
  echo -n " -D$(echo $target | tr 'a-z' 'A-Z')"
  [[ "$no_syscalls" ]] && echo -n " -DGINT_NO_SYSCALLS"
  [[ "$static_gray" ]] && echo -n " -DGINT_STATIC_GRAY"
  [[ "$user_vram"   ]] && echo -n " -DGINT_USER_VRAM"
  [[ "$atexit_max"  ]] && echo -n " -DATEXIT_MAX=$atexit_max"
  echo ""
}

output_config > $output

src="Makefile"
dst="../make/Makefile"

[[ -L $src && $(readlink $src) == $dst ]] && rm $src
ln -s $dst $src

echo "Configuration saved in $output, ready to make!"