set -e

# determine architecture's family
case $TARGET_ARCH in
  i386)
    TARGET_FAMILY=i686
    ;;

  x86_64)
    TARGET_FAMILY=x86_64
    ;;
esac

# determines TARGET_CPU, if not forced by user
if [ -z "$TARGET_CPU" ]; then
  case $TARGET_ARCH in
    i386)
      TARGET_CPU=i686
      ;;

    x86_64)
      TARGET_CPU=i686
      ;;
  esac
fi

# setup initial directorys
  CONFIG=config
  SCRIPTS=scripts
  PACKAGES=packages
  SOURCES=sources
  BUILD_BASE=build
  TARGET=target
  ADDONS=addons

HOST_NAME=`$SCRIPTS/configtools/config.guess`
TARGET_NAME=$TARGET_FAMILY-openelec-linux-gnu

BUILD=$BUILD_BASE.$DISTRONAME-$PROJECT.$TARGET_ARCH-$OPENELEC_VERSION
TARGET_IMG=$ROOT/$TARGET
TARGET_ADDONS="$TARGET_IMG/$ADDONS/$ADDON_PATH"
ADDON_BUILD="$BUILD/$ADDONS/$1"
STAMPS_NOARCH=.stamps
STAMPS=$BUILD/.stamps
STAMPS_INSTALL=$BUILD/image/.stamps
DOCS=DOCS
TOOLCHAIN=$BUILD/toolchain
SYSROOT_PREFIX=$ROOT/$TOOLCHAIN/$TARGET_NAME/sysroot
LIB_PREFIX=$SYSROOT_PREFIX/usr
TARGET_PREFIX=$ROOT/$TOOLCHAIN/bin/$TARGET_NAME-

FAKEROOT_SCRIPT=$ROOT/.fakeroot.$PROJECT.$TARGET_ARCH

. config/sources

if [ "$OPENELEC_VERSION" = devel ]; then
  DEVTOOLS=yes
fi

MAKE="$ROOT/$TOOLCHAIN/bin/make"
MAKEINSTALL="$ROOT/$TOOLCHAIN/bin/make -j1 DESTDIR=$SYSROOT_PREFIX install"
SED="sed -i"

# set package metadata
  PKG_NAME="$1"
  PKG_VERSION="0.0invalid"
  PKG_REV="0"
  PKG_ARCH="any"
  PKG_LICENSE="unknown"
  PKG_SITE=""
  PKG_URL=""
  PKG_DEPENDS=""
  PKG_BUILD_DEPENDS=""
  PKG_PRIORITY="optional"
  PKG_SECTION="misc"
  PKG_SHORTDESC="$1 (autogenerated)"
  PKG_LONGDESC=""
  PKG_IS_ADDON="no"

  [ -n "$1" ] && PKG_DIR=`find $PACKAGES -type d -name $1 2>/dev/null`

  [ -r $PKG_DIR/meta ] && . $PKG_DIR/meta
  [ "$PKG_IS_ADDON" = "yes" ] && PKG_ADDON_ID="addon.$PKG_SECTION.$PKG_NAME"

  [ -d $BUILD/${PKG_NAME}[-_.]${PKG_VERSION} ] && PKG_BUILD=`ls -d $BUILD/${PKG_NAME}[-_.]${PKG_VERSION}`
  [ -d $BUILD/${PKG_NAME}${PKG_VERSION} ] && PKG_BUILD=`ls -d $BUILD/${PKG_NAME}${PKG_VERSION}`

XORG_PATH_ENCODINGS=/usr/share/X11/encodings
XORG_PATH_FONTS=/usr/share/X11/fonts
XORG_PATH_MAPS=$XORG_PATH_FONTS/util
XORG_PATH_DRI=/usr/lib/dri
XORG_PATH_XKB=/usr/share/X11/xkb
XORG_PATH_XKB_OUTPUT=/var/lib/xkb
XORG_PATH_RGB=/usr/lib/X11/rgb
XORG_PATH_MODULES=/usr/lib/xorg/modules
XORG_PATH_DRIVERS=/usr/lib/xorg/modules/drivers

TOOLCHAIN_LANGUAGES=c
[ "$TOOLCHAIN_CXX" = yes ] && TOOLCHAIN_LANGUAGES=${TOOLCHAIN_LANGUAGES},c++

. config/optimize

TARGET_CC=${TARGET_PREFIX}gcc
TARGET_CXX=${TARGET_PREFIX}g++
TARGET_LD=${TARGET_PREFIX}ld
TARGET_AS=${TARGET_PREFIX}as
TARGET_AR=${TARGET_PREFIX}ar
TARGET_NM=${TARGET_PREFIX}nm
TARGET_RANLIB=${TARGET_PREFIX}ranlib
TARGET_OBJCOPY=${TARGET_PREFIX}objcopy
TARGET_STRIP=${TARGET_PREFIX}strip

