#! /bin/sh

INSTDIR=/opt/cross/arm-unknown
ARCH=arm
TARGET=arm-unknown-linux-gnu
HOST=i686-pc-linux-gnu

SYSROOT=$INSTDIR/sys-root-hard
HEADERDIR=$SYSROOT/usr/include
BINDIR=linuxthreads-hard

SYSROOTNPTL=$INSTDIR/sys-root-nptl-hard
HEADERDIRNPTL=$SYSROOTNPTL/usr/include
BINDIRNPTL=nptl-hard

ORIGPATH=$PATH

cleanup() {
  # cleanup
  sudo rm -rf ep-hard-*
  sudo rm -rf $SYSROOT
  sudo rm -rf $SYSROOTNPTL
  sudo rm -rf $BINDIR
  sudo rm -rf $BINDIRNPTL
}

create_sys_root() {
  # create sys-root
  mkdir -p $SYSROOT/lib
  mkdir -p $SYSROOT/usr/lib
  mkdir -p $HEADERDIR
}

create_sys_root_nptl() {
  # create sys-root-nptl
  mkdir -p $SYSROOTNPTL/lib
  mkdir -p $SYSROOTNPTL/usr/lib
  mkdir -p $HEADERDIRNPTL
}

prepare_kernel() {
  # prepare kernel
  pushd linux
  make distclean
  make ARCH=$ARCH edb7211_defconfig
  make ARCH=$ARCH include/asm include/linux/version.h include/asm-$ARCH/.arch

  cp -r include/linux $HEADERDIR
  cp -r include/asm-${ARCH} $HEADERDIR/asm
  cp -r include/asm-generic $HEADERDIR/asm-generic

  cp -r include/linux $HEADERDIRNPTL
  cp -r include/asm-${ARCH} $HEADERDIRNPTL/asm
  cp -r include/asm-generic $HEADERDIRNPTL/asm-generic

  popd
}

build_binutils() {
  # build binutils
  mkdir -p ep-hard-binutils

  pushd ep-hard-binutils
  ../binutils/configure --target=$TARGET --host=$HOST --with-sysroot=$SYSROOT \
	--prefix=$INSTDIR/$BINDIR
  make
  sudo make install

  ../binutils/configure --target=$TARGET --host=$HOST \
  	--with-sysroot=$SYSROOTNPTL --prefix=$INSTDIR/$BINDIRNPTL
  make
  sudo make install

  popd
}

glibc_header() {
  # glibc-header
  mkdir -p ep-hard-glibc-header

  pushd ep-hard-glibc-header
  libc_cv_ppc_machine=yes ../libc/configure --prefix=/usr \
	--build=$HOST --host=$TARGET --without-cvs \
	--disable-sanity-checks --with-headers=$HEADERDIR \
	--enable-hacker-mode

  libc_cv_ppc_machine=yes make sysdeps/gnu/errlist.c
  mkdir -p stdio-common
  touch stdio-common/errlist-compat.c

  libc_cv_ppc_machine=yes make cross-compiling=yes install_root=$SYSROOT \
	install-headers
  mkdir -p $HEADERDIR/gnu
  touch $HEADERDIR/gnu/stubs.h
  cp ../libc/include/features.h $HEADERDIR/features.h
  cp bits/stdio_lim.h $HEADERDIR/bits/stdio_lim.h

  libc_cv_ppc_machine=yes make cross-compiling=yes install_root=$SYSROOTNPTL \
	install-headers
  mkdir -p $HEADERDIRNPTL/gnu
  touch $HEADERDIRNPTL/gnu/stubs.h
  cp ../libc/include/features.h $HEADERDIRNPTL/features.h
  cp bits/stdio_lim.h $HEADERDIRNPTL/bits/stdio_lim.h

  popd
}

build_gcc_core() {
  # build gcc-core
  export PATH=$INSTDIR/$BINDIR/bin:$ORIGPATH
  mkdir -p ep-hard-gcc-core

  pushd ep-hard-gcc-core
  ../gcc-arm/configure --target=$TARGET --host=$HOST \
	--with-local-prefix=$SYSROOT --disable-multilib --with-newlib \
	--with-sysroot=$SYSROOT --enable-threads=no \
	--enable-symvers=gnu --enable-__cxa_atexit --enable-languages=c \
	--disable-shared --prefix=$INSTDIR/$BINDIR
  make all-gcc
  sudo make PATH=$INSTDIR/$BINDIR/bin:$ORIGPATH install-gcc
  popd
}

