#!/bin/sh
#
# FreeBSD Port Tools 1.11
#
# Copyright (c) 2003-2009, Sergei Kolobov
# Copyright (c) 2013-2014, Mathieu Arnold
# Copyright (c) 2014, Johannes Meixner
# Copyright (c) 2014-2015, Steven Kreuzer
# 
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
# cmd_commit
# Module for port(1)
# SUMMARY: commit a port into the FreeBSD Ports GIT Repository

# Check if this script is run via port(1)
if [ "${PORTTOOLS}" = "" ]
then
	echo "This script should be run via port(1) front-end"
	exit 1
fi

# Usage
usage ()
{
cat << EOF
FreeBSD Port Tools 1.11
Usage:	port commit [-h]
	-h	- Display this usage summary
EOF
}


# Parse command line arguments
ARGS=`/usr/bin/getopt h $*`
if [ $? != 0 ]
then
	echo "Error: invalid arguments"
	usage
	exit 2
fi

set -- $ARGS
while [ x"$1" != x"--" -a x"$1" != x"" ]
do
	i=$1
	case "$i" in
	# help
	-h)
		usage
		exit 0
		;;
	esac
	shift
done

echo ${EMAIL} | grep -qsi "@FreeBSD.org"
if [ $? != 0 ]
then
	echo "===> ERROR: Please set EMAIL to your FreeBSD address in ~/.porttools"
	exit 1
fi

# Determine committer's username on freefall
FREEFALL_USERNAME="${EMAIL%%@FreeBSD.org}"
echo "===> Your username on freefall: ${FREEFALL_USERNAME}"

# Determine if this is a new port
MODE="update"
git status Makefile 2>&1 | grep -qs 'fatal: not a git repository'
if [ $? -eq 0 ]
then
	MODE="new"
fi

# Run portlint(1) to validate port's sanity
echo "===> Pre-commit portlint check"
FLAGS="-C"
[ "${MODE}" = "new" ] && FLAGS="${FLAGS} -N"
PL_SVN_IGNORE='^\d+$|^pr-patch$|^svn-msg$' \
portlint ${FLAGS}
if [ $? -ne 0 ]
then
	echo "Error validating port"
	exit 1
fi


# See if GIT message already exists, and use that for commit log
MSG="svn-msg"
FLAGS=""

if [ "${MODE}" = "new" ]
then
	PORTSDIR="`make -V PORTSDIR`"

	if [ -e ${MSG} ]
	then
		FLAGS="-c ${MSG}"
	fi

	${PORTSDIR}/Tools/scripts/addport -d `pwd` -u ${FREEFALL_USERNAME} ${FLAGS}
else
	if [ -e ${MSG} ]
	then
		FLAGS="-F ${MSG}"

		ANSWER="n"
		while true
		do
			echo '============================================='
			cat ${MSG}
			echo '============================================='
			read -p "Is the GIT message above correct? (y/n)" ANSWER
			[ "${ANSWER}" = "y" ] && break
			${VISUAL:-vi} ${MSG}
		done
	fi
	# Commit the port update
	echo "===> Committing port update"
	git commit ${FLAGS}

fi
# Remove GIT message file only if commit was successful
[ $? -eq 0 -a -e ${MSG} ] && rm ${MSG}

echo "===> Done"
exit 0
