#!/bin/bash
[ $# -lt 2 ]  && { echo "usage: $0 <tdaq-common-version> <tdaq-version> [ <lcg-version> ]"; exit 1; }

common=$(printf "tdaq-common-%02d-%02d-%02d" $(echo $1 | tr '\.' ' '))
tdaq=$(printf "tdaq-%02d-%02d-%02d" $(echo $2 | tr '\.' ' '))

if [ ! -d tdaq-common-cmake ]; then
   git clone -q https://:@gitlab.cern.ch:8443/atlas-tdaq-software/tdaq-common-cmake.git || exit 2
   (cd tdaq-common-cmake; git checkout -b ${common})
fi

if [ ! -d tdaq-cmake ];then 
 git clone -q https://:@gitlab.cern.ch:8443/atlas-tdaq-software/tdaq-cmake.git || exit 2
 (cd tdaq-cmake; git checkout -b ${tdaq})
fi

sed -i -e "s;set(TDAQ_COMMON_VERSION .*;set(TDAQ_COMMON_VERSION $1 CACHE STRING \"tdaq-common version number\");" tdaq-common-cmake/CMakeLists.txt tdaq-cmake/CMakeLists.txt
if [ $# -gt 2 ]; then
   sed -i -e "s;set(LCG_VERSION_CONFIG .*;set(LCG_VERSION_CONFIG $3 CACHE STRING \"LCG version\");" tdaq-common-cmake/CMakeLists.txt
fi
sed -i -e "s;set(TDAQ_VERSION .*;set(TDAQ_VERSION $2 CACHE STRING \"TDAQ version number\");" tdaq-cmake/CMakeLists.txt
sed -i -e "s;    TDAQ_COMMON_VERSION:.*;    TDAQ_COMMON_VERSION: ${common};" -e "s;    # TDAQ_COMMON_VERSION:.*;    TDAQ_COMMON_VERSION: ${common};" tdaq-cmake/.gitlab-ci.yml

echo "###### tdaq-common ######"
(cd tdaq-common-cmake; git diff)
echo
echo
echo "###### tdaq ######"
(cd tdaq-cmake; git diff)

echo
echo
echo "##### Next Steps #####"
echo "If you are happy:"
echo "(cd tdaq-common-cmake; git push origin ${common})"
echo "(cd tdaq-cmake; git push origin ${tdaq})"
