#!/bin/sh

export USER="USER_@REPLACE"
export HOME="HOME_@REPLACE"
export SSH_AUTH_SOCK="SSH_AUTH_SOCK_@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; }
}

cd "PROJECT_ROOT_@REPLACE" && ./scripts/export.sh --schedule=schedule_@REPLACE
check_exit_status $?

fix_ownership

su USER_@REPLACE -c 'cd "PROJECT_ROOT_@REPLACE" && ./scripts/git/push.sh --schedule=schedule_@REPLACE'
