#!/bin/bash
# the above line updates the 'ps' display for 'bell'

# this script is hard-linked to pgcommit and pgcommitall

. traprm

export QUIET=$(($QUIET + 1))

# gitweb and email line lengths
# titles from https://git.wiki.kernel.org/index.php/CommitMessageConventions
INITIAL_CONTENTS="\
-- email subject limit -----------------------------------------
-- gitweb summary limit --------------------------


Reported-by: 

Bug: 

Discussion: 

Author: 

Reviewed-by: 

Tested-by: 

Backpatch-through: "

[ "$#" -ne 0 ] && echo "Usage:  $(basename $0)" 1>&2 && exit 1

cd /pgsql || exit 1

# exit if no changes
if [ "$BASE" = "pgcommit" ]
then	(cd postgresql && pggit diff $OPT; pggit diff --cached) > $TMP/diff
	[ ! -s $TMP/diff ] && echo "no changes to commit" 1>&2 && exit 1
fi

# feed directories
if [ "$BASE" = "pgcommit" ]
then	echo "postgresql"
else	ls
fi |
while read DIR
do	# skip unless real directory, not symlink
	[ -h "$DIR" ] && continue
	[ ! -d "$DIR" ] && continue

	cd "$DIR"

	# right branch?
	[ "$DIR" = "postgresql" -a $(pgsw) != "master" ] && echo "$(pwd) is not on the \"master\" branch, exiting." 1>&2 && exit 1
	[ "$DIR" != "postgresql" -a $(expr $(pgsw) : 'REL') -ne 3 ] && echo "$(pwd) is not on a 'REL' branch, exiting." 1>&2 && exit 1
	
	# reject files?
	find -H . \( -name '*.rej' -print \) -o \( -name '*.orig' -exec rm {} \; \) | tee $TMP/rej
	[ -s $TMP/rej ] && echo "Reject files exist, exiting." 1>&2 && exit 1
	
	# un-added files?
	pggit status -u > $TMP/untracked
	grep -q '\<Untracked files:' $TMP/untracked && cat $TMP/untracked && exit 1

	cd - > /dev/null
done || exit 1

echo "$INITIAL_CONTENTS" > $TMP/0
emastd +3 $TMP/0
# remove dashed lines
# can't use pipe as an error occurs if there are no matches
# remove dashed header lines
grep -v '^-- .*--$' $TMP/0 | 
# URL shortener
pgurl |
# if there is only a message-id, add the URL, but only does one
sed '/https\?:\/\//!s;^\(Discussion:'"$SPTABCLASS"'*\)\(.*@.*\)$;\1https://postgr.es/m/\2;' > $TMP/1
mv $TMP/1 $TMP/0

# remove unused markup
# can't use pipe as an error occurs if there are no matches
egrep -v "^(Reported-by|Bug|Discussion|Author|Reviewed-by|Tested-by|Backpatch-through):$SPTABCLASS*$" $TMP/0 > $TMP/1; mv $TMP/1 $TMP/0

# delete trailing blank lines
sed -e :a -e '/./,$!d;/^\n*$/{$d;N;};/\n$/ba' $TMP/0 > $TMP/1; mv $TMP/1 $TMP/0

# remove multiple blank lines
# this can be caused by blank lines left _under_ removed boilerplate lines
cat --squeeze-blank $TMP/0 > $TMP/1; mv $TMP/1 $TMP/0

# exit if file is only empty lines
grep -qv '^[ 	]*$' $TMP/0  || exit 1

cat $TMP/0
chown postgres $TMP/0

# feed directories
if [ "$BASE" = "pgcommit" ]
then	echo "postgresql"
else	ls
fi |
while read DIR
do	# skip unless real directory, not symlink
	[ -h "$DIR" ] && continue
	[ ! -d "$DIR" ] && continue

	cd "$DIR"
	if ! pggit commit -a -F $TMP/0
	then	echo
		cat $TMP/0
		exit 1
	fi
	cd - > /dev/null
done

pgpullall || exit 1

# Must use 'git config --global push.default matching' to push all branches
# http://stackoverflow.com/questions/13148066/warning-push-default-is-unset-its-implicit-value-is-changing-in-git-2-0
# http://www.postgresql.org/message-id/19278.1285948404@sss.pgh.pa.us
. cd_pgtop
pggit push

[ -t 0 ] && bell

exit 0
