#!/bin/sh

cxx=g++
libbutl=

while test $# -ne 0; do
  case $1 in
    --help)
      echo "Usage: $0 [--help] [--cxx <c++-compiler>]" 1>&2
      echo "See the INSTALL file for details." 1>&2
      exit 0
      ;;
    --cxx)
      shift
      if test $# -eq 0; then
	echo "error: c++ compiler executable expected after --cxx" 1>&2
	exit 1
      fi
      cxx=$1
      shift
      ;;
    --libbutl)
      shift
      if test $# -eq 0; then
	echo "error: libbutl path expected after --libbutl" 1>&2
	exit 1
      fi
      if test ! -d "$1"; then
	echo "error: libbutl directory '$1' does not exist" 1>&2
	exit 1
      fi
      libbutl=$1
      shift
      ;;
    *)
      echo "error: unknown option $1" 1>&2
      exit 1
      ;;
  esac
done

# See if there is libbutl or libbutl-* in the current directory and
# one directory up.
#
if test -z "$libbutl"; then
  if test -d libbutl; then
    libbutl=libbutl
  else
    libbutl=`echo libbutl-*`
    if test ! -d "$libbutl"; then
      libbutl=
    fi
  fi
fi

if test -z "$libbutl"; then
  if test -d ../libbutl; then
    libbutl=../libbutl
  else
    libbutl=`echo ../libbutl-*`
    if test ! -d "$libbutl"; then
      libbutl=
    fi
  fi
fi

if test -z "$libbutl"; then
  echo "error: unable to find libbutl, use --libbutl to specify its location" 1>&2
  exit 1
fi

src="build/*.cxx build/config/*.cxx build/bin/*.cxx build/cxx/*.cxx $libbutl/butl/*.cxx"

echo $cxx -std=c++1y -I$libbutl -I. -o build/b-boot $src 1>&2
exec $cxx -std=c++1y -I$libbutl -I. -o build/b-boot $src