#!/bin/bash

################################################################
# Script:  dbuild
# Purpose: Start the Slackware ARM package build scripts using
#          distcc to a number of specified compile hosts.
# Author:  Stuart Winter <mozes@slackware.com>
# Date:    31-Aug-2010
# Version: 0
################################################################
# Not for distribution because this script was written to be
# modified for specific purposes rather than be called with a
# number of command line operators.
################################################################
# Copy 'dbuild' into a location in your $PATH
# 
# Usage: 
#   cd source/a/bash  # for example
#   dbuild arm/build
#   
# Make sure we're not running on the older ARMedslack legacy ABI
# since we don't have distcc setup with a compiler for oldabi anymore -
# everything is built natively.


# To rip out the useful bits of this into a scrpit:
# eval $( egrep "export NUMJOBS|export DISTCC_HOSTS|PATH=/tmp/DISTCC" ~/armedslack/dbuild |grep -v "^#" ) > /dev/null 2>&1

# Also check distcc -dumpspecs | grep eabi
# to automate further.
# Version5 ABI changed with new gcc4.4.2?
if [ ! -z "$( readelf -h /bin/ls | egrep "Version.*EABI" )" ]; then

   EABI=gnueabi-

   if [ -f .no-distcc ]; then
      echo "This source is not to be built with distcc (.no-distcc exists)"
      cat .no-distcc
      # Incase we're being run recursively (as KDE.SlackBuild does)
      unset NUMJOBS DISTCC_HOSTS
      export PATH=$( echo $PATH | sed -e 's?:/tmp/DISTCC:??g' -e 's?/tmp/DISTCC??g' )
      ./arm/build
    else   
      if [ ! -d /tmp/DISTCC ]; then
         mkdir -p /tmp/DISTCC
         ( cd /tmp/DISTCC
           ln -vfs /usr/bin/distcc gcc
           ln -vfs /usr/bin/distcc cc
           ln -vfs /usr/bin/distcc g++
           ln -vfs /usr/bin/distcc c++
           ln -vfs /usr/bin/distcc arm-slackware-linux-${EABI}c++
           ln -vfs /usr/bin/distcc arm-slackware-linux-${EABI}g++
           ln -vfs /usr/bin/distcc arm-slackware-linux-${EABI}gcc )
      fi

#      export DISTCC_HOSTS="turrican/2 prisere/4 kitt/2 goose/1 trance/1 kato/1"

      export DISTCC_HOSTS="turrican/2 prisere/4 trance/1 kato/1"

      # For testing distcc problems: easier to run on one host.
#       export DISTCC_HOSTS="prisere/4"

      # Determine the number of parallel jobs based on the number of 
      # distcc hosts & their cores:
      export NUMJOBS=-j$[$(sed 's?[^0-9]*/?+?g' <<<$DISTCC_HOSTS)]

      echo "*********************************"
      echo "distcc hosts: $DISTCC_HOSTS"
      echo "Number of jobs: $NUMJOBS"
      echo "*********************************"
      # Doing this MAY break stuff in the future if a remote compilation
      # fails and the build tries to build locally.
      # However, most build scripts will quit if this happens so you can fix it.
      # Also doing this makes l/qt work with distcc, which saves the best part of a day.
      # but breaks stuff like xap/electricsheep
      # export CC="distcc gcc" CXX="distcc g++"
      PATH=/tmp/DISTCC:$PATH ./arm/build $@

   fi

  else
  echo "*** Old ABI detected: running natively ***"
  # We're on Old ABI -- Run it natively:
  ./arm/build $@
fi

