#!/bin/bash

set -o pipefail
set -e

setman-log notice "lwr-isc restore started"
/usr/bin/timeout 800 sh -c 'while ! ss -H -t -l -n sport = :8000 | grep -q "^LISTEN.*:8000"; do sleep 1; done'
setman-log notice "lwr-isc restore: '/usr/bin/timeout' returned with value $?"

#Workaround for 1.6 -> 2.0 update:
#First setman-restore's purpose is to check if backup is missing; then stops executing this script in case of failure
#(set -e is called, so if setman-restore exits with error, this script also exits)
setman-restore "laraConfiguration" "isc_config.zip" > /dev/null
setman-restore "laraConfiguration" "isc_config.zip" | curl --silent --fail -H "Content-Type:application/zip" --data-binary @- http://localhost:8000/lara/api/configuration
