#!/usr/bin/env sh

hook() {
  local hook=.git/hooks/$1.sh
  # compat without extname
  if test ! -f $hook; then
    hook=.git/hooks/$1
  fi

  if test -f $hook; then
    echo "... $1"
    if test -x $hook; then
      $hook
    else
      . $hook
    fi
  fi
}

if test $# -gt 0; then
  version=$1
  shift #remove version from arguments list
  remote=''
  hook pre-release
  echo "... releasing $version"
  # check for flags
  while test $# != 0
  do
    case "$1" in
    -c) git-changelog -t "$version" ;;
    -r) remote=$2; shift ;;
    -m) msg=$2; shift ;;
    --) shift; break;;
    *)  usage ;;
    esac
    shift
  done
  if [ -z "$msg" ]; then
      msg="Release ${version}"
  fi
  git commit -a -m "$msg"
  git tag $version -a -m "$msg" \
    && git push $remote \
    && git push $remote --tags \
    && hook post-release \
    && echo "... complete"
else
  echo "tag required" 1>&2 && exit 1
fi
