set -e

test -f config/target-$TARGET_PLATFORM && . config/target-$TARGET_PLATFORM

# 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

TARGET_NAME=$TARGET_FAMILY-openelec-linux-gnu

OPENELEC_VERSION=`cat VERSION`
CONFIG=config
SCRIPTS=scripts
PACKAGES=packages
SOURCES=sources
BUILD_BASE=build
BUILD=$BUILD_BASE.OpenELEC-$PROJECT.$TARGET_ARCH.$OPENELEC_VERSION
[ -n "$TARGET_PLATFORM" ] && BUILD=$BUILD.$TARGET_PLATFORM
PKG_BUILD=$BUILD/$1\[-_][0-9]*
STAMPS_NOARCH=.stamps
STAMPS=$BUILD/.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 [ "$OPENELEC_VERSION" = devel ]; then
 DEBUG=no
 DEVTOOLS=yes
elif [ "$OPENELEC_VERSION" = debug ]; then
 DEBUG=yes
 DEVTOOLS=yes
fi

HOST_NAME_CACHE=$BUILD/configtools/host_name
if [ -f $HOST_NAME_CACHE ]; then
  HOST_NAME=`cat $HOST_NAME_CACHE`
elif [ -x $BUILD/configtools/config.guess ]; then
  HOST_NAME=`$BUILD/configtools/config.guess`
fi

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

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++

if [ "$OPTIMIZATIONS" = speed ];then
  GCC_OPTIM="-Os -ffast-math $PROJECT_CFLAGS"
  GCC_OPTIM="$GCC_OPTIM -ftree-loop-distribution"
  GCC_OPTIM="$GCC_OPTIM -floop-interchange"
  GCC_OPTIM="$GCC_OPTIM -floop-strip-mine"
  GCC_OPTIM="$GCC_OPTIM -floop-block"
  GCC_OPTIM="$GCC_OPTIM -fgraphite-identity"
else
  GCC_OPTIM="-Os"
fi

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="$GCC_OPTIM -Wall -pipe"
TARGET_CXXFLAGS="$TARGET_CFLAGS"
TARGET_LDFLAGS=""
TARGET_PKG_CONFIG_PATH="$SYSROOT_PREFIX/usr/lib/pkgconfig"

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

if [ "$DEBUG" = yes ]; then
  TARGET_CFLAGS="$TARGET_CFLAGS -ggdb"
  TARGET_CXXFLAGS="$TARGET_CXXFLAGS -ggdb"
  TARGET_LDFLAGS="$TARGET_LDFLAGS -ggdb"
else
  TARGET_CFLAGS="$TARGET_CFLAGS -s -fomit-frame-pointer"
  TARGET_CXXFLAGS="$TARGET_CXXFLAGS -s -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="-L$ROOT/$TOOLCHAIN/lib"
HOST_PKG_CONFIG_PATH="$ROOT/$TOOLCHAIN/lib/pkgconfig"

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

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

  if [ "$1" = target ]; then
    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"

    if [ -f $ROOT/$TOOLCHAIN/bin/libtool ]; then
      export LIBTOOL=$ROOT/$TOOLCHAIN/bin/libtool
      export LIBTOOLIZE=$ROOT/$TOOLCHAIN/bin/libtoolize
    fi

    if [ -f $ROOT/$TOOLCHAIN/bin/automake ]; then
      export AUTOMAKE=$ROOT/$TOOLCHAIN/bin/automake
      export ACLOCAL_DIR=$SYSROOT_PREFIX/usr/share/aclocal
      export ACLOCAL="$ROOT/$TOOLCHAIN/bin/aclocal -I $ACLOCAL_DIR"
    fi

    if [ -f $ROOT/$TOOLCHAIN/bin/autoconf ]; then
      export AUTOCONF=$ROOT/$TOOLCHAIN/bin/autoconf
      export AUTOHEADER=$ROOT/$TOOLCHAIN/bin/autoheader
      export AUTORECONF="$ROOT/$TOOLCHAIN/bin/autoreconf -v -f -i -I $ACLOCAL_DIR"
    fi

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

  elif [ "$1" = host ]; then
    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"

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

  fi
}
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

OPENELEC_SRC=http://sources.openelec.tv/src/$OPENELEC_VERSION

VERSION_SUFFIX=$TARGET_ARCH
[ -n "$TARGET_PLATFORM" ] && VERSION_SUFFIX=$TARGET_PLATFORM

# read generic personal options if available
 if [ -f ~/.openelec/options ]; then
   . ~/.openelec/options
 fi

# read project based personal options if available
 if [ -f ~/.openelec/$PROJECT/options ]; then
   . ~/.openelec/$PROJECT/options
 fi

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

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
}
