#!/bin/sh
#
# typical usage:
# push-pcp -b master -t pcp -r 6bad98e4b0537c2e99b3906ca7a36f4998ea3419..
#

tmp=/var/tmp/$$
trap "rm -f $tmp.*; exit 0" 0 1 2 3 15

_usage()
{
    echo >&2 "Usage: pcp-push [options]"
    echo >&2
    echo >&2 "options:"
    echo >&2 "  -b branch           [defaults to master]"
    echo >&2 "  -n                  dryrun"
    echo >&2 "  -r range            [defaults to \$(cat pushed.sha)..)]"
    echo >&2 "  -s                  short format, do not include commit messages"
    exit 1
}

dryrun=false
GIT=git
branch=master
tree=origin
short=false
if [ -f pushed.sha ]
then
    range=$(cat pushed.sha)..
else
    range=''
fi
while getopts "b:nr:s?" c
do
    case $c
    in
	b)
	    branch="$OPTARG"
	    ;;
	n)
	    dryrun=true
	    GIT="echo + git"
	    ;;
	r)
	    range="$OPTARG"
	    ;;
	s)
	    short=true
	    ;;
	*)
	    _usage
	    # NOTREACHED
    esac
done
shift `expr $OPTIND - 1`

[ $# -eq 0 ] || _usage

if [ -z "$range" ]
then
    echo "Error: no range from pushed.sha, so need -r range"
    exit 1
fi


unset GIT_EXTERNAL_DIFF
pull=`git config remote.origin.url`
case "$pull"
in
    *oss.sgi.com*|*git.pcp.io*)
	# old school ... does not work any more
	#
	push=`echo "$pull" | sed -e 's/^git:/ssh:/' -e "s;/git.pcp.io/;/git.pcp.io/oss/git/;"`
	;;

    *github*)
	# new school ...
	#
	push=''
	;;

    *)
	echo "Sorry, no recipe to handle repo: $pull"
	exit 1
	;;
esac

echo "Changes committed to $pull $branch" >/tmp/msg
echo >>/tmp/msg
git shortlog --no-merges --numbered $range >$tmp.tmp
if [ -s $tmp.tmp ]
then
    cat $tmp.tmp >>/tmp/msg
else
    echo "Nothing to push ... bye."
    rm -f /tmp/msg
    exit
fi
git log --no-merges -p $range | diffstat -p1 >>/tmp/msg
if $short
then
    :
else
    echo >>/tmp/msg
    echo "Details ..." >>/tmp/msg
    echo >>/tmp/msg
    git log --no-merges $range >>/tmp/msg
fi

xclip -sel clip < /tmp/msg
cat /tmp/msg
echo "(all of this for email is in /tmp/msg)"
rm -f $tmp.y
while true
do
    if [ -z "$push" ]
    then
	echo -n "Push to $pull [y|n|q] (or ctrl+C to abort) "
    else
	echo -n "Push to $push? [y|n|q] (or ctrl+C to abort) "
    fi
    read ans </dev/tty
    if [ -z "$ans" ]
    then
	:
    elif [ "$ans" = y ]
    then
	touch $tmp.y
	break
    elif [ "$ans" = n ]
    then
    	break
    elif [ "$ans" = q ]
    then
	echo "Quitting ... pushed.sha not updated"
	exit
    fi
    echo "Answer the question, bozo!"
done
if [ -f $tmp.y ]
then
    $GIT push $push
    $GIT push --tags $push
    # may have local git mirror that needs to be updated ...
    #
    if [ -d $HOME/git-mirror ]
    then
	here=`pwd`
	cd $HOME/git-mirror
	for dir in *
	do
	    [ -d "$dir" ] || continue
	    [ -f "$dir/config" ] || continue
	    if grep "url = $pull" "$dir/config" >/dev/null 2>&1
	    then
		# found repo with url matching the one we've just pushed to
		#
		cd $dir
		echo "Update local $dir git mirror ..."
		git fetch
		cd ..
	    fi
	done
	cd $here
    fi
fi

rm -f $tmp.y
while true
do
    echo -n "Push to github mirror? [y|n|q] (or ctrl+C to abort) "
    read ans </dev/tty
    if [ -z "$ans" ]
    then
	:
    elif [ "$ans" = y ]
    then
	touch $tmp.y
	break
    elif [ "$ans" = n ]
    then
    	break
    elif [ "$ans" = q ]
    then
	echo "Quitting ... pushed.sha not updated"
	exit
    fi
    echo "Answer the question, bozo!"
done
if [ -f $tmp.y ]
then
    push="ssh://git@github.com/performancecopilot/pcp.git"
    $GIT push --mirror $push
fi

# remember last commit that was pushed ...
#
sha=`git log | sed -e 's/commit //' -e 1q`
if $dryrun
then
    echo "+ echo $sha >pushed.sha"
else
    echo "$sha" >pushed.sha
    case `id -un`
    in
	kenj)
	    echo "To send commit mail ..."
	    echo '$ sendcommitmail'
	    ;;
    esac
fi
