#!/usr/bin/env bash

# This script is use to update the version of ITK used by ITKPythonPackage. It
# is run nightly by a system with push permissions to the
#
#   git@github.com:InsightSoftwareConsortium/ITKPythonPackage.git
#
# repository.

set -x -e

workdir=/tmp/

cd $workdir

if test ! -d ITKNightlyMaster; then
  git clone --branch nightly-master \
    https://github.com/InsightSoftwareConsortium/ITK.git ITKNightlyMaster
fi
pushd ITKNightlyMaster/
git checkout nightly-master
git fetch
git reset --hard origin/nightly-master
ITK_SHA=$(git rev-parse --short HEAD)
popd

if test ! -d ITKPythonPackage; then
  git clone --branch master \
    git@github.com:InsightSoftwareConsortium/ITKPythonPackage.git ITKPythonPackage
fi
pushd ITKPythonPackage/
git checkout master
git fetch
git reset --hard origin/master
git config user.name 'Kitware Robot'
git config user.email 'kwrobot@kitware.com'
ITKPythonPackage_SHA=$(git rev-parse --short HEAD)
./scripts/update_python_version.py ../ITKNightlyMaster
git add -- CMakeLists.txt itkVersion.py
git commit -m "ITK nightly version update

This commit updates:
  (1) SHA used in CMakeLists.txt to checkout ITK sources
          (InsightSoftwareConsortium/ITK@${ITK_SHA})
  (2) VERSION variable set in itkVersion.py

It was automatically generated by the script ``update_python_version.sh`` [1]

[1] https://github.com/InsightSoftwareConsortium/ITKPythonPackage/blob/${ITKPythonPackage_SHA}/scripts/update_python_version.py"
git push origin master
