#!/bin/bash
#
# check whether current directory is inside a git repository
#

is_git_repo() {
  git rev-parse --show-toplevel > /dev/null 2>&1
  result=$?
  if test $result != 0; then
    >&2 echo 'Not a git repo!'
    exit $result
  fi
}

is_git_repo

hook() {
  local hook=.git/hooks/$1.sh
  if test -f $hook; then
    echo "... $1"
    . $hook
  fi
}

if test $# -gt 0; then
  hook pre-release
  echo "... releasing $1"
  git commit -a -m "Release $1"
  git tag $1 -a -m "Release $1" \
    && git push $2 \
    && git push $2 --tags \
    && hook post-release \
    && echo "... complete"
else
  echo "tag required" 1>&2 && exit 1
fi
