#!/bin/bash
#
# Enter
#   ./alltools help
# for additional information

# Initialisation
# --------------
HighLightNeutral="\033[1;37;44m"   # White on blue
HighLightGood="\033[1;37;42m"      # White on green
HighLightBad="\033[1;37;41m"       # White on red
HighLightOff="\033[0m"

SetTitleCommandStart="\033]0;"
SetTitleCommandEnd='\007'


if [ "$1" == "" ]; then
   DoClean=0
   DoDep=1
   DoCompile=1
else
   if [ "$1" == "clean" ]; then
      DoClean=1
      DoDep=0
      DoCompile=0
      shift
   else
      if [ "$1" == "dep" ] || [ "$1" == "makemake" ] || [ "$1" == "qmake" ] || [ "$1" == "qmake-qt4" ]; then
         DoClean=0
         DoDep=1
         DoCompile=0
         shift
      else
         if [ "$1" == "make" ]; then
            DoClean=0
            DoDep=0
            DoCompile=1
            shift
         else
            if [ "$1" == "all" ]; then
               DoClean=1
               DoDep=1
               DoCompile=1
               shift
            else
               if [ "$1" == "help" ] || [ "$1" == "--help" ] || [ "$1" == "--h" ] || [ "$1" == "?" ]; then
                  echo -e "Help for alltools"
                  echo -e "-----------------"
                  echo -e "Use this script for building all the CAT tools. The following commands are supported:"
                  echo -e "   ./alltools help      Show this help text"
                  echo -e "   ./alltools           Rebuild the dependencies and call make"
                  echo -e "   ./alltools clean     Delete all the object, executable, etc. files"
                  echo -e "   ./alltools dep       Rebuild the dependencies"
                  echo -e "   ./alltools makemake  Same as ./alltools dep"
                  echo -e "   ./alltools make      Call make"
                  echo -e "   ./alltools all       Clean everything, then build the dependencies and compile"
                  exit 0
               else
                  echo -e "$HighLightBad Error: Unknown command $1 $HighLightOff"
                  exit 1
               fi
            fi
         fi
      fi
   fi
fi

Parameters=$@


# Subroutine calling make and checking for errors
# -----------------------------------------------

check_return_code()
{
   ReturnCode=$?
   Command=$1
   if [ "$ReturnCode" -ne "0" ]; then
     echo -e "$HighLightBad Error: '$Command' returned $ReturnCode in tool '$SubDir' $HighLightOff"
     echo -e "$SetTitleCommandStart $SetTitleCommandEnd"
     exit 1
   fi
}

call_make_in_subdir()
{
   SubDir=$1
   DepCommand=$2
   OldDir=`pwd`

   echo
   echo -e "$HighLightNeutral Processing tool '$SubDir' $HighLightOff"
   cd $SubDir
   echo -e "$SetTitleCommandStart NOW PROCESSING: tools/$SubDir       $SetTitleCommandEnd"

   if [ "$DoClean" == "1" ]; then
      Command="make clean"
      echo -e "$Command"
      $Command
      check_return_code "$Command"
   fi

   if [ "$DoDep" == "1" ]; then
      Command=$DepCommand
      echo -e "$Command"
      $Command
      check_return_code "$Command"
   fi

   if [ "$DoCompile" == "1" ]; then
      NumCpus=`cat /proc/cpuinfo  | grep ^processor | wc -l`
      Command="make -j"$NumCpus
      echo -e "$Command"
      $Command
      check_return_code "$Command"
   fi

   cd $OldDir
}


if [ "$DoClean" == "1" ]; then
   Command="rm -f ./lib/*"
   echo -e "$Command"
   $Command
   check_return_code "$Command"
fi

# Generate library version from top entry found in debian/changelog
# -----------------------------------------------------------------
#head -n 1 debian/changelog | awk '{ Version = $2
#                                    gsub ("\\(", "", Version)
#                                    gsub ("\\)", "", Version)
#                                    print "VERSION = " Version}' > libguytools_version.pro.inc

TOOLS_VERSION=`head -n 1 debian/changelog`
TOOLS_VERSION="${TOOLS_VERSION%%)*}"
TOOLS_VERSION="${TOOLS_VERSION##*(}"
echo "VERSION = ${TOOLS_VERSION%%-*}" >libguytools_version.pro.inc


# List of all sub projects
#
#                   Sub-           Command for building
#                   directory      dependencies
# ---------------------------------------------------

call_make_in_subdir error            "qmake-qt4"
call_make_in_subdir sysinfo          "qmake-qt4"
call_make_in_subdir log              "qmake-qt4"
call_make_in_subdir signal           "qmake-qt4"
call_make_in_subdir cfg              "qmake-qt4"

echo -e "$HighLightGood Successfully finished $HighLightOff"
echo -e "$SetTitleCommandStart $SetTitleCommandEnd"