TARGET_CPPFLAGS=
TARGET_CFLAGS="-Wall -pipe $GCC_OPTIM $PROJECT_CFLAGS"
TARGET_CXXFLAGS="$TARGET_CFLAGS"
TARGET_LDFLAGS="$GCC_OPTIM $LD_OPTIM"

case $TARGET_ARCH in
    i386)
     TARGET_CFLAGS="$TARGET_CFLAGS -march=$TARGET_CPU -m32"
     TARGET_CXXFLAGS="$TARGET_CXXFLAGS -march=$TARGET_CPU -m32"
     TARGET_LDFLAGS="$TARGET_LDFLAGS -march=$TARGET_CPU -m32"
     ;;
    x86_64)
     TARGET_CFLAGS="$TARGET_CFLAGS -fPIC -march=$TARGET_CPU -m64"
     TARGET_CXXFLAGS="$TARGET_CXXFLAGS -fPIC -march=$TARGET_CPU -m64"
     TARGET_LDFLAGS="$TARGET_LDFLAGS -fPIC -march=$TARGET_CPU -m64"
     ;;
esac

TARGET_PKG_CONFIG_PATH=""
TARGET_PKG_CONFIG_LIBDIR="$SYSROOT_PREFIX/usr/lib/pkgconfig:$SYSROOT_PREFIX/usr/share/pkgconfig"
TARGET_PKG_CONFIG_SYSROOT_DIR="$SYSROOT_PREFIX"

if [ "$DEBUG" = yes ]; then
  TARGET_CFLAGS="$TARGET_CFLAGS -ggdb"
  TARGET_CXXFLAGS="$TARGET_CXXFLAGS -ggdb"
  TARGET_LDFLAGS="$TARGET_LDFLAGS -ggdb"
else
  TARGET_CFLAGS="$TARGET_CFLAGS -fomit-frame-pointer"
  TARGET_CXXFLAGS="$TARGET_CXXFLAGS -fomit-frame-pointer"
  TARGET_LDFLAGS="$TARGET_LDFLAGS -s"
fi

HOST_AWK=gawk
HOST_CC=$ROOT/$TOOLCHAIN/bin/host-gcc
HOST_CXX=$ROOT/$TOOLCHAIN/bin/host-g++
HOST_LD=ld
HOST_AS=as
HOST_AR=ar
HOST_NM=nm
HOST_RANLIB=ranlib
HOST_OBJCOPY=objcopy
HOST_STRIP=strip

HOST_CPPFLAGS=""
HOST_CFLAGS="-O2 -Wall -pipe -I$ROOT/$TOOLCHAIN/include"
HOST_CXXFLAGS="$HOST_CFLAGS"
HOST_LDFLAGS="-Wl,-rpath,$ROOT/$TOOLCHAIN/lib -L$ROOT/$TOOLCHAIN/lib"

# use -fPIC when necessary
  case "`uname -m`" in
    x86_64|ppc64|arm|armeb|armel)
    HOST_CFLAGS="$HOST_CFLAGS -fPIC"
    HOST_CXXFLAGS="$HOST_CXXFLAGS -fPIC"
    HOST_LDFLAGS="$HOST_LDFLAGS -fPIC"
    ;;
  esac

HOST_PKG_CONFIG_PATH=""
HOST_PKG_CONFIG_LIBDIR="$ROOT/$TOOLCHAIN/lib/pkgconfig:$ROOT/$TOOLCHAIN/share/pkgconfig"
HOST_PKG_CONFIG_SYSROOT_DIR=""

export CCACHE_DIR=$ROOT/$BUILD/.ccache
export MAKEFLAGS=-j$CONCURRENCY_MAKE_LEVEL
export PKG_CONFIG=$ROOT/$TOOLCHAIN/bin/pkg-config

if [ -z "$PATH" -o "$PATH" = "${PATH#$ROOT/$TOOLCHAIN/bin:}" ]; then
  export PATH="$ROOT/$TOOLCHAIN/bin:$PATH"
fi

VERSION_SUFFIX=$TARGET_ARCH

. config/functions

setup_toolchain target

SILENT_OUT=3
VERBOSE_OUT=4
if [ "$VERBOSE" = yes ]; then
  exec 3>&1
  exec 4>&2
else
  exec 3>&2
  exec 4>/dev/null
fi
INDENT_SIZE=4

