#!/bin/bash

# Copyright (C) 2011-2013 by Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
#
# This programme is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This programme is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

GIT_USER="gituser"
GIT_HOSTNAME="git.mydomain.org"

RPMEMAIL="firstname.lastname@mydomain.org"
RPMFULLNAME="Firstname Lastname"
GPG_KEY=
DISTS_SUPPORTED="fedora epel"
FEDORA_DISTROS="fe18 fe19 fe20 rawhide"
EPEL_DISTROS="sl6"
RPM_BUILDS_FOR="\
fedora: $FEDORA_DISTROS\n\
epel: $EPEL_DISTROS\n\
"

COMPONENT_MAIN="main"
COMPONENT_NIGHTLY="nightly"
COMPONENT_BUNDLES="bundle-release1 bundle-release2"
REPOS_SERVER="packages.mydomain.org"
PACKAGES_WITHOUT_OTHERMIRROR="keyring"
GNUPGHOME=$HOME/.gnupg

test -z $1 && { echo "usage: $(basename $0) [<subpath>/]<git-project> {main,main/<codename>,nightly,nightly/<codename>} [<git-checkout>]"; exit -1; }

PREFIX=$(echo `basename $0` | cut -d"-" -f1)
test -f ~/.buildscripts/$PREFIX.conf && . ~/.buildscripts/$PREFIX.conf || { echo "$0 has no valid context prefix..."; exit -1; }

set -ex

set_vars() {
	TEMP_BASE="$HOME/tmp/"
	mkdir -p "$TEMP_BASE"
	chmod 2770 "$TEMP_BASE"

	# first argv is the name of the Git project
	PROJECT_PATH="$1"
	PROJECT_PATH=${PROJECT_PATH/%.git/}
	PROJECT="$(basename $PROJECT_PATH)"

	# grab repository component area from command line (2nd argv) or guess it
	ARGV2_COMPONENT="$(echo "$2/" | cut -d"/" -f1)"
	ARGV2_CODENAME="$(echo "$2/" | cut -d"/" -f2)"
	COMPONENT="${ARGV2_COMPONENT:-${COMPONENT:-$COMPONENT_NIGHTLY}}"
	CODENAMES="${ARGV2_CODENAME:-${CODENAMES}}"
	[ -n "$ARGV2_CODENAME" ] && FORCE_BUILD=0 || FORCE_BUILD=-1
	DATE="${DATE:-$(date +%Y%m%d)}"
	if [ "x$COMPONENT" = "x$COMPONENT_MAIN" ]; then
		CHECKOUT="${3:-build-main}"
	elif echo "$COMPONENT_RELEASES" | grep "$COMPONENT" >/dev/null; then
		CHECKOUT="${3:-build-$COMPONENT}"
	elif [ "x$COMPONENT" = "x$COMPONENT_NIGHTLY" ]; then
		CHECKOUT="${3:-master}"
	else
		echo "error: no such package component area for this Git project. Aborting..."
		exit -1
	fi
	# the DATE might be given as ,,today'' from the command line
	[ "x$DATE" = "xtoday" ] && DATE="$(date +%Y%m%d)"

	# setting paths
	PROJECT_DIR="$HOME/build/$COMPONENT/$PROJECT"
	PKGDIST="$HOME/pkg-dist/$COMPONENT/$PROJECT"

	# build for other architectures than amd64/i386
	EXTRA_ARCHS="${EXTRA_ARCHS:-}"
	EXTRA_ARCHS_ONLY="${EXTRA_ARCHS_ONLY:-}"

	# creating paths
	mkdir -p "$TEMP_BASE"
	mkdir -p "$PROJECT_DIR"
	mkdir -p "$PKGDIST"

	return 0
}

