#!/bin/sh

export USER="USER_@REPLACE"
export HOME="HOME_@REPLACE"
export SSH_AUTH_SOCK="SSH_AUTH_SOCK_@REPLACE"
export DBUS_SESSION_BUS_ADDRESS="DBUS_SESSION_BUS_ADDRESS_@REPLACE"

git config --global --unset safe.directory "PROJECT_ROOT_@REPLACE"
git config --global --add   safe.directory "PROJECT_ROOT_@REPLACE"

fix_ownership() {
  chown -R USER_@REPLACE:USER_@REPLACE "PROJECT_ROOT_@REPLACE"
}
check_exit_status() {
  [ "$1" -eq 0 ] || { fix_ownership; exit 1; }
}

su USER_@REPLACE -c 'cd "PROJECT_ROOT_@REPLACE" && ./scripts/git/pull.sh --schedule=schedule_@REPLACE'
check_exit_status $?

su USER_@REPLACE -c 'cd "PROJECT_ROOT_@REPLACE" && ./scripts/import.sh --only-dconfs --schedule=schedule_@REPLACE'
check_exit_status $?

cd "PROJECT_ROOT_@REPLACE" && ./scripts/import.sh --only-files --schedule=schedule_@REPLACE

fix_ownership
