#!{{ shell_binary }}
LC_ALL=C
export LC_ALL
umask 077
restore_element () {
  backup_path=$1
  restore_path=$2
  shift 2
  element_list=$*
  for element in $element_list
  do
    cd $backup_path;
    if [ -f $element ] || [ -d $element ]; then
       {{ rsync_binary }} -av --delete $backup_path/$element $restore_path;
    fi
  done
}

write_backup_proof () {
  cd {{ directory['backup'] }}
  find -type f ! -name backup.signature ! -wholename "./rdiff-backup-data/*" -print0 | xargs -P4 -0 sha256sum  | LC_ALL=C sort -k 66 > {{ directory['srv'] }}/proof.signature
  diff -ruw {{ directory['backup'] }} {{ directory['srv'] }}/proof.signature > {{ directory['srv'] }}/backup.diff
}

# For now we just make the diff before 
write_backup_proof

restore_element {{ directory['backup'] }}/runner/ {{ directory['srv'] }}/runner  instance project  proxy.db
restore_element  {{ directory['backup'] }}/etc/ {{ directory['etc'] }} config.json ssh
cp -r {{ directory['backup'] }}/etc/.??* {{ directory['etc'] }};
{{ curl_binary }} --insecure -vg6L --max-time 5  {{ backend_url }}/isSRReady;