prepare_workspace() {
	# in any case remove the RPM_BUILDS_FOR file
	rm -f "$PROJECT_DIR/RPM_BUILDS_FOR"

	# create rpmbuild subdirectories
	mkdir -p ~/rpmbuild/SOURCES

	# make sure our local working copy is up to date...

	if [ -d "$PROJECT_DIR/.git" ]; then 
		cd "$PROJECT_DIR" && git reset --hard
		git checkout --force $CHECKOUT || git checkout --force -b $CHECKOUT
		git pull origin $CHECKOUT
		git fetch origin upstream:upstream || true
		git fetch origin pristine-tar:pristine-tar || true
		# and again, get the $CHECKOUT refspec in pure state
		git reset --hard
		git clean -df
	else
		cd "$(dirname $PROJECT_DIR)"
		git clone git://$GIT_HOSTNAME/$PROJECT_PATH.git
		cd "$PROJECT"
		git checkout --force $CHECKOUT || git checkout --force -b $CHECKOUT;
		git fetch origin upstream:upstream
		git fetch origin pristine-tar:pristine-tar || true
		git clean -df
	fi

	GIT_OBJECT_ID=`git show-ref -s heads/master`
	cd "$PROJECT_DIR"

	# by default we build for all current debian versions
	if test -z $ARGV2_CODENAME; then
		test -f RPM_BUILDS_FOR || echo -e "$RPM_BUILDS_FOR" > RPM_BUILDS_FOR
	elif echo "$DEBIAN_DISTROS" | grep $ARGV2_CODENAME >/dev/null; then
		echo "fedora: $ARGV2_CODENAME" > RPM_BUILDS_FOR
	elif echo "$UBUNUT_DISTROS" | grep $ARGV2_CODENAME >/dev/null; then
		echo "epel: $ARGV2_CODENAME" >/dev/null
	fi
	return 0
}

clear_pkgdist() {
	# pkgdist directory cleanup
	cat "$PROJECT_DIR/RPM_BUILDS_FOR" | egrep -v '(^$|^#.*$)' | while read line; do
		l_DIST="$(echo ${line/: /:} | cut -d":" -f1 | tr [:upper:] [:lower:])"
		l_CODENAMES="${CODENAMES:-$(echo ${line/: /:} | cut -d":" -f2- | tr [:upper:] [:lower:])}"
		echo "$DISTS_SUPPORTED" | grep $l_DIST >/dev/null && {
			for l_CODENAME in $l_CODENAMES; do

				# in case we build a special CODENAME (squeeze, wheezy, lucid, ...) do skip
				# the wrong distribution here...
				test -z $CODENAMES || echo $line | grep $CODENAMES || break

				if [ "x$EXTRA_ARCHS_ONLY" = "x" ]; then
					for l_ARCH in amd64 i386; do
						[ "x$SKIP_ARCH" != "x$l_ARCH" ] && {
							mkdir -p "$PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH"
							rm -f "$PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/dupload.conf"
							rm -f "$PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/$PROJECT_"*.changes
							rm -f "$PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/$PROJECT_"*.upload
							rm -f "$PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/$PROJECT_"*.build
							rm -f "$PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/$PROJECT_"*.dsc
							rm -f "$PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/$PROJECT_"*.tar.gz
							rm -f "$PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/"*.deb
						}
					done
				fi
				for l_EXTRA_ARCH in $EXTRA_ARCHS; do 
					mkdir -p "$PKGDIST/$l_DIST/$l_CODENAME/$l_EXTRA_ARCH"
					rm -f "$PKGDIST/$l_DIST/$l_CODENAME/$l_EXTRA_ARCH/dupload.conf"
					rm -f "$PKGDIST/$l_DIST/$l_CODENAME/$l_EXTRA_ARCH/$PROJECT_"*.changes
					rm -f "$PKGDIST/$l_DIST/$l_CODENAME/$l_EXTRA_ARCH/$PROJECT_"*.upload
					rm -f "$PKGDIST/$l_DIST/$l_CODENAME/$l_EXTRA_ARCH/$PROJECT_"*.build
					rm -f "$PKGDIST/$l_DIST/$l_CODENAME/$l_EXTRA_ARCH/$PROJECT_"*.dsc
					rm -f "$PKGDIST/$l_DIST/$l_CODENAME/$l_EXTRA_ARCH/$PROJECT_"*.tar.gz
					rm -f "$PKGDIST/$l_DIST/$l_CODENAME/$l_EXTRA_ARCH/"*.deb
				done
			done
		}
	done
	return 0
}

