#! /bin/bash

PREFIX="/opt/local"

if [ "$ARCH" == "" ]; then
  ARCH="$(grep '^build_arch' $PREFIX/etc/macports/macports.conf \
         | awk '{print $2}')"
fi

if [ "$OSVER" == "" ]; then
  OSFULLVER="$(sw_vers -productVersion)"
  if echo $OSFULLVER | egrep -q '[0-9]+[.][0-9]+[.][0-9]+'; then 
    OSVER="$(echo $OSFULLVER | sed -E 's/[.][0-9]+$//')"
  else
    OSVER="$OSFULLVER"
  fi
fi

if [ "$CC" == "" ]; then
  case $OSVER in

    10.4 )
      export CC="$PREFIX/bin/gcc-apple-4.2"
      export TIGER="-Itiger_only/include"
      ;;

    10.5 )
      export CC="/Developer/usr/bin/gcc-4.2"
      ;;
  esac
fi

if echo $CC | grep -q "/clang" ; then
  export CXX="$(echo $CC | sed 's|/clang|/clang++|')"
fi
if echo $CC | grep -q "/gcc" ; then
  export CXX="$(echo $CC | sed 's|/gcc|/g++|')"
fi
if echo $CC | grep -q "/cc" ; then
  export CXX="$(echo $CC | sed 's|/cc|/c++|')"
fi

export CFLAGS="-arch $ARCH $CFLAGS $TIGER"
export CXXFLAGS="-arch $ARCH $CXXFLAGS $TIGER"
export LDFLAGS="-arch $ARCH $LDFLAGS"

exec "$@"
