#!/bin/bash

if [ "${1}" == "" ]
then
  echo "Syntax: "
  echo
  echo "	${0} <architecture>"
  echo
  exit
fi

TARGET_CFLAGS_I386="-O2 -pipe -m32 -march=i386 -mtune=i686"
TARGET_CFLAGS_X86_64="-O2 -pipe -m64 -march=x86_64 -mtune=x86_64"
TARGET_CFLAGS_PPC="-O2 -pipe -m32 -mcpu=G3 -mtune=G3"
TARGET_CFLAGS_PPC64="-O2 -pipe -m64 -mcpu=G5 -mtune=G5"
TARGET_CFLAGS_SPARC="-O2 -pipe -m32 -mcpu=v7 -mtune=ultrasparc"
TARGET_CFLAGS_SPARC64="-O2 -pipe -m64 -mcpu=ultrasparc -mtune=ultrasparc"
GCC_CONFIGURE_I386="--with-cpu=i386 --with-tune=pentium3"
GCC_CONFIGURE_X86_64="--with-cpu=x86_64 --with-tune=x86_64"
GCC_CONFIGURE_PPC="--with-cpu=G3 --with-tune=G3"
GCC_CONFIGURE_PPC64="--with-cpu=G5 --with-tune=G5"
GCC_CONFIGURE_SPARC="--with-cpu=v7 --with-tune=ultrasparc"
GCC_CONFIGURE_SPARC64="--with-cpu=ultrasparc --with-tune=ultrasparc"

case ${1} in
	i386)
		TARGET_CFLAGS="${TARGET_CFLAGS_I386}"
		GCC_CONFIGURE="${GCC_CONFIGURE_I386}"
		;;
	x86_64)
		TARGET_CFLAGS="${TARGET_CFLAGS_X86_64}"
		GCC_CONFIGURE="${GCC_CONFIGURE_X86_64}"
		;;
	ppc)
		TARGET_CFLAGS="${TARGET_CFLAGS_PPC}"
		GCC_CONFIGURE="${GCC_CONFIGURE_PPC}"
		;;
	ppc64)
		TARGET_CFLAGS="${TARGET_CFLAGS_PPC64}"
		GCC_CONFIGURE="${GCC_CONFIGURE_PPC64}"
		;;
	sparc)
		TARGET_CFLAGS="${TARGET_CFLAGS_SPARC}"
		GCC_CONFIGURE="${GCC_CONFIGURE_SPARC}"
		;;
	sparc64)
		TARGET_CFLAGS="${TARGET_CFLAGS_SPARC64}"
		GCC_CONFIGURE="${GCC_CONFIGURE_SPARC64}"
		;;
	*)
		echo "Unsupported architecture ${1}"
		echo
		echo "Currently we support :"
		echo
		echo "	i386"
		echo "	ppc"
		echo "	sparc"
		echo "	sparc64"
		echo
		exit
		;;
esac

ARCH_HOST=`/usr/share/automake-1.8/config.sub ${MACHTYPE} | sed -e "s/-/-host_/"`
ARCH_DEST=`/usr/share/automake-1.8/config.sub ${1}-unknown-linux-gnu`
INSTALL_ROOT=${HOME}/cross/${1}
SYS_ROOT=${INSTALL_ROOT}/${ARCH_DEST}/sys-root

PATH=${HOME}/cross/${1}/bin:$PATH
export PATH

set +h

function install_directories () {

mkdir -p ${INSTALL_ROOT}/bin
mkdir -p ${INSTALL_ROOT}/lib
mkdir -p ${SYS_ROOT}/usr/include
mkdir -p ${SYS_ROOT}/lib
mkdir -p ${SYS_ROOT}/usr/lib

}

function install_kernel_headers () {

pushd linux-libc-headers

  if [ ! -d ${SYS_ROOT}/usr/include/linux ]
  then
    (
	cd include
	tar cf - asm-${1} linux scsi sound
    ) | (
	cd ${SYS_ROOT}/usr/include
	tar xf -
	ln -fs asm-${1} asm
    )
  fi

popd

}

function install_binutils () {

mkdir -p binutils-${1}
pushd binutils-${1}

  if [ ! -f config.status ]
  then
    ../binutils/configure \
	--prefix=${INSTALL_ROOT} \
	--host=${ARCH_HOST} \
	--build=${ARCH_HOST} \
	--target=${ARCH_DEST} \
	--with-sysroot=${SYS_ROOT} \
	--disable-nls
  fi

  if [ ! -f ld/ld-new ]
  then
    make -s
  fi

  if [ ! -f ${INSTALL_ROOT}/bin/${ARCH_DEST}-ld ]
  then
    make -s install
  fi

popd

}

