set -e

# setup initial directorys (relative to root)
  CONFIG=config
  SCRIPTS=scripts
  PACKAGES=packages
  SOURCES=sources
  BUILD_BASE=build
  TARGET=target
  ADDONS=addons

# include ARCH specific options
  if [ -f config/arch.$TARGET_ARCH ]; then
    . config/arch.$TARGET_ARCH
  fi

HOST_NAME=`$LOCAL_CC -dumpmachine`
TARGET_NAME=$(echo $TARGET_SUBARCH | sed -e "s,-,,")-openelec-linux-gnu${TARGET_ABI}

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

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"

  if [ -n "$1" ]; then
    PKG_DIR=`find $PACKAGES -type d -name $1 ! -wholename \*\/source\/\* 2>/dev/null`
    if [ "${PKG_DIR}" != "$(echo $PKG_DIR | tr -d '\n')" ]; then
      echo "Error - multiple package folders:"
      echo "$PKG_DIR"
      exit 1
    fi
  fi

  [ -r $PKG_DIR/meta ] && . $PKG_DIR/meta
  [ "$PKG_IS_ADDON" = "yes" ] && PKG_ADDON_ID="`echo $PKG_SECTION | sed 's,/,.,g'`.$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_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

SSL_CERTIFICATES="/etc/pki/tls"

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

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_OBJDUMP=${TARGET_PREFIX}objdump
TARGET_STRIP=${TARGET_PREFIX}strip

. config/optimize

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"

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_PKG_CONFIG_PATH=""
HOST_PKG_CONFIG_LIBDIR="$ROOT/$TOOLCHAIN/lib/pkgconfig:$ROOT/$TOOLCHAIN/share/pkgconfig"
HOST_PKG_CONFIG_SYSROOT_DIR=""

if [ -z "$CCACHE_DIR" ]; then
    export CCACHE_DIR=$HOME/.ccache-openelec
fi
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

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

VERSION_SUFFIX=$TARGET_ARCH
. config/functions

check_path
check_config

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
BUILD_INDENT_SIZE=4
