#!/usr/bin/env bash
# puts the public files online after a release
# Copyright (c) 2006-2016 Willy Tarreau <w@1wt.eu>
#
# In short :
#   - requires git
#   - no restriction to master, uses last tag
#   - copies & compresses files, changelog & docs to the final destination
#   - shows a listing of the final file

USAGE="Usage: ${0##*/} [-a] [-q] [-y] [-b branch] [-n newver] DIR"
CMD_GZIP="${CMD_GZIP:-gzip -nc9}"
TARGET_DIR=
OUTPUT=
SAYYES=
BRANCH=
DEVEL=
QUIET=
AUTO=
ARG0="$0"
NEW=
DIR=
DOC=( )

# need to have group write on emitted files for others to update
umask 002

die() {
	[ "$#" -eq 0 ] || echo "$*" >&2
	exit 1
}

err() {
	echo "$*" >&2
}

quit() {
	[ "$#" -eq 0 -o -n "$QUIET" ] || echo "$*"
	exit 0
}

while [ -n "$1" -a -z "${1##-*}" ]; do
	case "$1" in
		-a)        AUTO=1         ; shift   ;;
		-q)        QUIET=1        ; shift   ;;
		-y)        SAYYES=1       ; shift   ;;
		-b)        BRANCH="$2"    ; shift 2 ;;
		-n)        NEW="$2"       ; shift 2 ;;
		-h|--help) quit "$USAGE" ;;
		*)         die  "$USAGE" ;;
	esac
done

if [ $# -ne 1 ]; then
	die "$USAGE"
fi

DIR="$1" ; shift
if [ -z "$DIR" ]; then
	die "Missing target directory name."
fi

if [ -n "${DIR##/*}" ]; then
	DIR="$PWD/$DIR"
fi

if [ ! -d "$DIR/." ]; then
	die "Target directory doesn't exist : $DIR"
fi

if ! git rev-parse --verify -q HEAD >/dev/null; then
	die "Failed to check git HEAD."
fi

# we want to go to the git top dir
toplvl=$(git rev-parse --show-toplevel)
if [ -n "$toplvl" ]; then
	cd "$toplvl"
fi

# ensure that a master branch exists here
if [ -z "$(git rev-parse --verify -q master 2>/dev/null)" ]; then
	die "Current directory doesn't seem to be a valid git directory (no master branch)."
fi

if [ "$(git rev-parse --verify -q HEAD)" != "$(git rev-parse --verify -q master)" ]; then
	die "git HEAD doesn't match master branch."
fi

if [ "$(git diff HEAD 2>/dev/null |wc -c)" != 0 ]; then
	err "You appear to have uncommitted local changes, please commit them first :"
	git status -s -uno >&2
	die
fi

if [ -z "$NEW" -o -n "$AUTO" ]; then
	if [ -z "$NEW" ]; then
		NEW="$(git describe --tags HEAD --abbrev=0)"
		NEW="${NEW#v}"
		if [ -z "$NEW" ]; then
			die "Fatal: cannot determine new version, please specify it."
		fi
	fi

	if [ "$(git describe --tags HEAD)" != "v$NEW" ]; then
		if [ -n "$AUTO" ]; then
			quit "Not tagged, nothing to do."
		fi
		die "Current version doesn't seem tagged, it reports $(git describe --tags "v$NEW"). Did you release it ?"
	fi
fi

if ! git show-ref --tags "v$NEW" >/dev/null; then
	die "git tag v$NEW doesn't exist, did you create the release ?"
fi

# determine the product branch from the new release
if [ -z "$BRANCH" ]; then
	subvers=${NEW#[0-9]*.[0-9]*[-.]*[0-9].}
	[ "${subvers}" = "${NEW}" ] && subvers=""
	major=${NEW%.$subvers}
	branch_ext=${major#*[0-9].*[0-9]}
	BRANCH=${major%${branch_ext}}
fi

TARGET_DIR="$DIR/$BRANCH"
if [ ! -d "$TARGET_DIR/." ]; then
	die "Target directory doesn't contain branch $BRANCH. You may have to create it in $DIR."
fi

if [ -z "${NEW##*-dev*}" ]; then
	DEVEL="/devel"
fi

if [ -n "$AUTO" -a -e "$TARGET_DIR/src${DEVEL}/haproxy-$NEW.tar.gz.md5" ]; then
	quit "Version $NEW Already released."
fi

if ! mkdir -p "$TARGET_DIR/src$DEVEL" "$TARGET_DIR/doc"; then
	die "failed to create target directories."
fi

case "$BRANCH" in
	1.3) DOC=( doc/{haproxy-en,haproxy-fr,configuration,architecture}.txt )               ;;
	1.4) DOC=( doc/{haproxy-en,haproxy-fr,configuration}.txt )                            ;;
	1.5) DOC=( doc/{coding-style,configuration,proxy-protocol}.txt )                      ;;
	1.6) DOC=( doc/{coding-style,intro,management,configuration,proxy-protocol,lua}.txt ) ;;
	*)   DOC=( doc/{coding-style,intro,management,configuration,proxy-protocol,lua,SPOE}.txt ) ;;
esac

if [ -z "$AUTO" ]; then
	echo "Ready to produce the following files in $TARGET_DIR/ :"
	echo "    haproxy-$NEW.tar.gz -> src${DEVEL}/"
	echo "    CHANGELOG -> src/CHANGELOG"
	echo "    ${DOC[@]} -> doc/*{,.gz}"
	echo

	git ls-tree -l --abbrev=12 "v$NEW" -- CHANGELOG "${DOC[@]}"

	if [ -z "$SAYYES" ]; then
		echo "Press ENTER to continue or Ctrl-C to abort now!"
		read
	fi
fi

echo "Archiving sources for version $NEW ..."
rm -f "${TARGET_DIR}/src${DEVEL}/haproxy-${NEW}.tar.gz"{,.md5,.sha256}
if ! git archive --format=tar --prefix="haproxy-${NEW}/" "v$NEW" | \
     $CMD_GZIP > "${TARGET_DIR}/src${DEVEL}/haproxy-${NEW}.tar.gz"; then
	die "Failed to produce the tar.gz archive"
fi

( cd "$TARGET_DIR/src${DEVEL}" ; \
  md5sum haproxy-$NEW.tar.gz > haproxy-$NEW.tar.gz.md5 ; \
  sha256sum haproxy-$NEW.tar.gz > haproxy-$NEW.tar.gz.sha256 )

echo "Extracting doc ..."
git show "v$NEW:CHANGELOG" > "$TARGET_DIR/src/CHANGELOG"

for i in "${DOC[@]}"; do
	git show "v$NEW:$i" > "$TARGET_DIR/doc/${i#doc/}"
	$CMD_GZIP < "$TARGET_DIR/doc/${i#doc/}" > "$TARGET_DIR/doc/${i#doc/}.gz"
done

if [ -x "${ARG0%/*}/make-releases-json" ]; then
        # regenerate versions
        "${ARG0%/*}/make-releases-json" -o "$TARGET_DIR/src/releases.json" "$TARGET_DIR"
fi

echo "Done : ls -l ${TARGET_DIR}"
( cd "$TARGET_DIR" ;
  ls -l src/CHANGELOG "src${DEVEL}/haproxy-${NEW}".tar.gz{,.md5,.sha256} $(for i in "${DOC[@]}"; do echo "doc/${i#doc/}"{,.gz}; done)
)
echo