build_packages() {

	# tar upstream sources from our Git clone
	TEMP_DIR="$(mktemp -d --tmpdir=$TEMP_BASE)"
	mkdir -p "$TEMP_DIR/$PROJECT"
	chmod 2770 "$TEMP_DIR" -Rf
	cd "$PROJECT_DIR"
	git clone --local "$PROJECT_DIR" "$TEMP_DIR/$PROJECT/"
	cd $TEMP_DIR
	rm -Rf "$PROJECT/.git"
	cp $PROJECT/$PROJECT.spec .
	UPSTREAM_VERSION=$(cat $PROJECT.spec | egrep "^Version:.*" | awk '{ print $2 }')
	GITREV=$(cd $PROJECT && gitrevno && cd - 1>/dev/null)

	mv $TEMP_DIR/$PROJECT $TEMP_DIR/$PROJECT-$UPSTREAM_VERSION
	tar -czf ~/rpmbuild/SOURCES/$PROJECT-$UPSTREAM_VERSION.tar.gz $PROJECT-$UPSTREAM_VERSION

	# create the SRPM package
	rpmbuild -bs $PROJECT.spec

	# clean up the Git clone from the temp folder
	cd && rm $TEMP_DIR/$PROJECT -Rf


	cat "$PROJECT_DIR/RPM_BUILDS_FOR" | egrep -v '(^$|^#.*$)' | while read line; do
		l_DIST="$(echo ${line/: /:} | cut -d":" -f1 | tr [:upper:] [:lower:])"
		l_CODENAMES="${CODENAMES:-$(echo ${line/: /:} | cut -d":" -f2- | tr [:upper:] [:lower:])}"
		echo "$DISTS_SUPPORTED" | grep $l_DIST >/dev/null && {
			for l_CODENAME in $l_CODENAMES; do

				test -z $CODENAMES || echo $line | grep $CODENAMES || break

				# modify the section for non-main package builds

				### TODO: How to host different builds (releases / nightlies in the same repository???)

				#[ "x$COMPONENT" != "xmain" ] && {
				#	mv debian/control debian/control.tmp
				#	cat debian/control.tmp | sed  "s#Section:[\ ]*\(.*\)#Section: $COMPONENT/\1#g" > debian/control
				#}

				# modify changelog for this build

				### TODO: manipulate the version string in the .spec file

				#if [ "$COMPONENT" != "$COMPONENT_NIGHTLY" ]; then
				#	dch --distribution $VERSION --force-distribution -l "+git$DATE.$GITREV+$l_CODENAME.$COMPONENT." "Auto-built $l_DIST $l_CODENAME package for $REPOS_SERVER repository (Git commit: $GIT_OBJECT_ID)."
				#else
				#	dch --distribution $VERSION --force-distribution -l "~git$DATE.$GITREV+$l_CODENAME.$COMPONENT." "Development-Snapshot!!! Auto-built $l_DIST $l_CODENAME package for $REPOS_SERVER repository (Git commit: $GIT_OBJECT_ID)."
				#fi


				mkdir -p $PKGDIST/$l_DIST/$l_CODENAME/{amd64,i386}

				# TODO: obtain packages from our YUM repository???

				#OTHERMIRROR=""
				#if [ "x$COMPONENT" = "x$COMPONENT_NIGHTLY" ]; then
				#	echo $PACKAGE_WITHOUT_OTHERMIRROR | grep $PROJECT >/dev/null || OTHERMIRROR="deb http://$REPOS_SERVER/$l_DIST $l_CODENAME $COMPONENT_MAIN $COMPONENT"
				#else
				#	echo $PACKAGE_WITHOUT_OTHERMIRROR | grep $PROJECT >/dev/null || OTHERMIRROR="deb http://$REPOS_SERVER/$l_DIST $l_CODENAME $COMPONENT"
				#fi

				[ "x$EXTRA_ARCHS_ONLY" = "x" ] && {

					### TODO: build amd64/i386 packages here

					echo .
					#[ "x$SKIP_ARCH" != "xamd64" ] && cat debian/control | egrep 'Architecture.*(all|any|amd64)' >/dev/null && {
					#	DIST=$l_DIST CODENAME=$l_CODENAME ARCH=amd64 $PDEBUILD --auto-debsign --debsign-k $GPG_KEY --buildresult "$PKGDIST/$l_DIST/$l_CODENAME/amd64" -- --allow-untrusted $SA_OPTION 0<&-
					#}
					#[ "x$SKIP_ARCH" != "xi386" ] && cat debian/control | egrep 'Architecture.*(any|i386)' >/dev/null && {
					#	DIST=$l_DIST CODENAME=$l_CODENAME ARCH=i386  $PDEBUILD --auto-debsign --debsign-k $GPG_KEY --buildresult "$PKGDIST/$l_DIST/$l_CODENAME/i386"  -- --binary-arch --allow-untrusted $SA_OPTION 0<&-
					#}
				}
				for extra_arch in $EXTRA_ARCHS; do
					mkdir -p "$PKGDIST/$l_DIST/$l_CODENAME/$extra_arch"

					### TODO: build extra-arch packages here

					echo .
					#cat debian/control | egrep "Architecture.*(any|$extra_arch)" >/dev/null && {
					#	DIST=$l_DIST CODENAME=$l_CODENAME ARCH=$extra_arch $PDEBUILD --auto-debsign --debsign-k $GPG_KEY --buildresult "$PKGDIST/$l_DIST/$l_CODENAME/$extra_arch" -- --binary-arch --allow-untrusted $SA_OPTION 0<&-
					#}
				done
			done
		}
	done
	rm -Rf "$TEMP_DIR"
	return 0
}

