#!/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
#
##################################################################

source /usr/share/slackdev/buildkit.sh

echo "Syncing time from NTP"
ntpdate clock.akamai.com

# Wipe the old build logs:
echo "Removing any old build logs for architecture: $PKGARCH"
rm -fv ${SLKPORTARCH}-$( basename $PWD )-*build.log*
#rm -fv $( basename $PWD )-*build.log*

# 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

# Only -current has a distcc setup - stable releases don't.
# Building Slackware packages on a 'stable' server typically will be conducted within
# '/patches', so we'll disable distcc in such cases:

if [ -f .no-distcc -o ! -z "$( echo $PWD | fgrep /patches/ )" ]; then
   # Build natively/without distcc:
   echo "This source is not to be built with distcc (.no-distcc exists or building in /patches)"
   [ -s .no-distcc ] && 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 $@ || exit 1
 else
   # Build with distcc:
   mkdir -vpm755 /tmp/DISTCC
   TOOLTARGET=$( gcc -v 2>&1| egrep  '^Target:' | awk '{print $2}' )
   if [ "$( readlink /tmp/DISTCC/gcc )" != "/usr/bin/distcc" ]; then
      ( cd /tmp/DISTCC
        for tool in gcc cc g++ c++ ; do
           ln -vfs /usr/bin/distcc $tool
           ln -vfs /usr/bin/distcc ${TOOLTARGET}-${tool}
        done )
   fi

   # Determine the distcc build servers for each architecture:
   case $SLKPORTARCH in
       # ARM only uses prisere as the Orange Pi only has 4 cores
       # and it's always online, so we'll not test for it:
      arm) export DISTCC_HOSTS="prisere/4" ;;
      aarch64)
          # Are the other machines powered up and running distccd?
          timeout 1 bash -c 'cat < /dev/null > /dev/tcp/kitt/3632' > /dev/null 2>&1 && export DISTCC_HOSTS="$DISTCC_HOSTS kitt/8"
          timeout 1 bash -c 'cat < /dev/null > /dev/tcp/turrican/3632' > /dev/null 2>&1 && export DISTCC_HOSTS="$DISTCC_HOSTS turrican/4" ;;
      #   *) ;;
   esac

#   unset DISTCC_HOSTS ; export NUMJOBS="-j$( nproc )"
#   export PATH=$( echo $PATH | sed -e 's?:/tmp/DISTCC:??g' -e 's?/tmp/DISTCC??g' )
#   ./arm/build $@ || exit 1

    # Ensure devices are not overloaded:
#    if [ $NUMJOBS -gt 6 -a ! -z "$( uname -m | grep -i armv7 )" ]; then
#       NUMJOBS="-j15"
#       echo "Restricting to $NUMJOBS"
#       else
#         #echo "Not on ARMv5 - using all numjobs, -j$NUMJOBS"
#         #NUMJOBS="-j$NUMJOBS"
#         NUMJOBS="-j12"
#         echo "Not on ARMv5 - using $NUMJOBS"
#    fi

   # 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)]

   # We'll leave 1 core free to keep the machine running smoothly
   # on the ARM platform.  If there are any particular packages that
   # need tuning, they are configured on a case-by-case basis.
   [ "$SLKPORTARCH" = "arm" ] && export NUMJOBS="-j$(( $(nproc) -1 ))"

    echo "*********************************"
    echo "Toolchain: $TOOLTARGET"
    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++"
    # Instead, let's keep this stuff package specific:
    [ -f .dbuildopts ] && source .dbuildopts
    export PATH=/tmp/DISTCC:$PATH
    if [ -x arm/build ]; then
       echo "** Launching 'arm/build'"
       ./arm/build $@ || exit 1
     else
       echo "** ERROR: No 'arm/build' script found! If you want distcc you need to source it"
    fi
fi
