#!/usr/bin/bash

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###
DIR=`mktemp -d pcpXXX -p /tmp/`; cd $DIR

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

git clone git://git.pcp.io/pcp.git pcp-git
#git clone git://sourceware.org/git/pcpfans.git pcp-git
cd pcp-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)
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`
PCP_MAJOR_VERSION=`grep "^PACKAGE_MAJOR" VERSION.pcp | cut -f2 -d"="`
PCP_MINOR_VERSION=`grep "^PACKAGE_MINOR" VERSION.pcp | cut -f2 -d"="`
PCP_REVISION=`grep "^PACKAGE_REVISION" VERSION.pcp | cut -f2 -d"="`
PCP_FULL_VERSION=`echo $PCP_MAJOR_VERSION.$PCP_MINOR_VERSION.$PCP_REVISION`
CHANGELOG_VERSION=`echo $PCP_FULL_VERSION`
GIT_DESCRIBE=`git describe | rev | cut -f1 -d"g" | rev`
PCP_WEB_JS=`grep "^Source*" build/rpm/fedora.spec | grep "webjs" | cut -f2 -d" " | rev | cut -f1 -d"/" | rev`

### 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 pcp
cd ..
fedpkg co pcp
cd pcp

GIT_VERSION_TAG=`echo 0.${TAG_DATE}git${GIT_DESCRIBE}`

git diff pcp.spec ../pcp-git/build/rpm/fedora.spec > ../spec-diff

if [ -s ../spec-diff ]; then
    CHANGELOG_PRESENT=`grep \%changelog ../spec-diff`
    # 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 pcp.spec < ../spec-diff
    fi
    cd ..
fi

cd pcp-git

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

# if $PCP_WEB_JS is empty, then we don't need to git archive it seperately.
if [ ! -z $PCP_WEB_JS ]; then
    cd ..
    #    git clone git://git.pcp.io/pcp-webjs.git pcp-webjs
    git clone git://sourceware.org/git/pcpfans.git --branch=webjs pcp-webjs
    cd pcp-webjs
    git archive --prefix=pcp-webjs/ -o ../pcp/$PCP_WEB_JS HEAD
fi

# fedpackage stuff
cd ../pcp

sed -i "s/Release:.*/Release: $GIT_VERSION_TAG\%\{\?dist\}/" pcp.spec
sed -i "s/^Source0:.*/Source0: %{name}-%{version}-$GIT_VERSION_TAG.tar.gz/" pcp.spec
sed -i "s/%global buildversion.*/%global buildversion 0/" pcp.spec
sed -i "s/^Version:.*/Version: $PCP_FULL_VERSION/" pcp.spec

# make note in the comments if any differences from upstream fedora.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/" pcp.spec
else
sed -i "s/\%changelog/\%changelog\n* $DATE $NAME <$EMAIL> - $CHANGELOG_VERSION-$GIT_VERSION_TAG\n- Automated weekly rawhide release\n/" pcp.spec
fi

echo "Please ensure that you have an up to date fedoraproject SSL certificate, you can achieve this by running fedora-cert -n"
echo `pwd`
fedpkg new-sources pcp-$CHANGELOG_VERSION-$GIT_VERSION_TAG.tar.gz $PCP_WEB_JS

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

fedpkg build
