#!/bin/sh

SELF="$( basename $0 )"

warn() {
  echo "$SELF: warning: $@" 1>&2
}

die() {
  echo "$SELF: fatal: $@" 1>&2
  exit 1
}

# read config fresh, rather than use the env.
if [ -e ~/.sbostuff.cfg ]; then
  source ~/.sbostuff.cfg
else
  warn "can't find ~/.sbostuff.cfg"
fi

NAME="$1"
EMAIL="$2"

if [ "$NAME" = "me" -a "$EMAIL" = "" ]; then
  if [ "$SBO_NAME" = "" -o "$SBO_EMAIL" = "" ]; then
  die "can't use 'me' without SBO_NAME and SBO_EMAIL in the env"
  fi
  NAME="$SBO_NAME"
  EMAIL="$SBO_EMAIL"
elif [ "$NAME" = "nobody" -o "$NAME" = "orphaned" ]; then
  NAME="orphaned - no maintainer"
  EMAIL="NOEMAIL"
fi

if [ "$NAME" = "" -o "$NAME" = "--help" -o "$EMAIL" = "" ]; then
  echo "Usage: $( basename $0 ) 'Full Name' email@domain.com" 1>&2
  echo "       $( basename $0 ) me" 1>&2
  echo "       $( basename $0 ) nobody" 1>&2
  exit 1
fi

INFO="$( echo *.info )"
if [ ! -e "$INFO" ]; then
  die "No (or multiple) .info file in current dir"
  exit 1
fi

cp "$INFO" "$INFO.bak"
sed -i \
  -e "/^MAINTAINER=/s,\"[^\"]*\",\"$NAME\"," "$INFO" \
  -e "/^EMAIL=/s,\"[^\"]*\",\"$EMAIL\"," "$INFO"

if cmp -s "$INFO" "$INFO.bak"; then
  warn "No change to .info file"
  exit 0
fi

if colordiff --version &>/dev/null; then
  DIFF=colordiff
else
  DIFF=diff
fi

$DIFF -u "$INFO.bak" "$INFO"

if git rev-parse &>/dev/null; then
  echo
  echo "Don't forget to do this:"
  echo "   git commit -a -m'New maintainer'"
fi

rm -f "$INFO.bak"
