set -e

. config/functions

# 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
if [ "$OPENELEC_VERSION" = "devel" ] ; then
  BUILD=$BUILD_BASE.$DISTRONAME-$PROJECT.$TARGET_ARCH-$OS_VERSION-$OPENELEC_VERSION
fi

if [ -n "$BUILD_SUFFIX" ]; then
  BUILD=$BUILD-$BUILD_SUFFIX
fi

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

if [ -z "$INSTALL" ]; then
  INSTALL=$BUILD/image/system
fi
INSTALL_INIT=$BUILD/image/initramfs/root-image

. 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_TARGET=""
  PKG_DEPENDS_HOST=""
  PKG_DEPENDS_INIT=""
  PKG_DEPENDS_BOOTSTRAP=""
  PKG_PRIORITY="optional"
  PKG_SECTION=""
  PKG_SHORTDESC="$1 (autogenerated)"
  PKG_LONGDESC=""
  PKG_IS_ADDON="no"

  if [ -n "$1" ]; then
    _PKG_ROOT_NAME=$(echo $1 | cut -d: -f1)
    _ALL_DIRS=""
    _FOUND=0
    PKG_DIR=""

    # first check project folder for a package
    for DIR in $(find $ROOT/projects/$PROJECT/packages -type d -name $_PKG_ROOT_NAME 2>/dev/null); do
      if [ -r "$DIR/package.mk" ]; then
        # found first, set $PKG_DIR
        PKG_DIR="$DIR"
        # keep track of dirs with package.mk for detecting multiple folders
        _ALL_DIRS="${_ALL_DIRS}${DIR}\\n"
        _FOUND=$((_FOUND+1))
        if [ $_FOUND -gt 1 ]; then
          # _FOUND more ? fail
          echo "Error - multiple package folders:"
          echo -e "$_ALL_DIRS"
          exit 1
        fi
      fi
    done

    # then check packages folder if not found already
    if [ $_FOUND -eq 0 ]; then
      for DIR in $(find $ROOT/$PACKAGES -type d -name $_PKG_ROOT_NAME 2>/dev/null); do
        if [ -r "$DIR/package.mk" ]; then
          # found first, set $PKG_DIR
          PKG_DIR="$DIR"
          # keep track of dirs with package.mk for detecting multiple folders
          _ALL_DIRS="${_ALL_DIRS}${DIR}\\n"
          _FOUND=$((_FOUND+1))
          if [ $_FOUND -gt 1 ]; then
            # found more ? fail
            echo "Error - multiple package folders:"
            echo -e "$_ALL_DIRS"
            exit 1
          fi
        fi
      done
    fi
  fi

  if [ -r $PKG_DIR/package.mk ]; then
    . $PKG_DIR/package.mk
  fi

  if [ "$PKG_IS_ADDON" = "yes" ] ; then
    [ -z $PKG_SECTION ] && PKG_ADDON_ID="$PKG_NAME" || PKG_ADDON_ID="`echo $PKG_SECTION | sed 's,/,.,g'`.$PKG_NAME"
  fi

  PKG_BUILD="$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/ssl"

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

unset TARGET_CPP

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"

unset HOST_CPP

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:$ROOT/$TOOLCHAIN/sbin:$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

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

# define colors
  black="\e[0;30m"
  boldblack="\e[1;30m"
  red="\e[0;31m"
  boldred="\e[1;31m"
  green="\e[0;32m"
  boldgreen="\e[1;32m"
  yellow="\e[0;33m"
  boldyellow="\e[1;33m"
  blue="\e[0;34m"
  boldblue="\e[1;34m"
  magenta="\e[0;35m"
  boldmagenta="\e[1;35m"
  cyan="\e[0;36m"
  boldcyan="\e[1;36m"
  white="\e[0;37m"
  boldwhite="\e[1;37m"
  endcolor="\e[0m"

# multilib? nah
unset CONFIG_SITE

# meh suse
unset PYTHONSTARTUP
unset PYTHONPATH