function install_glibc_headers () {

mkdir -p glibc_headers-${1}
pushd glibc_headers-${1}

  if [ ! -f config.status ]
  then
    # Assuming GCC 3.x
    CC=gcc \
	../glibc/configure \
	--host=${ARCH_HOST} \
	--build=${ARCH_DEST} \
	--prefix=/usr \
	--without-cvs \
	--disable-sanity-checks \
	--with-headers=${SYS_ROOT}/usr/include \
	--enable-hacker-mode
  fi

  if [ ! -f ${SYS_ROOT}/usr/include/signal.h ]
  then
    make -s sysdeps/gnu/errlist.c
    mkdir -p stdio-common
    touch stdio-common/errlist-compat.c
    make -s cross-compiling=yes \
	install_root=${SYS_ROOT} \
	install-headers
  fi

  if [ ! -f ${SYS_ROOT}/usr/include/gnu/stubs.h ]
  then
    mkdir -p ${SYS_ROOT}/usr/include/gnu
    touch ${SYS_ROOT}/usr/include/gnu/stubs.h
  fi

  if [ ! -f ${SYS_ROOT}/usr/include/features.h ]
  then
    cp ../../glibc/include/features.h ${SYS_ROOT}/usr/include/features.h
  fi

  if [ ! -f ${SYS_ROOT}/usr/include/bits/stdio_lim.h ]
  then
    cp bits/stdio_lim.h ${SYS_ROOT}/usr/include/bits/stdio_lim.h
  fi

popd

}

function install_gcc_core () {

mkdir -p gcc-core-${1}
pushd gcc-core-${1}

  if [ ! -f config.status ]
  then
    ../gcc-core/configure \
	--prefix=${INSTALL_ROOT} \
	--host=${ARCH_HOST} \
	--build=${ARCH_HOST} \
	--target=${ARCH_DEST} \
	${GCC_CONFIGURE} \
	--without-newlib \
	--disable-nls \
	--disable-threads \
	--enable-symvers=gnu \
	--enable-__cxa_atexit \
	--enable-languages=c \
	--disable-shared \
	--with-sysroot=${SYS_ROOT}
  fi

  if [ ! -f gcc/xgcc ]
  then
    make -s 
  fi

  if [ ! -f ${INSTALL_ROOT}/bin/${ARCH_DEST}-gcc ]
  then
    make -s install
  fi

  if [ -d ${INSTALL_ROOT}/lib/gcc-lib/${ARCH_DEST} ]
  then
    GCC_LIB=${INSTALL_ROOT}/lib/gcc-lib
  fi
  if [ -d ${INSTALL_ROOT}/lib/gcc/${ARCH_DEST} ]
  then
    GCC_LIB=${INSTALL_ROOT}/lib/gcc
  fi

  if [ ! -f ${GCC_LIB}/${ARCH_DEST}/*/libgcc_eh.a ]
  then
    pushd ${GCC_LIB}/${ARCH_DEST}/*/
    ln -fs libgcc.a libgcc_eh.a
    popd
  fi

popd

}

function install_glibc () {

mkdir -p glibc-${1}
pushd glibc-${1}

  if [ ! -f config.status ]
  then
    BUILD_CC=gcc \
    CFLAGS="${TARGET_CFLAGS}" \
    CC="${ARCH_DEST}-gcc" \
    AS="${ARCH_DEST}-as" \
    AR="${ARCH_DEST}-ar" \
    LD="${ARCH_DEST}-ld" \
    NM="${ARCH_DEST}-nm" \
    RANLIB="${ARCH_DEST}-ranlib" \
    ../glibc/configure \
	--host=${ARCH_DEST} \
	--build=${ARCH_HOST} \
	--prefix=/usr \
	--without-tls \
	--without-__thread \
	--enable-kernel=2.6.7 \
	--without-cvs \
	--disable-profile \
	--disable-debug \
	--without-gd \
	--enable-add-ons=linuxthreads \
	--with-headers=${SYS_ROOT}/usr/include
  fi

  if [ ! -f ${SYS_ROOT}/etc/rpc ]
  then
    make -s
    make -s install \
	install_root=${SYS_ROOT}
  fi

popd

}

function install_gcc () {

mkdir -p gcc-${1}
pushd gcc-${1}

  if [ ! -f config.status ]
  then
    ../gcc/configure \
	--prefix=${INSTALL_ROOT} \
        --host=${ARCH_HOST} \
        --build=${ARCH_HOST} \
        --target=${ARCH_DEST} \
	${GCC_CONFIGURE} \
        --without-newlib \
        --disable-nls \
        --enable-symvers=gnu \
        --enable-__cxa_atexit \
	--enable-threads=posix \
	--enable-shared \
	--enable-c99 \
	--enable-long-long \
        --with-sysroot=${SYS_ROOT}
  fi

  if [ ! -f ${INSTALL_ROOT}/bin/${ARCH_DEST}-g++ ]
  then
    make -s
    make -s install
  fi

popd

}

install_directories ${1}
install_kernel_headers ${1}
install_binutils ${1}
install_glibc_headers ${1}
install_gcc_core ${1}
install_glibc ${1}
# install_gcc ${1}


