#!/bin/bash
#
# prepare_patches <package1> <package2>...
#
#    prepare_patches ipc is
#
# This script should be called after cm_setup <release> <configuration>  
# for which you want to build patches. The location should be the
# tdaq-common-cmake or tdaq-cmake checked out package without
# submodules (they will be wiped out anyway).
#
NUMJOBS=${NUMJOBS:=4}

usage()
{
    echo "prepare_patches <packages>..."
    echo "   <packages>       : ipc is oh ..."
}

packaging=package

while [ $# -gt 0 ]
do
    case $1 in
        --no-rpms)
            packaging=
            shift
            ;;
        *)
            break
            ;;
    esac
done

[ $# -gt 0 ] || { usage; exit 1; }

[ -n "${CMTCONFIG}" ] || { echo "No CMTCONFIG set, do a cm_setup first"; exit 1; }

# Check
[ -f CMakeLists.txt ] || { echo "You should run this from the top level project area"; exit 1; }

# Clean up area, remove all submodules
echo "Cleaning up submodules"
git submodule -q deinit --all

git submodule -q update -i $@

echo "Checked out packages"
echo "--------------------"
git submodule status $@ | cut -d' ' -f3-

cmake_config ${CMTCONFIG} -- -D TDAQ_PATCH_AREA=1 
cd ${CMTCONFIG}
make -j ${NUMJOBS} all 
result=$?
if [ ! -z "${packaging}" ]; then
  env -u PYTHONHOME make ${packaging}
else
  exit ${result}
fi
