#!/usr/bin/bash

set -e  # exit on error

function determine_apply_diff() {

    read ANSWER
    if [ "$ANSWER" == "no" ]
    then
	return 1
    elif [ "$ANSWER" == "yes" ]
    then
	return 0
    else
	echo "Please answer \"yes\" or \"no\""
	determine_apply_diff
	return $?
    fi

}

### Create the directory we'll be working in###

SRCDIR=`dirname $0`/..; SRCDIR=`realpath $SRCDIR`
DIR=`mktemp -d stapXXX -p /tmp/`; cd $DIR

echo "Working in $DIR"
trap 'rm -rf $DIR' 0 1 2 3 5 15

# We need to differenciate between upstream sources and the later used distro sources
# hence the specific checkout to systemtap-git

git clone --reference $SRCDIR git://sourceware.org/git/systemtap.git systemtap-git
cd systemtap-git

# We need to use --global for the NAME and USER vars as we're
# assuming this script can be run from anywhere (ie, not in an
# already existing git repo)
APPLY_DIFF=1
DATE=`date +"%a %b %d %Y"`
TAG_DATE=`date +"%Y%m%d"`
NAME=`git config --get-all user.name`
EMAIL=`git config --get-all user.email`
TAG_DATE=`date +"%Y%m%d"`
STAP_MAJOR_VERSION=`grep "^Version:" systemtap.spec`
CHANGELOG_VERSION=`echo $STAP_MAJOR_VERSION | cut -f2 -d" "`
#GIT_VERSION_TAG=`echo 0.$(git describe --abbrev=12 | rev | cut -f1,2 -d- --output-delimiter=. | rev)`
GIT_DESCRIBE=`git describe | rev | cut -f1 -d"g" | rev`
GIT_VERSION_TAG=`echo 0.${TAG_DATE}git${GIT_DESCRIBE}`
### lets do a sanity check first
if which fedpkg &>/dev/null; then
 :
else
echo "Please install the fedpkg package"
exit
fi

# Checkout the fedora sources systemtap
cd ..
fedpkg co systemtap

# Diff and check the differences between the two spec files,
cd systemtap

git diff systemtap.spec ../systemtap-git/systemtap.spec > ../spec-diff || true

if [ -s ../spec-diff ]; then
    CHANGELOG_PRESENT=`grep \%changelog ../spec-diff || true`
    # the space in front of %changelog is because its a diff context line
    if [ "$CHANGELOG_PRESENT" == " %changelog" ]; then
	# We don't need to know the difference in spec changelogs
	# changelogs should only ever grow, make sure we also trim the %changelog
	# context lines as well
	sed -i '/\%changelog/,$d' ../spec-diff
	tac ../spec-diff | sed '1,3d' | tac > spec-diff
	mv spec-diff ../spec-diff
    fi
    # end changelog diff if
    cat ../spec-diff
    echo "The above changes will apply to the fedora git tree,"
    echo "would you like to apply the patch? (yes/no):"
    determine_apply_diff
    APPLY_DIFF=$?
    if [ "$APPLY_DIFF" == 0 ]; then
	patch -p1 systemtap.spec < ../spec-diff
    fi
    cd ..
fi

# create a tar.gz of the latest git sources from HEAD
cd systemtap-git

git archive --prefix=systemtap-$CHANGELOG_VERSION/ -o ../systemtap/systemtap-$CHANGELOG_VERSION-$GIT_VERSION_TAG.tar.gz HEAD

# fedpackage stuff

cd ../systemtap

sed -i "s/Release: .*/Release: $GIT_VERSION_TAG\%\{\?dist\}/" systemtap.spec
sed -i "s/^Source:.*/Source: %{name}-%{version}-$GIT_VERSION_TAG.tar.gz/" systemtap.spec

# make note in the comments if any differences from upstream systemtap.spec have been merged
if [ "$APPLY_DIFF" == 0 ]; then
    sed -i "s/\%changelog/\%changelog\n* $DATE $NAME <$EMAIL> - $CHANGELOG_VERSION-$GIT_VERSION_TAG\n- Automated weekly rawhide release\n- Applied spec changes from upstream git\n/" systemtap.spec
else
    sed -i "s/\%changelog/\%changelog\n* $DATE $NAME <$EMAIL> - $CHANGELOG_VERSION-$GIT_VERSION_TAG\n- Automated weekly rawhide release\n/" systemtap.spec
fi

echo "Please ensure that you have an up to date fedoraproject krb5 ticket, else kinit `whoami`@FEDORAPROJECT.ORG"
klist

fedpkg new-sources systemtap-$CHANGELOG_VERSION-$GIT_VERSION_TAG.tar.gz

fedpkg commit -m "Automated weekly systemtap rawhide release: $GIT_VERSION_TAG" -p

fedpkg build