#!/bin/bash


# Automatically determine architecture for build & packaging:
case "$( uname -m )" in
  # The i486 here just means "x86".
  i?86)   export LOCALARCH=i486
          export SLKCFLAGS="-O3" ;;
  x86_64) export LOCALARCH=$( uname -m ) 
          export SLKCFLAGS="-O3 -fPIC" ;;
esac
echo "Local architecture: $LOCALARCH"

NUMJOBS="-j$(awk '/^processor[[:space:]]*:/{n=$3} END{print n+1}' /proc/cpuinfo)"

# Versions of packages:
#BINUTILSVER=2.22.52.0.2
BINUTILSVER=2.23.51.0.6
GCCVER=4.7.2
GLIBCVER=2.15

################## Functions ##############################################
# Functions:
#

# Determine patch level required & apply the patch:
function auto_apply_patch () {
 patchfile=$1

 echo
 echo "***********************************************************"
 echo "** Working on Patch: $patchfile"
 echo "***********************************************************"
 echo

 # Decompress the patch if it's compressed with a known method:
 FTYPE=$( file $patchfile )
 case "$FTYPE" in
    *xz*compressed*)
        xz -dc $patchfile > $TMP/$(basename $patchfile).unpacked
        patchfile=$TMP/$(basename $patchfile).unpacked ;;
    *bzip2*compressed*)
        bzcat -f $patchfile > $TMP/$(basename $patchfile).unpacked
        patchfile=$TMP/$(basename $patchfile).unpacked ;;
    *gzip*compressed*)
        zcat -f $patchfile > $TMP/$(basename $patchfile).unpacked
        patchfile=$TMP/$(basename $patchfile).unpacked ;;
 esac

 # By now the patch is decompressed or wasn't compressed originally.
 #
 # Most patches should not require more levels than this:
 success=0
 for (( pl=0 ; pl<=5 ; pl++ )) ; do
   echo "Patch : $patchfile , trying patch level $pl"
     patch -N --fuzz=20 -t --dry-run -p$pl < $patchfile > /dev/null 2>&1 && success=1 && break
   done
 if [ $success = 1 ]; then
    echo "Patch: $patchfile will apply at level $pl"
    patch -N --fuzz=20 --verbose -p$pl < $patchfile
    return 0
  else
    echo "Patch: $patchfile failed to apply at levels 0-5"
    return 1
 fi
}


function build_gcc () {

cat << "EOF"
###########################################################################
################## Build gcc ##############################################
###########################################################################
EOF

# Extract source:
cd $TMP
echo "Unpacking gcc source..."
tar xf $CWD/sources/gcc/gcc-$GCCVER.tar.xz || exit 1
# Add this symlink to ease application of the patches from debian:
rm -rf src
ln -vfs gcc-$GCCVER src 
cd gcc-* || exit 1

########### Apply patches #################################################

# Apply Debian patches:
xz -dc $CWD/sources/gcc/gcc*diff.xz | patch -p1 || exit 1

# Go back into the parent directory
cd ..

# This list is taken from the Debian Build log for the particular
# patch release we're using, with a few redundant ones stripped.
#pushd src/debian/patches
#rm -f gcc-linaro-updates.diff # Tracking file rather than any actual patch code.
# The normal 'svn-updates.diff' is replaced with svn-updates-linaro.diff
# 'svn-doc-updates' also no longer applies with gcc-linaro-doc.diff.
# Anything surrounded by two \'s is a linaro patch.
for patchf in \
  \
  \
  gcc-linaro.diff \
  svn-updates-linaro.diff \
  gcc-linaro-doc.diff \
  \
  hjl-x32-gcc-4_7-branch.diff \
  gcc-gfdl-build.diff \
  \
  \
  aarch64-multiarch.diff \
  aarch64-hash-style-gnu.diff \
  \
  \
  aarch64-libffi.diff \
  aarch64-libffi-testsuite.diff \
  gcc-textdomain.diff \
  gcc-driver-extra-langs.diff \
  gcc-hash-style-gnu.diff \
  libstdc++-pic.diff \
  libstdc++-doclink.diff \
  libstdc++-man-3cxx.diff \
  libstdc++-test-installed.diff \
  libjava-stacktrace.diff \
  libjava-jnipath.diff \
  libjava-sjlj.diff \
  libjava-disable-plugin.diff \
  alpha-no-ev4-directive.diff \
  boehm-gc-getnprocs.diff \
  note-gnu-stack.diff \
  libgomp-omp_h-multilib.diff \
  sparc-force-cpu.diff \
  pr24619.diff \
  pr45078.diff \
  pr47818.diff \
  pr49940.diff \
  pr49944.diff \
  libffi-kfreebsd.diff \
  libffi-powerpc-sf.diff \
  libffi-powerpc-sysv-without-string-ops.diff \
  mudflapth-link.diff \
  libffi-m68k.diff \
  pr26155.diff \
  arm-no-va_list-warn.diff \
  libmudflap-x32.diff \
  pr54411.diff \
  hurd-pthread.diff \
  pr54974.diff \
  libgo-testsuite.diff \
  gcc-target-include-asm.diff \
  libgcc-backports.diff \
  ppl-version.diff \
  gcc-cloog-dl.diff \
  libjava-armel-unwind.diff \
  arm-dynamic-linker.diff \
  arm-multilib-defaults.diff \
  gcc-ice-hack.diff \
  gcc-ice-apport.diff \
  libjava-fixed-symlinks.diff \
  libstdc++-arm-wno-abi.diff \
  ada-mips.diff \
  libffi-ro-eh_frame_sect.diff \
  libjava-multiarch.diff \
  libjava-nobiarch-check.diff \
  gcc-powerpc-undef.diff \
  mips-fix-loongson2f-nop.diff \
  libgomp-kfreebsd-testsuite.diff ; do
      popd >/dev/null 
      auto_apply_patch src/debian/patches/${patchf} || exit 1
  done

}

CWD=$PWD

# Temp build locations:
TMP=/tmp/xbuild
rm -rf $TMP
mkdir -p $TMP

build_gcc || exit 1
