#!/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"`
NAME=`git config --get-all user.name`
EMAIL=`git config --get-all user.email`
STAP_MAJOR_VERSION=`grep "^Version:" systemtap.spec`
CHANGELOG_VERSION=`echo $STAP_MAJOR_VERSION | cut -f2 -d" "`
TAG_DATE=$(expr `date +%s` / 100) # short: 100-second blocks since epoch
GIT_DESCRIBE=`git rev-parse HEAD | cut -c1-8`
GIT_VERSION_TAG=`echo ${TAG_DATE}g${GIT_DESCRIBE}`
FINAL_VERSION="$CHANGELOG_VERSION~pre$GIT_VERSION_TAG" # the resulting Version: / Source: identifier

### 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-$FINAL_VERSION/ -o ../systemtap/systemtap-$FINAL_VERSION.tar.gz HEAD

# fedpackage stuff

cd ../systemtap

sed -i "s/^Version:.*/Version: $FINAL_VERSION/" systemtap.spec
sed -i "s/^Source:.*/Source: %{name}-%{version}.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> - $FINAL_VERSION\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-$FINAL_VERSION.tar.gz

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

fedpkg build
