#!/bin/bash

version=0.10
program=${0##*/}
progdir=${0%/*}
if [ "$progdir" = "$program" ]; then progdir="."; fi

# ----------------------------------------------------------------------
function usage() {
    cat <<EOF
NAME
	$program - Make a release

SYNOPSIS
	$program [OPTIONS] VERSION

DESCRIPTION

	Do the sequence of actions necessary to properly produce a release
	branch. This includes updating the project version and committing that
	to the repository, creating a release tag and a release branch if
	needed, and updating the project version again to indicate that any
	further commits are development work. Requires 1 argument: the VERSION
	number (e.g., 1.23).

	The script uses svn info to retrieve information about the repository
	and path of the current directory, and inspects that to determine
	exactly what to do. If the current path relative to the repository root
	starts with 'trunk', then a new branch is created named
	branch/\$VERSION. If the current path starts with something else 
	than 'trunk', it is assumed to be a working branch, and no new branch is
	created. In either case, a copy of the current working copy is created
	in tags/\$VERSION.

EOF
    echo -e "OPTIONS"
    if   [ "$(uname)" = "Linux" ]; then
        egrep "^[	]+[-][A-Za-z| -]+\*?\)[	]+[A-Za-z].+#" $0 | tr -s "\t|" "\t," | sed -r -e 's/\)[ \t]+([A-Z]+)=\$2[^#]*#/=\\1\t/' -e 's/\)[^#]*#/\t/'
    else
        egrep "^[	]+[-][A-Za-z| -]+\*?\)[	]+[A-Za-z].+#" $0 | sed 's/\|.*\$2[^#]*#/	/'| sed -E 's/\|.*\)[^#]*#/	/'
    fi
    cat <<EOF

AUTHOR
	Written by Henrik Levkowetz, <henrik@levkowetz.com>

COPYRIGHT
	Copyright 2007,2013 The IETF Trust.

EOF

}

# ----------------------------------------------------------------------
function die() {
    echo -e "\n$program: error: $*" > /dev/stderr
    exit 1
}

function note() { 
	if [ -n "$VERBOSE" ]; then echo -e "$*"; fi
}

# ----------------------------------------------------------------------
function version() {
	echo -e "$program $version"
}

# ----------------------------------------------------------------------
trap 'echo "$program($LINENO): Command failed with error code $? ([$$] $0 $*)"; exit 1' ERR


# ----------------------------------------------------------------------
# Option parsing

# Options
shortopts=hnvV
longopts=help,dry-run,verbose,version

# Default values
MSG=""
PROJ=xml2rfc
VERFILE=$PROJ/__init__.py
SETUP=setup.py
do=""

[ -z "$by" ] && by=${RELEASER_REAL_NAME}

if   [ "$(uname)" = "Linux" ]; then
    args=$(getopt -o "$shortopts" --long "$longopts" -n '$program' -- $SV "$@")
    if [ $? != 0 ] ; then die "Terminating..." >&2 ; exit 1 ; fi
    eval set -- "$args"
    sed="sed -r"
    [ -z "$by" ] && by=$(getent passwd $(whoami) | cut -d ':' -f 5 | tr -d ',')
elif [ "$(uname)" = "Darwin" ]; then
    args=$(getopt -o "$shortopts" --long "$longopts" -n '$program' -- $SV "$@")
    if [ $? != 0 ] ; then die "Terminating..." >&2 ; exit 1 ; fi
    eval set -- "$args"
    sed="sed -E"
    [ -z "$by" ] && by=$(id -F)
else
    # BSDs
    args=$(getopt -o$shortopts $SV $*)
    if [ $? != 0 ] ; then die "Terminating..." >&2 ; exit 1 ; fi
    set -- $args
    sed="sed -E"
    [ -z "$by" ] && by=$(getent passwd $(whoami) | cut -d ':' -f 5 | tr -d ',')
fi

while true ; do
    case "$1" in
	-h| --help)		usage; exit;;	#	 Show this help, then exit
	-m| --message)		MSG=$2; shift;;		# Specify a commit message
	-n| --dry-run)		do="echo ==> ";;	#	 Show what would be done	
	-v| --verbose)		VERBOSE=1;;	#	 Be more talkative
	-V| --version)		version; exit;;	#	 Show program version, then exit
	--)			shift; break;;
	*) die "Internal error, inconsistent option specification: '$1'";;
    esac
    shift
done


# ----------------------------------------------------------------------
# The program itself

ARGMIN=1

if [ $# -lt $ARGMIN ]; then
    usage
    die "$# arguments found, $ARGMIN required"
fi

VER=$1
WORK=$2

REPO=$(svn info | grep "^Repository Root:" | awk '{ print $3 }')
RURL=$(svn info | grep "^URL:" | awk '{ print $2 }')
RDIR=${RURL#$REPO}
DIR=${RDIR#/}
if   [ -z "$DIR" ]; then
    die "Couldn't find anything to release here"
elif [ "${DIR%%/*}" = "trunk" ]; then
    SRC="trunk"
elif [ "${DIR%%/*}" = "branch" ]; then
    tmp=${DIR#*/}		# get rid of 'branch/'
    SRC="branch/${tmp%%/*}"	# keep first subdir under branch/
fi

[ -z "$by" ] && die "Can't determine the real name of the user running this script"

# If a project subdirectory was specified on the command line, then use it
SRC=$SRC${WORK:+/$WORK}

note ""
note "Releasing from $SRC"

note "Locating the root of the working copy..."
while [ "${#DIR}" -gt "${#SRC}" ]; do
    [ "$DIR" = "$prev" ] && die "Internal error"
    cd ..
    #note "  now at $PWD"
    prev=$DIR
    DIR=${DIR%/*}
done
if [ "$DIR" != "$SRC" ]; then
    die "Couldn't find the root of your '$SRC' working copy"
fi
note "  $DIR"

REPO=${REPO%/}				# remove trailing slash
SRC=${SRC#/}				# remove leading slash

MAJOR=${VER%%.*}
REST=${VER#*.}
MINOR=${REST%.*}
MAINT=${REST#*.}
VER="$(printf %d.%d.%d $MAJOR $MINOR $MAINT)"
NEXT=$(( $MAINT + 1 ))
DEV="$(printf %d.%d.%ddev1 $MAJOR $MINOR $NEXT)"

note "Checking that changelog information is available"
changes=$( sed -n "/^$PROJ ($VER.*)/,/^ -- /p" changelog )
[ "$changes" ] || die "No changelog information for $VER found"

contributors=$(echo "$changes" | sed 's/\.[ \t\n]/ /'| tr -c "a-z0-9.@-" "\n" | sort | uniq | grep '@' | sed -r -e 's/^\.+//' -e 's/\.+$//' -e 's/^/-c /' || true)

note "Checking that we don't have temporary editor files"
find $SRC/$PROJ -name '?*~' -print0 | grep -z . && die "Found temporary editor files, see above."

note "Set the current time on the release notes in the changelog file"
$do sed -r -i -e "1,/^ -- /s/([A-Za-z-]+ <[a-z0-9.-]+@[a-z0-9.-]+>  ).*$/\1$(date +'%d %b %Y %H:%M:%S %z')/" changelog
note " $(grep -m1 "^ -- " changelog)"

note "Verifying that version $VER doesn't already exist..."
$do svn info $REPO/tags/${WORK:+$WORK/}$VER 2>&1 | $do egrep -q "(Not a valid URL|URL .* non-existent)" || die "The tag '$VER' already exists (or there was an error testing for it)."
note "  Ok"

note "\nUpdating the version info in $VERFILE and settings.py"
$do sed -i -r -e "/^__version__/s/'[\.0-9]+(dev[0-9]+)?'/'$VER'/" $VERFILE
$do sed -i -r -e "/version=/s/'[\.0-9]+(dev[0-9]?)?'/'$VER'/" setup.py

note "\nVerifying that we're now reporting the correct version"
$do python setup.py -q install
$do [ $(xml2rfc --version | awk '{print $2}') = $VER ] || die "Xml2rfc reported the wrong version number"

note "\nCommitting version information for version $VER: $MSG"
$do svn update
$do svn commit $VERFILE setup.py -m "Set version info to release version $VER before tagging."

note "\nCommitting the changelog..."
$do svn commit changelog -m "Changelog entry for $VER"

note "\nUpdating manpage and docfiles with the new versions"
$do tox -epy36 -- manupdate
$do svn commit -m "Updated manpage.txt and docfiles"

note "\nVerifying there's no uncommitted changes..."
$do svn st | grep "^[AMGRD] " && die "There seems to be uncommitted changes in this working copy"

note "\nChecking that the changelog is valid rst ..."
$do rst2html changelog > /dev/null	# verify that the changelog is valid rst

note "\nDoing the upload..."
if [ -n "${XML2RFC_SIGNING_IDENTITY}" ]; then
    $do python setup.py sdist upload --sign --identity="${XML2RFC_SIGNING_IDENTITY}"
else
    $do python setup.py sdist upload --sign
fi

#note "\nCopying the tarfile to the web directory for xml2rfc"
#$do rsync dist/xml2rfc-$(xml2rfc --version | awk '{print $2}').tar.gz /www/tools.ietf.org/tools/xml2rfc2/cli/ || true
#
#note "\nPushing the tarfile to other tools servers"
#$do toolpush /www/tools.ietf.org/tools/xml2rfc2/cli/

note "\nCreating new tag 'tags/${WORK:+$WORK/}$VER' from $SRC"
$do svn cp $REPO/$SRC $REPO/tags/${WORK:+$WORK/}$VER -m "Creating new tag 'tags/${WORK:+$WORK/}$VER' from $SRC"

note "\nUpdating version info to indicate development mode"
$do sed -i -r -e "/^__version__/s/'[\.0-9]+(dev[0-9]?)?\"/'$NEXT'/" $VERFILE
$do sed -i -r -e "/version=/s/'[\.0-9]+(dev[0-9]?)?\"/'$NEXT'/" setup.py

note "\nCommitting the updated version"
$do svn commit $VERFILE setup.py -m "Set version info back to development mode"

$do svn update

echo "
Hi,

This is an automatic notification about a new xml2rfc release, 
v$VER, generated when running the mkrelease script.

Release notes:

$changes

The preferred way to install xml2rfc is by doing 'pip install xml2rfc',
and 'pip install --upgrade xml2rfc' to upgrade.  If there are system-
installed python modules which pip will not upgrade, you may have to
use 'pip install --upgrade --no-deps xml2rfc' and install dependencies
manually.

The new version is also available through SVN checkout, with
  'svn checkout $REPO/tags/${WORK:+$WORK/}$VER'

Documentation for this release is built-in, and also available at:
  https://xml2rfc.tools.ietf.org/xml2rfc-doc-$VER.html

Regards,

	${by}
	(via the mkrelease script)
" > ./mail/release-mail-v$VER.txt

note "\nSend mail to interested parties"
SEND_ARGS=""
[ "${RELEASER_EMAIL}" ] && SEND_ARGS="-r ${RELEASER_EMAIL}"
cat ./mail/release-mail-v$VER.txt | $do mail "${SEND_ARGS}" -s "New xml2rfc release: v$VER" \
        -c henrik@levkowetz.com -c glen@amsl.com -c rjsparks@nostrum.com -c jay@ietf.org \
        -c housley@vigilsec.com -c arusso@amsl.com -c priyanka@amsl.com -c johnl@taugh.com \
        -c sginoza@amsl.com -c mferguson@amsl.com -c kmoreland@amsl.com -c maddy@amsl.com \
        $contributors rse@rfc-editor.org
note "\nSend mail to mailing lists (avoiding too many addressees)"
cat ./mail/release-mail-v$VER.txt | $do mail "${SEND_ARGS}" -s "New xml2rfc release: v$VER" \
        xml2rfc@ietf.org xml2rfc-dev@ietf.org -c rfc-markdown@ietf.org

#$do toolsfeed control changelog /www/tools.ietf.org/tools/atomfeed.xml
#$do toolpush /www/tools.ietf.org/tools/atomfeed.xml
#$do toolcmd sudo pip3.6 install --upgrade xml2rfc
#$do toolcmd "xml2rfc --doc --out /www/tools.ietf.org/tools/xml2rfc/htdocs/xml2rfc-doc-$VER.xml; xml2rfc --text --html /www/tools.ietf.org/tools/xml2rfc/htdocs/xml2rfc-doc-$VER.xml; ln -sf xml2rfc-doc-$VER.html /www/tools.ietf.org/tools/xml2rfc/htdocs/xml2rfc-doc.html"
