#!/bin/bash
# When recovering the broken svn-git migration,
# we needed to temporarily disable the commit email notifications.

# Stop on errors.
set -e

# Check that ~/.netrc knows github
. github-secrets

organization="yast"
# Get the list from http://zq1.de/bernhard/yast-broken-list.txt
repolist=`sed 's/^/yast-/' yast-broken-list.txt`

for repo in $repolist
do
  echo -n $repo...
  curl -f -s -n https://api.github.com/repos/${organization}/${repo}/hooks -o ${json}.list
  if [ $(grep '"id"' ${json}.list | wc -l) != 1 ]; then
	  echo "$repo has more than one hook. fix manually"
	  touch ${repo}.error
	  continue
  fi
  ID=$(sed -n 's/.*"id": \([0-9]*\).*/\1/;T;p;q' ${json}.list)
  json=json-${repo}
  cat > ${json} << EOJSON
  {
  "name": "email",
  "active": false,
  "config": {
    "address": "yast-commit@opensuse.org"
  }
  }
EOJSON
  curl -f -s -n -X PATCH -d @${json}  https://api.github.com/repos/${organization}/${repo}/hooks/$ID -o ${json}.result
  echo " OK"
done
