#!/bin/bash 

TOPDIR=`pwd`
BINUTILSDISTO_LIST="binutils-2.12"
GCCDISTO_LIST="gcc-3.0.4 gcc-3.1"
TARFLAGS=xf
TARDIR=$TOPDIR/Zipped
PREFIX=

TARGET_LIST="sparc-sun-solaris"


###############################################################################
#
# Function    :
# Description :
#
###############################################################################

read_target()
{
  
  echo
  echo Linux crosscompiler building script,
  echo

  #
  # only prompt if we're missing information
  #
  if [ x${PREFIX} = x ] || [ x${TARGET} = x ] || [ x${TARDIR} = x ] ; then

     echo ""
     PS3="Please select a target: "
     select TARGET in ${TARGET_LIST};
	 do echo; break; done

     echo ""
     PS3="Please select a GCC distribution: "
     select GCCDISTO in ${GCCDISTO_LIST};
	 do echo; break; done

     echo ""
     PS3="Please select a binutils distribution: "
     select BINUTILSDISTO in ${BINUTILSDISTO_LIST};
	 do echo; break; done

     PREFIX=${TOPDIR}/${TARGET}_${GCCDISTO}

     echo ""
     echo "Install the tools where?"
     read -p "[${PREFIX}]: "

     if [ x${REPLY} != x ]; then 
	PREFIX=$REPLY;
     fi
 
     echo ""
     echo "Tarballs are where?"
     read -p "[${TARDIR}]: "

     if [ x${REPLY} != x ]; then 
	TARDIR=${REPLY}; 
     fi

  fi

  #
  # test that we have write permissions to the install dir
  #
  mkdir -p ${PREFIX}
  touch ${PREFIX}/test-if-write
  if [ ! -f ${PREFIX}/test-if-write ]; then
      echo "You don't appear to have write permissions to ${PREFIX}."
      echo "You must fix that before continuing."
      exit
  else 
      rm -f ${PREFIX}/test-if-write
  fi

  echo "----------------------------------------------------------------------"
  echo Building for:
  echo "    --target=$TARGET"
  echo "    --prefix=$PREFIX"
  echo "    tarballs are at $TARDIR"

  export PATH=${PATH}:${PREFIX}/bin
}


###############################################################################
#
# Function    :
# Description :
#
###############################################################################

prepare_headers_and_libs()
{
  echo ------------------------------------------------------------------------
  echo 
  echo Preparing headers end libs for $TARGET target
  echo 

  case $TARGET in

      sparc-sun-solaris)

          mkdir ${PREFIX}/copied-includes
          mkdir ${PREFIX}/copied-libs
	  cd ${PREFIX}/copied-includes
	  gunzip -c ${TARDIR}/${TARGET}_incs.tar.gz | tar $TARFLAGS -
	  cd ${PREFIX}/copied-libs
	  gunzip -c ${TARDIR}/${TARGET}_libs.tar.gz | tar $TARFLAGS -
	  cd ${PREFIX}/copied-libs

 	  ;; 
  esac
}

###############################################################################
#
# Function    :
# Description :
#
###############################################################################

build_binutils()
{
  echo ------------------------------------------------------------------------
  echo
  echo Building binutils ${BINUTILSDISTO}
  echo
  

  cd ${TOPDIR}
  tar xzf ${TARDIR}/${BINUTILSDISTO}.tar.gz 
  mkdir ${TOPDIR}/${BINUTILSDISTO}-build; 
  cd ${TOPDIR}/${BINUTILSDISTO}-build

  ../${BINUTILSDISTO}/configure --target=$TARGET --prefix=$PREFIX > ${PREFIX}/Build.log 2>&1
  make all install >> ${PREFIX}/Build.log 2>&1
  cd ${TOPDIR}

  #
  # test to see if this step passed
  #
  if [ ! -f ${PREFIX}/bin/${TARGET}-ld ]; then
      echo Build failed during binutils 
      exit 1
  fi
}

###############################################################################
#
# finally, build a full-up gcc c/c++ compiler
#
###############################################################################

build_gcc()
{
  echo ------------------------------------------------------------------------
  echo
  echo Building gcc. ${GCCDISTO}
  echo


  cd ${TOPDIR}
  tar xzf ${TARDIR}/${GCCDISTO}.tar.gz
  mkdir ${TOPDIR}/${GCCDISTO}-build

  #
  #patch libstdc++ configure.in to pickup the right bits headers
  #
  case $GCCDISTO in
       gcc-3.0.4)
       PATCHFILE=${TARDIR}/patch_gcc304_libstdc_configure.in
       ;;

       gcc-3.1)
       PATCHFILE=${TARDIR}/patch_gcc31_libstdc_configure.in
       ;;

       *)
       ;;
  esac

  if [ "${GCCDISTO}" = "gcc-3.0.4" -o "${GCCDISTO}" = "gcc-3.1" ] ; then
     echo "Patching libstdc++-v3 configure script..."
     cd ${TOPDIR}/${GCCDISTO}/libstdc++-v3
     patch < ${PATCHFILE}
     autoconf configure.in > configure
     echo "Done"
  fi

  cd ${TOPDIR}/${GCCDISTO}-build

  #
  # GCC Configuration
  #
  case $TARGET in

	sparc-sun-solaris)

        ../${GCCDISTO}/configure --target=$TARGET --prefix=$PREFIX \
            --with-headers=${PREFIX}/copied-includes/include \
	    --with-libs=${PREFIX}/copied-libs/lib \
	    --enable-languages=c,c++ --without-newlib  \
            --with-gnu-as --with-gnu-ld >> ${PREFIX}/Build.log 2>&1
	;;

  esac

  #
  # GCC Build
  #
  case $TARGET in

      sparc-sun-solaris)
  
      # everyone else can use the standard procedure
      make all install >> ${PREFIX}/Build.log 2>&1


      # test to see if this step passed
      if [ ! -f ${PREFIX}/bin/${TARGET}-gcc ]; then
          echo Build failed during gcc && exit 1
      fi
  
      ;;

  esac

}

###############################################################################
#
#  Main Stuff
#
###############################################################################

read_target
#prepare_headers_and_libs
#build_binutils
build_gcc


