From 4e93aaa02d8ac2a0f8a1d024e091642d8f12c41b Mon Sep 17 00:00:00 2001
From: Andre Anjos <andre.anjos@idiap.ch>
Date: Fri, 23 Sep 2016 12:15:09 +0200
Subject: [PATCH] Allow automatic update of build scripts if necessary

---
 gitlab/before_deploy.sh |  3 +++
 gitlab/before_test.sh   |  3 +++
 gitlab/functions.sh     |  4 +---
 gitlab/install.sh       | 19 ++++++++++++++++---
 4 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/gitlab/before_deploy.sh b/gitlab/before_deploy.sh
index 25fc0ba..ca6c3d6 100755
--- a/gitlab/before_deploy.sh
+++ b/gitlab/before_deploy.sh
@@ -1,6 +1,9 @@
 #!/usr/bin/env bash
 # Wed 21 Sep 2016 13:08:05 CEST
 
+# Refreshes build scripts if necessary
+$(dirname ${0})/install.sh $(dirname ${0})
+
 source $(dirname ${0})/functions.sh
 
 run_cmd $(dirname ${0})/before_test.sh
diff --git a/gitlab/before_test.sh b/gitlab/before_test.sh
index e348f8d..f24b8ed 100755
--- a/gitlab/before_test.sh
+++ b/gitlab/before_test.sh
@@ -1,6 +1,9 @@
 #!/usr/bin/env bash
 # Wed 21 Sep 2016 13:08:05 CEST
 
+# Refreshes build scripts if necessary
+$(dirname ${0})/install.sh $(dirname ${0})
+
 source $(dirname ${0})/functions.sh
 
 run_cmd $(dirname ${0})/before_build.sh
diff --git a/gitlab/functions.sh b/gitlab/functions.sh
index 8b6d09a..e4e0ad8 100644
--- a/gitlab/functions.sh
+++ b/gitlab/functions.sh
@@ -55,8 +55,6 @@ run_cmd() {
   if [ ${status} != 0 ]; then
     log_error "Command Failed \"${@}\""
     exit ${status}
-  else
-    log_info "Finished comand \"${@}\""
   fi
 }
 
@@ -97,7 +95,7 @@ doc_upload() {
   check_pass DOCUSER
   check_pass DOCPASS
   log_info "${1} -> ${DOCSERVER}/${2}, via curl..."
-  curl --silent --user "${DOCUSER}:${DOCPASS}" --upload-file ${1} ${DOCSERVER}/${2}
+  curl --location --silent --user "${DOCUSER}:${DOCPASS}" --upload-file ${1} ${DOCSERVER}/${2}
   local status=$?
   if [ ${status} != 0 ]; then
     log_error "Curl command finished with an error condition (status=${status})"
diff --git a/gitlab/install.sh b/gitlab/install.sh
index 157d57e..fa0b05f 100755
--- a/gitlab/install.sh
+++ b/gitlab/install.sh
@@ -29,14 +29,26 @@ run_cmd() {
   if [ ${status} != 0 ]; then
     log_error "Command Failed \"${@}\""
     exit ${status}
-  else
-    log_info "Finished comand \"${@}\""
   fi
 }
 
 
 get_script() {
-  run_cmd curl --silent --output ${1}/${2} "https://gitlab.idiap.ch/bob/bob.admin/raw/master/gitlab/${2}"
+  local curlopt="--location --output ${1}/${2}"
+  if [ -e ${1}/${2} ]; then #check for updates
+    local checkopt="${curlopt} --time-cond ${1}/${2} --silent"
+    checkopt="${checkopt} --write-out %{http_code}"
+    if [ "$(curl ${checkopt})" == "200" ]; then
+      log_info "CI script ${1}/${2} needs updating..."
+      rm -f ${1}/${2}
+    else
+      log_info "CI script ${1}/${2} is up-to-date"
+      return 0
+    fi
+  fi
+  # check file needs updating
+  local url="https://gitlab.idiap.ch/bob/bob.admin/raw/master/gitlab/${2}"
+  run_cmd curl ${curlopt} ${url}
 }
 
 
@@ -48,6 +60,7 @@ get_exec() {
 
 run_cmd mkdir -pv ${1}
 get_script ${1} functions.sh
+get_script ${1} install.sh
 for stage in "build" "test" "wheels" "deploy"; do
   get_exec ${1} before_${stage}.sh
   get_exec ${1} ${stage}.sh
-- 
GitLab