#!/usr/bin/env bash
#=============================================================================
# Copyright (c) 2010 Insight Software Consortium. All rights reserved.
# See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even
# the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the above copyright notices for more information.
#=============================================================================


# Run this script to set up the topic stage for pushing changes.

die() {
	echo 'failure during topic stage setup' 1>&2
	echo '--------------------------------' 1>&2
	echo '' 1>&2
	echo "$@" 1>&2
	exit 1
}

# Make sure we are inside the repository.
cd "$(echo "$0"|sed 's/[^/]*$//')"

if git config remote.stage.url >/dev/null; then
  echo "Topic stage remote was already configured."
else
  echo "Configuring the topic stage remote..."
  git remote add stage http://itk.org/stage/ITK.git || \
    die "Could not add the topic stage remote."
  git config remote.stage.pushurl git@itk.org:stage/ITK.git
fi

read -ep "Do you have git push access to itk.org? [y/N]: " access
if test "$access" = "y"; then
  # We will have the private key corresponding the public key at itk.org at
  # ~/.ssh/id_git_itk.  This allows the developer to keep a single public key
  # on file with the server across multiple machines.
  if ! grep -q 'Host itk.org' ~/.ssh/config 2>/dev/null; then
    echo "Configuring the IdentityFile for itk.org to be ~/.ssh/id_git_itk..."
    mkdir -p ~/.ssh
    chmod og-rwx ~/.ssh
    echo "Host itk.org" >> ~/.ssh/config
    echo "  IdentityFile=~/.ssh/id_git_itk" >> ~/.ssh/config
    chmod 600 ~/.ssh/config
  fi
  if ! test -e ~/.ssh/id_git_itk; then
    if test -f ~/.ssh/id_rsa; then
      # Take care of the common case.
      pushd ~/.ssh >/dev/null
      ln -s id_rsa id_git_itk
      popd >/dev/null
      cat << EOF

Assuming ~/.ssh/id_rsa is the private key corresponding to the public key given
for the 'git' user at itk.org.  If this is not the case, please place the
appropriate private key at ~/.ssh/id_git_itk.

EOF
      read -e -n 1 -p "Press any key to continue..."
    else
      cat << EOF

Please place the private key corresponding to the public key registered at
itk.org in '~/.ssh/id_git_itk'.

EOF
      read -e -n 1 -p "Press any key to continue..."
    fi
  fi
  echo "Testing ssh capabilities..."
  git ls-remote git@itk.org:stage/ITK.git refs/heads/master || die "ssh test to git@itk.org failed."
  echo "Test successful!"
fi

echo "Done."

Add script to setup the git topic stage remote.
Add script to setup the git topic stage remote.

Change-Id: I6dbb7049aa748ba63e4545165b96054969a01448