build_glibc_linuxthreads() {
  # build glibc/linuxthreads
  export PATH=$INSTDIR/$BINDIR/bin:$ORIGPATH

  mkdir -p ep-hard-glibc
  sudo touch $INSTDIR/$BINDIR/lib/gcc/arm-unknown-linux-gnu/3.4.3/libgcc_s.a

  pushd ep-hard-glibc
  #BUILD_CC=gcc CFLAGS="-O2 -funroll-loops -mcpu=ep9312 -mfpu=maverick" \
  BUILD_CC=gcc CFLAGS="-O2 -funroll-loops" \
  CC=$TARGET-gcc AR=$TARGET-ar RANLIB=$TARGET-ranlib \
  ../libc/configure --prefix=/usr \
	--build=$HOST --host=$TARGET \
	--enable-kernel=2.4.3 --without-cvs --disable-profile \
	--disable-debug --without-gd --without-tls --without-__thread \
	--enable-shared --enable-add-ons=linuxthreads --enable-omitfp \
	--without-fp --with-headers=$HEADERDIR
  make LD=$TARGET-ld RANLIB=$TARGET-ranlib lib
  make install_root=$SYSROOT install-lib-all install-headers
  popd

  sudo rm $INSTDIR/$BINDIR/lib/gcc/arm-unknown-linux-gnu/3.4.3/libgcc_s.a
}

build_final_gcc() {
  # build final gcc
  export PATH=$INSTDIR/$BINDIR/bin:$ORIGPATH

  mkdir -p ep-hard-gcc
  pushd ep-hard-gcc
  ac_cv_lib_m_main=yes ../gcc-arm/configure --target=$TARGET --host=$HOST \
	--with-local-prefix=$SYSROOT --with-sysroot=$SYSROOT \
	--enable-threads=posix --enable-symvers=gnu \
	--enable-__cxa_atexit --enable-languages="c,c++" \
	--enable-shared --enable-c99 --enable-long-long \
	--prefix=$INSTDIR/$BINDIR --enable-multilib
  make all
  sudo make PATH=$INSTDIR/$BINDIR/bin:$ORIGPATH install
  popd
}

build_glibc_NPTL() {
  # build glibc-NPTL
  export PATH=$INSTDIR/$BINDIR/bin:$ORIGPATH

  mkdir -p ep-hard-glibc-nptl
  pushd ep-hard-glibc-nptl
  #BUILD_CC=gcc CFLAGS="-O2 -funroll-loops -mcpu=ep9312 -mfpu=maverick"  -mtp=linux" \
  BUILD_CC=gcc CFLAGS="-O2 -funroll-loops -mtp=soft" \
  CC=$TARGET-gcc AR=$TARGET-ar RANLIB=$TARGET-ranlib \
  ../libc/configure --prefix=/usr \
	--build=$HOST --host=$TARGET \
	--enable-kernel=2.4.3 --without-cvs --disable-profile \
	--enable-debug --without-gd --with-tls --with-__thread \
	--enable-shared --enable-add-ons=nptl --enable-profile \
	--without-fp --with-headers=$HEADERDIRNPTL
  make LD=$TARGET-ld RANLIB=$TARGET-ranlib lib
  make install_root=$SYSROOTNPTL install-lib-all install-headers
  popd
}

build_final_nptl_gcc() {
  # build final nptl gcc
  export PATH=$INSTDIR/$BINDIRNPTL/bin:$ORIGPATH
  mkdir -p ep-hard-gcc-nptl

  pushd ep-hard-gcc-nptl
  ac_cv_lib_m_main=yes ../gcc-arm/configure --target=$TARGET --host=$HOST \
	--with-local-prefix=$SYSROOTNPTL --with-sysroot=$SYSROOTNPTL \
	--enable-threads=posix --enable-symvers=gnu \
	--enable-__cxa_atexit --enable-languages="c,c++" \
	--enable-shared --enable-c99 --enable-long-long \
	--prefix=$INSTDIR/$BINDIRNPTL \
  #	--enable-cxx-flags='-mcpu=ep9312 -mfpu=maverick'
  make all
  sudo make PATH=$INSTDIR/$BINDIRNPTL/bin:$ORIGPATH install
  popd
}

build_gdb() {
  # build gdb
  pushd ep-hard-binutils

  mkdir readline
  cd readline
  ../../binutils-complete/readline/configure --target=$TARGET --host=$HOST
  make
  cd ..
  mkdir gdb
  cd gdb
  ../../binutils-complete/gdb/configure --target=$TARGET --host=$HOST \
  	--with-sysroot=$SYSROOT --prefix=$INSTDIR/$BINDIR \
	--enable-tui --disable-gdbtk
  make
  sudo make install

  ../../binutils-complete/gdb/configure --target=$TARGET --host=$HOST \
  	--with-sysroot=$SYSROOTNPTL --prefix=$INSTDIR/$BINDIRNPTL \
	--enable-tui --disable-gdbtk
  make
  sudo make install

  cd ..
  popd
}

err() {
	echo Failed to build $1
	exec false
}

keystroke() {
#	read -n1 -p "$@ [key to continue]"
	echo "*** $@ ***"
}

### MAIN ###

keystroke "cleanup"
cleanup

keystroke "create sys_root"
create_sys_root

keystroke "create sys_root_nptl"
create_sys_root_nptl

keystroke "prepare kernel"
prepare_kernel

keystroke "binutils"
build_binutils

keystroke "glibc header"
glibc_header

keystroke "gcc core"
build_gcc_core

keystroke "glibc linuxthreads"
build_glibc_linuxthreads

keystroke "final gcc linuxthreads"
build_final_gcc

keystroke "glibc nptl"
build_glibc_NPTL

keystroke "final gcc nptl"
build_final_nptl_gcc

keystroke "gdb"
build_gdb
