From 8aa0a4560e7eb5970b35a509a4519455735ccb92 Mon Sep 17 00:00:00 2001
From: Darwin Bautista <djclue917_at_gmail.com>
Date: Thu, 10 Jan 2008 20:26:16 +0800
Subject: [PATCH] Automatically include a '>=' version requirement for the dependencies.

This option would only affect dependencies which have no explicitly-defined
version requirement. For instance, depends=('pacman<3.0.0' 'gtk2')
would "become" depends=('pacman<3.0.0' 'gtk2>=2.12.3').

Signed-off-by: Darwin Bautista <djclue917_at_gmail.com>
---
 doc/makepkg.8.txt     |    6 ++++
 scripts/makepkg.sh.in |   69 ++++++++++++++++++++++++++++++++++---------------
 2 files changed, 54 insertions(+), 21 deletions(-)

diff --git a/doc/makepkg.8.txt b/doc/makepkg.8.txt
index 689556d..f3c1505 100644
--- a/doc/makepkg.8.txt
+++ b/doc/makepkg.8.txt
@@ -94,6 +94,12 @@ Options
 	Read the package script `buildscript` instead of the `PKGBUILD` default;
 	see linkman:PKGBUILD[5].
 
+*-v, \--versiondeps*::
+	Automatically include a '>=' version requirement for the dependencies.
+	This option would only affect dependencies which have no explicitly-defined
+	version requirement. For instance, depends=('pacman<3.0.0' 'gtk2')
+	would "become" depends=('pacman<3.0.0' 'gtk2>=2.12.3')
+
 *-r, \--rmdeps*::
 	Upon successful build, remove any dependencies installed by makepkg
 	during dependency auto-resolution (using `-b` or `-s`).
diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in
index 5dbfb88..32fb051 100644
--- a/scripts/makepkg.sh.in
+++ b/scripts/makepkg.sh.in
@@ -53,6 +53,7 @@ NOBUILD=0
 NODEPS=0
 NOEXTRACT=0
 RMDEPS=0
+VERSIONDEPS=0
 REPKG=0
 LOGGING=0
 SOURCEONLY=0
@@ -439,6 +440,29 @@ remove_deps() {
 	fi
 }
 
