From cfa6c7d67ab934dfe1dd083f11f2ce4dcc400afc Mon Sep 17 00:00:00 2001
From: Dubiousjim <dubiousjim@gmail.com>
Date: Fri, 5 Jul 2013 00:21:39 -0400
Subject: [PATCH] various: fancier readconfig, permit env overrides

---
 abuild-keygen.in | 14 ++++-----
 abuild-sign.in   |  4 +--
 checkapk.in      |  2 +-
 functions.sh.in  | 75 +++++++++++++++++++++++++++++++++++++++---------
 4 files changed, 71 insertions(+), 24 deletions(-)

diff --git a/abuild-keygen.in b/abuild-keygen.in
index dbedad1d09b7..c02ad829f68e 100644
--- a/abuild-keygen.in
+++ b/abuild-keygen.in
@@ -30,7 +30,7 @@ get_privkey_file() {
 
 	default_name="${emailaddr:-$USER}-$(printf "%x" $(date +%s))"
 
-	privkey="$abuild_home/$default_name.rsa"
+	privkey="$ABUILD_USERDIR/$default_name.rsa"
 	[ -n "$non_interactive" ] && return 0
 	msg "Generating public/private rsa key pair for abuild"
 	echo -n "Enter file in which to save the key [$privkey]: "
@@ -42,7 +42,7 @@ get_privkey_file() {
 }
 
 do_keygen() {
-	mkdir -p "$abuild_home"
+	mkdir -p "$ABUILD_USERDIR"
 
 	get_privkey_file
 	pubkey="$privkey.pub"
@@ -68,14 +68,14 @@ do_keygen() {
 	fi
 
 	if [ -n "$append_config" ]; then
-		if [ -f "$abuild_userconf" ]; then
+		if [ -f "$ABUILD_USERCONF" ]; then
 			# comment out the existing values
-			sed -i -e 's/^PACKAGER_PRIVKEY=/\#&/' "$abuild_userconf"
+			sed -i -e 's/^PACKAGER_PRIVKEY=/\#&/' "$ABUILD_USERCONF"
 		fi
-		echo "PACKAGER_PRIVKEY=\"$privkey\"" >> "$abuild_userconf"
+		echo "PACKAGER_PRIVKEY=\"$privkey\"" >> "$ABUILD_USERCONF"
 	else
 		msg ""
-		msg "You might want add following line to $abuild_userconf:"
+		msg "You might want add following line to $ABUILD_USERCONF:"
 		msg ""
 		msg "PACKAGER_PRIVKEY=\"$privkey\""
 		msg ""
@@ -92,7 +92,7 @@ usage() {
 $prog $abuild_ver - generate signing keys
 Usage: $prog [-a|--append] [-i|--install] [-n]
 Options:
-  -a, --append   Set PACKAGER_PRIVKEY=<generated key> in $abuild_userconf
+  -a, --append   Set PACKAGER_PRIVKEY=<generated key> in $ABUILD_USERCONF
   -i, --install  Install public key into /etc/apk/keys using sudo
   -n             Non-interactive. Use defaults
   -q, --quiet
diff --git a/abuild-sign.in b/abuild-sign.in
index c1a0c779378c..f00c0a185a65 100644
--- a/abuild-sign.in
+++ b/abuild-sign.in
@@ -85,9 +85,9 @@ if [ -z "$privkey" ]; then
 	cat >&2 << __EOF__
 No private key found. Use 'abuild-keygen' to generate the keys.
 Then you can either:
-  * set the PACKAGER_PRIVKEY in $abuild_userconf
+  * set the PACKAGER_PRIVKEY in $ABUILD_USERCONF
     ('abuild-keygen -a' does this for you)
-  * set the PACKAGER_PRIVKEY in $abuild_conf
+  * set the PACKAGER_PRIVKEY in $ABUILD_CONF
   * specify the key with the -k option to $prog
 
 __EOF__
diff --git a/checkapk.in b/checkapk.in
index d3e298807c0a..0d31a0c563fc 100644
--- a/checkapk.in
+++ b/checkapk.in
@@ -31,7 +31,7 @@ if [ $# -gt 0 ]; then
 	exit 2
 fi
 
-if ! [ -f "$abuild_conf" ] && ! [ -f "$abuild_userconf" ]; then
+if ! [ -f "$ABUILD_CONF" ] && ! [ -f "$ABUILD_USERCONF" ]; then
 	die "no abuild.conf found"
 fi
 
diff --git a/functions.sh.in b/functions.sh.in
index ab4dff5ff07d..0b0bc3bafd0a 100644
--- a/functions.sh.in
+++ b/functions.sh.in
@@ -3,20 +3,67 @@
 sysconfdir=@sysconfdir@
 prog=${0##*/}
 
-
-abuild_conf=${ABUILD_CONF:-"$sysconfdir/abuild.conf"}
-abuild_home=${ABUILD_USERDIR:-"$HOME/.abuild"}
-abuild_userconf=${ABUILD_USERCONF:-"$abuild_home/abuild.conf"}
-
-# read config
-if [ -f "$abuild_conf" ]; then
-	. "$abuild_conf" || abuild_conf=
-fi
-
-# read user config if exists
-if [ -f "$abuild_userconf" ]; then
-	. "$abuild_userconf" || abuild_userconf=
-fi
+readconfig() {
+	local _APORTSDIR _BUILDDIR _PKGDEST _SRCPKGDEST _REPODEST _SRCDEST
+	local _CARCH _CHOST _CTARGET _CPPFLAGS _CFLAGS _CXXFLAGS _LDFLAGS
+	local _JOBS _MAKEFLAGS _PACKAGER _USE_COLORS
+	local gitbase=
+	[ -n "${APORTSDIR+x}" ] && _APORTSDIR=$APORTSDIR
+	[ -n "${BUILDDIR+x}" ] && _BUILDDIR=$BUILDDIR
+	[ -n "${PKGDEST+x}" ] && _PKGDEST=$PKGDEST
+	[ -n "${SRCPKGDEST+x}" ] && _SRCPKGDEST=$SRCPKGDEST
+	[ -n "${REPODEST+x}" ] && _REPODEST=$REPODEST
+	[ -n "${SRCDEST+x}" ] && _SRCDEST=$SRCDEST
+	[ -n "${CARCH+x}" ] && _CARCH=$CARCH
+	[ -n "${CHOST+x}" ] && _CHOST=$CHOST
+	[ -n "${CTARGET+x}" ] && _CTARGET=$CTARGET
+	[ -n "${CPPFLAGS+x}" ] && _CPPFLAGS=$CPPFLAGS
+	[ -n "${CFLAGS+x}" ] && _CFLAGS=$CFLAGS
+	[ -n "${CXXFLAGS+x}" ] && _CXXFLAGS=$CXXFLAGS
+	[ -n "${LDFLAGS+x}" ] && _LDFLAGS=$LDFLAGS
+	[ -n "${JOBS+x}" ] && _JOBS=$JOBS
+	[ -n "${MAKEFLAGS+x}" ] && _MAKEFLAGS=$MAKEFLAGS
+	[ -n "${PACKAGER+x}" ] && _PACKAGER=$PACKAGER
+	[ -n "${USE_COLORS+x}" ] && _USE_COLORS="$USE_COLORS"
+	: ${ABUILD_CONF:=$sysconfdir/abuild.conf}
+	: ${ABUILD_USERDIR:=$HOME/.abuild}
+	: ${ABUILD_USERCONF:=$ABUILD_USERDIR/abuild.conf}
+	[ -f "$ABUILD_CONF" ] && . "$ABUILD_CONF"
+	[ -f "$ABUILD_USERCONF" ] && . "$ABUILD_USERCONF"
+	APORTSDIR=${_APORTSDIR-$APORTSDIR}
+	gitbase=$(git rev-parse --show-toplevel 2>/dev/null) # already is -P
+	if [ -d "$APORTSDIR" ]; then
+		APORTSDIR=$(cd "$APORTSDIR"; pwd -P)
+	elif [ -z "$APORTSDIR" ] && [ -d "$HOME/aports" ]; then
+		APORTSDIR=$(cd "$HOME/aports"; pwd -P)
+	else
+		if [ -n "$gitbase" ]; then
+			case $(git config remote.origin.url) in
+			*/aports) APORTSDIR=$gitbase ;;
+			*) APORTSDIR= ;;
+			esac
+		else
+			APORTSDIR=
+		fi
+	fi
+	BUILDDIR=${_BUILDDIR-$BUILDDIR}
+	PKGDEST=${_PKGDEST-$PKGDEST}
+	SRCPKGDEST=${_SRCPKGDEST-$SRCPKGDEST}
+	REPODEST=${_REPODEST-$REPODEST}
+	SRCDEST=${_SRCDEST-$SRCDEST}
+	CARCH=${_CARCH-$CARCH}
+	CHOST=${_CHOST-$CHOST}
+	CTARGET=${_CTARGET-$CTARGET}
+	CPPFLAGS=${_CPPFLAGS-$CPPFLAGS}
+	CFLAGS=${_CFLAGS-$CFLAGS}
+	CXXFLAGS=${_CXXFLAGS-$CXXFLAGS}
+	LDFLAGS=${_LDFLAGS-$LDFLAGS}
+	JOBS=${_JOBS-$JOBS}
+	MAKEFLAGS=${_MAKEFLAGS-$MAKEFLAGS}
+	PACKAGER=${_PACKAGER-$PACKAGER}
+	USE_COLORS=${_USE_COLORS-$USE_COLORS}
+}
+readconfig
 
 
 # expects $1 to be a package directory in the aports tree ('foo' or 'main/foo')
-- 
GitLab