#!/bin/bash
#
#   bumppkg
#
#   Copyright (c) 2006-2007 by Miklos Vajna <vmiklos@frugalware.org>
#
#   This program 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 2 of the License, or
#   (at your option) any later version.
#
#   This program 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
#   USA.
#

die()
{
	echo $0: $@
	exit 1
}

check_complex()
{
	if [ ! -f FrugalBuild ]; then
		die "Could not find FrugalBuild!"
	fi
	if [ "`grep -c sha1sums= FrugalBuild`" -gt 1 ]; then
		die "This FrugalBuild is too complex, update it manually!"
	fi
}

get_root()
{
	local i
	if [ "$F_makepkg_scm" = "git" ]; then
		i=`git rev-parse --git-dir 2>/dev/null`
		echo `dirname $i`
	else
	i=`pwd`
	while true
	do
		if [ -e "$i/_darcs" ]; then
			break
		elif [ "$i" == "" ]; then
			break
		fi
		i=`echo $i|sed 's|\(.*\)/.*|\1|'`
	done
	echo $i
	fi
}
USE_COLOR=y
src_cleanup()
{
	Fmessage "cleaning up old source files"
	local root="`get_root`"
	if [ "$F_makepkg_scm" = "git" ]; then
		git clean -x -d -f .
	else
	local i workdir rootlist darcsdir

	workdir=`pwd|sed "s|$root||"`
	rootlist=`mktemp`
	worklist=`mktemp`
	darcsdir="_darcs/pristine"
	if [ ! -d $root/$darcsdir ]; then
		darcsdir="_darcs/current"
	fi
	ls $root/$darcsdir/$workdir >$rootlist
	ls >$worklist
	for i in `diff -u $rootlist $worklist|grep ^+[^+]|sed 's/^+//'`
	do
		rm -v $i
	done
	rm $rootlist $worklist
	fi
}

update_pkgrel()
{
	local rel=$1
	[ -z "$rel" ] && rel=1
	sed -i "s/^pkgrel=.*/pkgrel=$rel/" FrugalBuild
}

update_pkgver()
{
	Fmessage "updating \$pkgver and \$pkgrel"
	local newpkgver=`chkworld |grep != |sed 's/.*!= \([^ ]*\) .*/\1/'`
	if [[ "$newpkgver" =~ " " ]] || [ -z "$newpkgver" ]; then
		die "Dunno what is the new version or this package is already up to date!"
	fi
	sed -i "s/^pkgver=.*/pkgver=$newpkgver/;/# vim: ft=sh/d" FrugalBuild
}

src_download()
{
	Fmessage "running makepkg -G"
	makepkg -G -t $1
	if [ $? != 0 ]; then
		die "failed to run makepkg -G"
	fi

	Fmessage "cleaning up the duplicated source files"
	rm -rf pkg src
}

do_build()
{
	sudo makepkg -t $1 -c$2 || die "build failed!"
	Fmessage "done, testing time!"
}

do_push()
{
	if [ "$1" == "--push" ]; then
		repoman -t $tree -k push
	elif [ "$1" == "--silentpush" ]; then
		dg push -a
	fi
}


. /usr/lib/frugalware/fwmakepkg
. /etc/repoman.conf

if [ "$1" == "-h" -o "$1" == "--help" ]; then
	man bumppkg
	exit $?
fi

if [ "$1" == "-v" -o "$1" == "--version" ]; then
	pacman-g2 -Q pacman-tools
	exit $?
fi

tree=$repos
if [ "$1" == "-t" ]; then
	tree=$2
	shift 2
fi

if [ "$1" == "--rebuild" ]; then
	shift 1
	source ./FrugalBuild || die "failed to source the FrugalBuild!"
	newrel=$(echo $pkgrel|sed 's/[0-9]\+$//')$(($(echo $pkgrel|sed 's/.*[^0-9]\([0-9]\+\)$/\1/')+1))
	update_pkgrel $newrel
	if [ "$1" != "--nobuild" ]; then
		do_build $tree u
	else
		shift 1
	fi
	if [ -n "$1" ]; then
		repoman rec "$1"
		shift 1
	fi
	if [ "$1" == "--push" -o "$1" == "--silentpush" ]; then
		do_push $1
	fi
	exit 0
fi


if [ "$1" = "--update" ]; then

	source ./FrugalBuild || die "failed to source the FrugalBuild!"
	newrel=$(echo $pkgrel|sed 's/[0-9]\+$//')$(($(echo $pkgrel|sed 's/.*[^0-9]\([0-9]\+\)$/\1/')+1))
	Fmessage "Updating \$pkgrel from: $pkgrel to $newrel."
	update_pkgrel $newrel
	exit 0
fi

check_complex
update_pkgver
update_pkgrel
src_cleanup
src_download $tree
do_build $tree
if [ "$1" == "--push" -o "$1" == "--silentpush" ]; then
	do_push $1
fi