upload_packages() {
	# dupload the new packages to the reprepro repository
	cat "$PROJECT_DIR/RPM_BUILDS_FOR" | egrep -v '(^$|^#.*$)' | while read line; do
		l_DIST=$(echo ${line/: /:} | cut -d":" -f1 | tr [:upper:] [:lower:])
		l_CODENAMES=${CODENAMES:-$(echo ${line/: /:} | cut -d":" -f2- | tr [:upper:] [:lower:])}
		for l_CODENAME in $l_CODENAMES; do

			# in case we build a special CODENAME (squeeze, wheezy, lucid, ...) do skip
			# the wrong distribution here...
			test -z $CODENAMES || echo $line | grep $CODENAMES || break

			if [ "x$EXTRA_ARCHS_ONLY" = "x" ]; then	
				for l_ARCH in amd64 i386; do
					[ "x$SKIP_ARCH" != "x$l_ARCH" ] && {
						cd "$PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH"

						# TODO: upload package

						#test -f ./dupload.conf || ln -s ~/.dupload.conf.$PREFIX ./dupload.conf
						#ls $PROJECT_*.changes &>/dev/null && dupload -c --to $PREFIX-$l_DIST-$l_CODENAME $PROJECT_*.changes 0<&-
					}
				done
			fi
			for l_EXTRA_ARCH in $EXTRA_ARCHS; do 
				cd "$PKGDIST/$l_DIST/$l_CODENAME/$l_EXTRA_ARCH"

				# TODO: upload package

				#test -f ./dupload.conf || ln -s ~/.dupload.conf.$PREFIX ./dupload.conf
				#ls $PROJECT_*.changes &>/dev/null && dupload -c --to $PREFIX-$l_DIST-$l_CODENAME $PROJECT_*.changes 0<&-
			done
		done
	done
	return 0
}

### MAIN ###
set_vars $@ && {
	if [ "x$(basename $0)" = "x$PREFIX-build-rpm-package" ] || [ "x$(basename $0)" = "x$PREFIX-build+upload-rpm-package" ]; then
		cd $PROJECT_DIR && pkgneedsbuild $CHECKOUT || [ "$FORCE_BUILD" -eq 0 ] && {
			prepare_workspace && {
				clear_pkgdist
				build_packages
			}
		}
	fi
	if [ "x$(basename $0)" = "x$PREFIX-upload-rpm-package" ] || [ "x$(basename $0)" = "x$PREFIX-build+upload-rpm-package" ]; then
		upload_packages
	fi
}
