#!/bin/bash

if [ $# -eq 1 ]; then
  if [ "$1" == "--help" ]; then
    >&2 echo \
'Usage:  gitolite-sync [options]
Synchronize the git repository $GL_REPO in $GIT_DIR.

This can be used as a post-receive hook in gitolite to sync with other repositories.

Options:
  --help                 display this help and exit
  --version              display version and exit'
    exit
  elif [ "$1" == "--version" ]; then
    echo '0.5'
    exit
  fi
fi

if [ $# -ne 0 ]; then
  >&2 echo 'too many arguments'
  exit 1
fi

declare -A default_remotes
. "/etc/gitolite-sync.conf"

for remote in "${!default_remotes[@]}"; do
  if git -C "${GIT_DIR}" remote \
    | grep -qxF "${remote}"; then
    continue
  fi
  git -C "${GIT_DIR}" remote add "${remote}" "${default_remotes["${remote}"]}${GL_REPO}.git"
done

for remote in "${sync_remotes[@]}"; do
  git -C "${GIT_DIR}" remote \
  | grep -qxF "${remote}" \
  || continue
  git -C "${GIT_DIR}" push \
    --force \
    --prune \
    "${remote}" \
    'refs/heads/*:refs/heads/*' \
    'refs/tags/*:refs/tags/*'
done
