setup_toolchain() {
  if [ "$1" = "--optimize" ]; then
    OPTIMIZE=yes
    shift
  fi

  if [ "$1" = target ]; then
    export DESTIMAGE="target"

    export CC=$TARGET_CC
    export CXX=$TARGET_CXX
    export LD=$TARGET_LD
    export AS=$TARGET_AS
    export AR=$TARGET_AR
    export NM=$TARGET_NM
    export RANLIB=$TARGET_RANLIB
    export OBJCOPY=$TARGET_OBJCOPY
    export STRIP=$TARGET_STRIP
    export CPPFLAGS="$TARGET_CPPFLAGS"
    export CFLAGS="$TARGET_CFLAGS"
    export CXXFLAGS="$TARGET_CXXFLAGS"
    export LDFLAGS="$TARGET_LDFLAGS"
    export PKG_CONFIG_PATH="$TARGET_PKG_CONFIG_PATH"
    export PKG_CONFIG_LIBDIR="$TARGET_PKG_CONFIG_LIBDIR"
    export PKG_CONFIG_SYSROOT_DIR="$TARGET_PKG_CONFIG_SYSROOT_DIR"

  # set CMAKE configfile for target
    export CMAKE_CONF=$ROOT/$TOOLCHAIN/etc/cmake-$TARGET_NAME.conf

  elif [ "$1" = host ]; then
    export DESTIMAGE="host"

    export AWK=$HOST_AWK
    export CC=$HOST_CC
    export CXX=$HOST_CXX
    export LD=$HOST_LD
    export AS=$HOST_AS
    export AR=$HOST_AR
    export NM=$HOST_NM
    export RANLIB=$HOST_RANLIB
    export OBJCOPY=$HOST_OBJCOPY
    export STRIP=$HOST_STRIP
    export CPPFLAGS="$HOST_CPPFLAGS"
    export CFLAGS="$HOST_CFLAGS"
    export CXXFLAGS="$HOST_CXXFLAGS"
    export LDFLAGS="$HOST_LDFLAGS"
    export PKG_CONFIG_PATH="$HOST_PKG_CONFIG_PATH"
    export PKG_CONFIG_LIBDIR="$HOST_PKG_CONFIG_LIBDIR"
    export PKG_CONFIG_SYSROOT_DIR="$HOST_PKG_CONFIG_SYSROOT_DIR"

  # set CMAKE configfile for host
    export CMAKE_CONF=$ROOT/$TOOLCHAIN/etc/cmake-$HOST_NAME.conf

  fi
}

kernel_path() {
  ls -d $ROOT/$BUILD/linux-[0-9]*
}

tolower(){
  echo "$@" | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
}

require_eglibc() {
  if [ "$TARGET_LIBC" != eglibc ]; then
    echo "$1 requires eglibc, aborting."
    exit 1
  fi
}

require_cxx() {
  if [ "$TOOLCHAIN_CXX" != yes ]; then
    echo "$1 requires C++ toolchain support, aborting."
    exit 1
  fi
}

xorg_drv_configure_prepend() {
  incdir=${SYSROOT_PREFIX}/usr/include/xorg
  for f in dri.h sarea.h dristruct.h exa.h damage.h xf86Module.h; do
    incfile="$incdir/$f"
    if [ -f "$incfile" ]; then
      p=`echo "$incfile" | sed 'y%*+%pp%;s%[^_[:alnum:]]%_%g'`
      eval "export ac_cv_file_$p=yes"
    fi
  done
}

add_user() {
  # Usage: add_user "username" "password" "userid" "groupid" "description" "home" "shell"
  mkdir -p ${INSTALL}/etc
  touch ${INSTALL}/etc/passwd
  if [ -z "`grep "$1:" ${INSTALL}/etc/passwd`" ]; then
    echo "$1:$2:$3:$4:$5:$6:$7" >> ${INSTALL}/etc/passwd
  fi
}

add_group() {
  # Usage: add_group "groupname" "groupid" ("members")
  mkdir -p ${INSTALL}/etc
  touch ${INSTALL}/etc/group
  if [ -z "`grep "$1:" ${INSTALL}/etc/group`" ]; then
    echo "$1:x:$2:$3" >> ${INSTALL}/etc/group
  fi
}

get_version() {

  BUILD_DATE=`date +%Y%m%d`

  if [ "$OPENELEC_VERSION" = devel -o "$OPENELEC_VERSION" = debug ]; then
    GIT_BUILD=`git log --pretty=format:'' | wc -l`
    OPENELEC_VERSION=$OPENELEC_VERSION-$BUILD_DATE-r$GIT_BUILD
  fi

  TARGET_VERSION="$PROJECT.$TARGET_ARCH-$OPENELEC_VERSION"
}

do_autoreconf() {
  if [ -e $ROOT/$TOOLCHAIN/bin/autoreconf ] &&
     [ -e $ROOT/$TOOLCHAIN/bin/autoconf ] &&
     [ -e $ROOT/$TOOLCHAIN/bin/automake ] &&
     [ -e $ROOT/$TOOLCHAIN/bin/libtoolize ] &&
     [ -e $ROOT/$TOOLCHAIN/bin/intltoolize ]; then
       export LIBTOOL=$ROOT/$TOOLCHAIN/bin/libtool
       export LIBTOOLIZE=$ROOT/$TOOLCHAIN/bin/libtoolize
       export AUTOMAKE=$ROOT/$TOOLCHAIN/bin/automake
       export ACLOCAL_DIR=$SYSROOT_PREFIX/usr/share/aclocal
       export ACLOCAL="$ROOT/$TOOLCHAIN/bin/aclocal -I $ACLOCAL_DIR"
       export AUTOCONF=$ROOT/$TOOLCHAIN/bin/autoconf
       export AUTOHEADER=$ROOT/$TOOLCHAIN/bin/autoheader
       export AUTORECONF="$ROOT/$TOOLCHAIN/bin/autoreconf -v -f -i -I $ACLOCAL_DIR"

       mkdir -p $ACLOCAL_DIR
       autoreconf --verbose --force --install -I $ACLOCAL_DIR $@
  fi
}

strip_lto() {
  # strip out LTO optimization from *FLAGS
  CFLAGS=`echo $CFLAGS | sed -e "s|-flto||"`
  CXXFLAGS=`echo $CXXFLAGS | sed -e "s|-flto||"`
  LDFLAGS=`echo $LDFLAGS | sed -e "s|-flto||"`
}

strip_gold() {
  # strip out usage from GOLD linker
  LDFLAGS=`echo $LDFLAGS | sed -e "s|-fuse-ld=gold||" -e "s|-fuse-linker-plugin||"`
}

strip_linker_plugin() {
  # strip out usage from linker plugin
  LDFLAGS=`echo $LDFLAGS | sed -e "s|-fuse-linker-plugin||"`
}

strip_kmod() {
  [ -z $1 ] && echo "usage strip_kmod <dir>" && exit 1
  find $1 -type f -name '*.ko' | xargs -r $STRIP --strip-debug
}