+version_dep() {
+	local dep="$*"
+	local depver ret
+
+	if [ -z "$(echo $dep | grep -E '<|=|>')" ]; then
+		depver="$(pacman -Q $dep | cut -d' ' -f2 | cut -d'-' -f1)"
+		ret=$?
+
+		if [ $ret -ne 0 ]; then
+			error "$(gettext "Pacman returned a fatal error (%i): %s")" "$ret" "$depver"
+			exit 1
+		fi
+
+		if [ -n "$depver" ]; then
+			echo "$dep>=$depver"
+		else
+			echo "$dep"
+		fi
+	else
+		echo "$dep"
+	fi
+}
+
 download_sources() {
 	msg "$(gettext "Retrieving Sources...")"
 
@@ -815,6 +839,7 @@ create_package() {
 		echo "group = $it" >>.PKGINFO
 	done
 	for it in "${depends[@]}"; do
+		[ "$VERSIONDEPS" = "1" ] && it=$(version_dep $it)
 		echo "depend = $it" >>.PKGINFO
 	done
 	for it in "${optdepends[@]}"; do
@@ -1051,26 +1076,27 @@ usage() {
 	printf "$(gettext "Usage: %s [options]")\n" "$0"
 	echo
 	echo "$(gettext "Options:")"
-	printf "$(gettext "  -A, --ignorearch Ignore incomplete arch field in %s")\n" "$BUILDSCRIPT"
-	echo "$(gettext "  -b, --builddeps  Build missing dependencies from source")"
-	echo "$(gettext "  -c, --clean      Clean up work files after build")"
-	echo "$(gettext "  -C, --cleancache Clean up source files from the cache")"
-	echo "$(gettext "  -d, --nodeps     Skip all dependency checks")"
-	echo "$(gettext "  -e, --noextract  Do not extract source files (use existing src/ dir)")"
-	echo "$(gettext "  -f, --force      Overwrite existing package")"
-	echo "$(gettext "  -g, --geninteg   Generate integrity checks for source files")"
-	echo "$(gettext "  -h, --help       This help")"
-	echo "$(gettext "  -i, --install    Install package after successful build")"
-	echo "$(gettext "  -L, --log        Log package build process")"
-	echo "$(gettext "  -m, --nocolor    Disable colorized output messages")"
-	echo "$(gettext "  -o, --nobuild    Download and extract files only")"
-	printf "$(gettext "  -p <buildscript> Use an alternate build script (instead of '%s')")\n" "$BUILDSCRIPT"
-	echo "$(gettext "  -r, --rmdeps     Remove installed dependencies after a successful build")"
+	printf "$(gettext "  -A, --ignorearch  Ignore incomplete arch field in %s")\n" "$BUILDSCRIPT"
+	echo "$(gettext "  -b, --builddeps   Build missing dependencies from source")"
+	echo "$(gettext "  -c, --clean       Clean up work files after build")"
+	echo "$(gettext "  -C, --cleancache  Clean up source files from the cache")"
+	echo "$(gettext "  -d, --nodeps      Skip all dependency checks")"
+	echo "$(gettext "  -e, --noextract   Do not extract source files (use existing src/ dir)")"
+	echo "$(gettext "  -f, --force       Overwrite existing package")"
+	echo "$(gettext "  -g, --geninteg    Generate integrity checks for source files")"
+	echo "$(gettext "  -h, --help        This help")"
+	echo "$(gettext "  -i, --install     Install package after successful build")"
+	echo "$(gettext "  -L, --log         Log package build process")"
+	echo "$(gettext "  -m, --nocolor     Disable colorized output messages")"
+	echo "$(gettext "  -o, --nobuild     Download and extract files only")"
+	printf "$(gettext "  -p <buildscript>  Use an alternate build script (instead of '%s')")\n" "$BUILDSCRIPT"
+	echo "$(gettext "  -v, --versiondeps Automatically include a '>=' version requirement for the dependencies")"
+	echo "$(gettext "  -r, --rmdeps      Remove installed dependencies after a successful build")"
 	# fix flyspray feature request #2978
-	echo "$(gettext "  -R, --repackage  Repackage contents of pkg/ without building")"
-	echo "$(gettext "  -s, --syncdeps   Install missing dependencies with pacman")"
-	echo "$(gettext "      --asroot     Allow makepkg to run as root user")"
-	echo "$(gettext "      --source     Do not build package; generate a source-only tarball")"
+	echo "$(gettext "  -R, --repackage   Repackage contents of pkg/ without building")"
+	echo "$(gettext "  -s, --syncdeps    Install missing dependencies with pacman")"
+	echo "$(gettext "      --asroot      Allow makepkg to run as root user")"
+	echo "$(gettext "      --source      Do not build package; generate a source-only tarball")"
 	echo
 	echo "$(gettext "These options can be passed to pacman:")"
 	echo
@@ -1116,9 +1142,9 @@ SRCDEST=${_SRCDEST:-$SRCDEST}
 SRCDEST=${SRCDEST:-$startdir} #default to $startdir if undefined
 
 # Parse Command Line Options.
-OPT_SHORT="AbcCdefFghiLmop:rRsSV"
+OPT_SHORT="AbcCdefFghiLmop:vrRsSV"
 OPT_LONG="ignorearch,asroot,builddeps,clean,cleancache,nodeps,noextract,force,geninteg,help,install,log"
-OPT_LONG="$OPT_LONG,nocolor,nobuild,rmdeps,repackage,source,syncdeps,usesudo,version,forcever:"
+OPT_LONG="$OPT_LONG,nocolor,nobuild,versiondeps,rmdeps,repackage,source,syncdeps,usesudo,version,forcever:"
 # Pacman Options
 OPT_LONG="$OPT_LONG,noconfirm,noprogressbar"
 OPT_TEMP="$(getopt -o "$OPT_SHORT" -l "$OPT_LONG" -n "$(basename "$0")" -- "$@" || echo 'GETOPT GO BANG!')"
@@ -1151,6 +1177,7 @@ while true; do
 		-m|--nocolor)     USE_COLOR='n' ;;
 		-o|--nobuild)     NOBUILD=1 ;;
 		-p)               shift; BUILDSCRIPT=$1 ;;
+		-v|--versiondeps) VERSIONDEPS=1 ;;
 		-r|--rmdeps)      RMDEPS=1 ;;
 		-R|--repackage)   REPKG=1 ;;
 		--source)         SOURCEONLY=1 ;;
-- 
1.5.3.7

