[pacman-dev] [PATCH] scripts/*.sh.in: Clean up and fix a few bugs

Andrew Fyfe andrew at neptune-one.net
Wed Aug 29 17:33:07 EDT 2007


	repo-add, repo-remove:
		'bsdtar -c * | ...' doesn't work (you need '-f -'). Code clean up
		eliminated this bug.

		Removed the multiple checksum support, pacman now only supports MD5, so
		there's no need for the database to contain multiple checksums.

		Quote all variables containing file/dir names to prevent paths
		containing spaces from causing problems.

		Add msg, warning and error functions.

		General code clean up.

	pacman-optimize:
		Use a sub-directory in /tmp for working files to make it easier to clean
		up at the end.

		Add quotes round $@ in die and die_r, otherwise printf can't display the
		message correctly.

	makepkg:
		Disable colour output if stderr is not a tty.

Signed-off-by: Andrew Fyfe <andrew at neptune-one.net>
---
 scripts/makepkg.sh.in         |   10 +-
 scripts/pacman-optimize.sh.in |   49 ++++---
 scripts/repo-add.sh.in        |  298 ++++++++++++++++++++---------------------
 scripts/repo-remove.sh.in     |  144 +++++++++++++-------
 4 files changed, 267 insertions(+), 234 deletions(-)

diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in
index cd5dbf5..d5b4114 100644
--- a/scripts/makepkg.sh.in
+++ b/scripts/makepkg.sh.in
@@ -66,7 +66,7 @@ PACMAN_OPTS=
 
 plain() {
 	local mesg=$1; shift
-	if [ ! "$USE_COLOR" = "n" -a "$(check_buildenv color)" = "y" ]; then
+	if [ -t 2 -a ! "$USE_COLOR" = "n" -a "$(check_buildenv color)" = "y" ]; then
 		printf "\033[1;37m    ${mesg}\033[0m\n" "$@" >&2
 	else
 		printf "    ${mesg}\n" "$@" >&2
@@ -75,7 +75,7 @@ plain() {
 
 msg() {
 	local mesg=$1; shift
-	if [ ! "$USE_COLOR" = "n" -a "$(check_buildenv color)" = "y" ]; then
+	if [ -t 2 -a ! "$USE_COLOR" = "n" -a "$(check_buildenv color)" = "y" ]; then
 		printf "\033[1;32m==>\033[1;37m ${mesg}\033[0m\n" "$@" >&2
 	else
 		printf "==> ${mesg}\n" "$@" >&2
@@ -84,7 +84,7 @@ msg() {
 
 msg2() {
 	local mesg=$1; shift
-	if [ ! "$USE_COLOR" = "n" -a "$(check_buildenv color)" = "y" ]; then
+	if [ -t 2 -a ! "$USE_COLOR" = "n" -a "$(check_buildenv color)" = "y" ]; then
 		printf "\033[1;34m  ->\033[1;37m ${mesg}\033[0m\n" "$@" >&2
 	else
 		printf "  -> ${mesg}\n" "$@" >&2
@@ -93,7 +93,7 @@ msg2() {
 
 warning() {
 	local mesg=$1; shift
-	if [ ! "$USE_COLOR" = "n" -a "$(check_buildenv color)" = "y" ]; then
+	if [ -t 2 -a ! "$USE_COLOR" = "n" -a "$(check_buildenv color)" = "y" ]; then
 		printf "\033[1;33m==> $(gettext "WARNING:")\033[1;37m ${mesg}\033[0m\n" "$@" >&2
 	else
 		printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2
@@ -102,7 +102,7 @@ warning() {
 
 error() {
 	local mesg=$1; shift
-	if [ ! "$USE_COLOR" = "n" -a "$(check_buildenv color)" = "y" ]; then
+	if [ -t 2 -a ! "$USE_COLOR" = "n" -a "$(check_buildenv color)" = "y" ]; then
 		printf "\033[1;31m==> $(gettext "ERROR:")\033[1;37m ${mesg}\033[0m\n" "$@" >&2
 	else
 		printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2
diff --git a/scripts/pacman-optimize.sh.in b/scripts/pacman-optimize.sh.in
index 4c47d88..83c56a7 100644
--- a/scripts/pacman-optimize.sh.in
+++ b/scripts/pacman-optimize.sh.in
@@ -63,13 +63,13 @@ There is NO WARRANTY, to the extent permitted by law.\n")"
 }
 
 die() {
-	error $@
+	error "$@"
 	exit 1
 }
 
 die_r() {
-	rm -f $lockfile
-	die $@
+	rm -f "$lockfile"
+	die "$@"
 }
 
 if [ "$1" = "-h" -o "$1" = "--help" ]; then
@@ -87,8 +87,8 @@ if [ "$1" != "" ]; then
 fi
 
 # make sure pacman isn't running
-if [ -f $lockfile ]; then
-	die "$(gettext "Pacman lockfile was found. Cannot run while pacman is running.")"
+if [ -f "$lockfile" ]; then
+	die "$(gettext "Pacman lock file was found. Cannot run while pacman is running.")"
 fi
 
 if [ ! -d "$dbroot" ]; then
@@ -99,55 +99,58 @@ if [ ! -w "$dbroot" ]; then
 	die "$(gettext "You must have correct permissions to optimize the database.")"
 fi
 
+workdir=$(mktemp -d /tmp/pacman-optimize.XXXXXXXXXX) ||
+	die_r "$(gettext "ERROR: Can not create temp directory for database building.")\n" >&2
+
 # do not let pacman run while we do this
-touch $lockfile
+touch "$lockfile"
 
 # step 1: sum the old db
 msg "$(gettext "MD5sum'ing the old database...")"
-find $dbroot -type f | sort | xargs md5sum > /tmp/pacsums.old
+find "$dbroot" -type f | sort | xargs md5sum > "$workdir/pacsums.old"
 
 # step 2: tar it up
 msg "$(gettext "Tar'ing up %s...")" "$dbroot"
-cd $dbroot
-bsdtar -czf /tmp/pacmanDB.tgz ./
+cd "$dbroot"
+bsdtar -czf "$workdir/pacmanDB.tgz" ./
 if [ $? -ne 0 ]; then
-	rm -f /tmp/pacmanDB.tgz /tmp/pacsums.old
+	rm -rf "$workdir"
 	die_r "$(gettext "Tar'ing up %s failed.")" "$dbroot"
 fi
 
 # step 3: make and sum the new db
 msg "$(gettext "Making and MD5sum'ing the new db...")"
-mkdir $dbroot.new
-bsdtar -zxpf /tmp/pacmanDB.tgz -C $dbroot.new/
+mkdir "$dbroot.new"
+bsdtar -zxpf "$workdir/pacmanDB.tgz" -C "$dbroot.new/"
 if [ $? -ne 0 ]; then
-       rm -f /tmp/pacmanDB.tgz /tmp/pacsums.old
-       rm -rf "$dbroot.new"
-       die_r "$(gettext "Untar'ing $dbroot failed.")"
+       rm -rf "$workdir"
+       die_r "$(gettext "Untar'ing %s failed.")" "$dbroot"
 fi
-find "$dbroot.new" -type f | sort | sed -e 's/pacman.new/pacman/g' | \
-		xargs md5sum > /tmp/pacsums.new
+find "$dbroot.new" -type f | sort | \
+		xargs md5sum | sed 's#.new/##' > "$workdir/pacsums.new"
 
 # step 4: compare the sums
 msg "$(gettext "Checking integrity...")"
-diff /tmp/pacsums.old /tmp/pacsums.new >/dev/null 2>&1
+diff "$workdir/pacsums.old" "$workdir/pacsums.new" >/dev/null 2>&1
 if [ $? -ne 0 ]; then
 	# failed
 	# leave /tmp/pacsums.old and .new for checking to see what doesn't match up
-	rm -rf "$dbroot.new" $lockfile /tmp/pacmanDB.tgz
-	die_r "$(gettext "integrity check FAILED, reverting to old database.")"
+	rm -rf "$dbroot.new"
+	die_r "$(gettext "Integrity check FAILED, reverting to old database.")"
 fi
 
 # step 5: remove the new temporary database and the old one
 #         and use the .tgz to replace the old one
 msg "$(gettext "Putting the new database in place...")"
 rm -rf "$dbroot.new" "$dbroot"/*
-bsdtar -zxpf /tmp/pacmanDB.tgz -C "$dbroot"/
+bsdtar -zxpf "$workdir/pacmanDB.tgz" -C "$dbroot/"
 
 # remove the lock file, sum files, and .tgz of database
-rm -f $lockfile /tmp/pacsums.old /tmp/pacsums.new /tmp/pacmanDB.tgz
+rm -f "$lockfile"
+rm -rf "$workdir"
 
 echo
-echo "$(gettext "Finished. Your pacman database has been optimized.")"
+msg "$(gettext "Finished. Your pacman database has been optimized.")"
 echo
 
 exit 0
diff --git a/scripts/repo-add.sh.in b/scripts/repo-add.sh.in
index f8c5de2..7420354 100644
--- a/scripts/repo-add.sh.in
+++ b/scripts/repo-add.sh.in
@@ -29,6 +29,42 @@ myver='@PACKAGE_VERSION@'
 FORCE=0
 REPO_DB_FILE=""
 
+msg() {
+	local mesg=$1; shift
+	if [ -t 1 ]; then
+		printf "\033[1;32m==>\033[1;37m ${mesg}\033[0m\n" "$@" >&1
+	else
+		printf "==> ${mesg}\n" "$@" >&1
+	fi
+}
+
+msg2() {
+	local mesg=$1; shift
+	if [ -t 1 ]; then
+		printf "\033[1;34m  ->\033[1;37m ${mesg}\033[0m\n" "$@" >&1
+	else
+		printf "  -> ${mesg}\n" "$@" >&1
+	fi
+}
+
+warning() {
+	local mesg=$1; shift
+	if [ -t 2 ]; then
+		printf "\033[1;33m==> $(gettext "WARNING:")\033[1;37m ${mesg}\033[0m\n" "$@" >&2
+	else
+		printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2
+	fi
+}
+
+error() {
+	local mesg=$1; shift
+	if [ -t 2 ]; then
+		printf "\033[1;31m==> $(gettext "ERROR:")\033[1;37m ${mesg}\033[0m\n" "$@" >&2
+	else
+		printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2
+	fi
+}
+
 # print usage instructions
 usage() {
 	printf "repo-add (pacman) %s\n\n" "$myver"
@@ -51,37 +87,28 @@ This is free software; see the source for copying conditions.\n\
 There is NO WARRANTY, to the extent permitted by law.\n")"
 }
 
-# return calculated checksum of package
-#   arg1 - checksum type
-#   arg2 - path to package
-get_checksum () {
-	case "$(echo "$1" | tr A-Z a-z)" in
-		md5) sum=$(md5sum $2); echo ${sum% *} ;;
-		sha1) sum=$(sha1sum $2); echo ${sum% *} ;;
-		sha256) sum=$(sha256sum $2); echo ${sum% *} ;;
-		sha384) sum=$(sha256sum $2); echo ${sum% *} ;;
-		sha512) sum=$(sha256sum $2); echo ${sum% *} ;;
-	esac
-}
-
-# return PKGINFO string for checksum type
-#   arg1 - checksum type
-checksum_name () {
-	case "$(echo "$1" | tr A-Z a-z)" in
-		md5) echo "MD5SUM" ;;
-		sha1) echo "SHA1SUM" ;;
-		sha256) echo "SHA256SUM" ;;
-		sha384) echo "SHA384SUM" ;;
-		sha512) echo "SHA512SUM" ;;
-	esac
-}
-
 # test if a file is a repository DB
 test_repo_db_file () {
 	if [ -f "$REPO_DB_FILE" ]; then
-		[ "$(bsdtar -tf "$REPO_DB_FILE" | grep -c "/desc")" -gt 0 ] || return 1
+		if bsdtar -tf "$REPO_DB_FILE" | grep -q "/desc"; then
+			return 0 # YES
+		fi
 	else
-		true
+		return 0 # YES - No database file is also aloud.
+	fi
+
+	return 1 # NO
+}
+
+# write a list entry
+#		arg1 - Entry name
+#		arg2 - List
+#		arg3 - File to write to
+write_list_entry() {
+	if [ -n "$2" ]; then
+		echo "%$1%" >>$3
+		echo $2 | tr -s ' ' '\n' >>$3
+		echo "" >>$3
 	fi
 }
 
@@ -90,36 +117,26 @@ test_repo_db_file () {
 db_write_entry()
 {
 	# blank out all variables and set pkgfile
-	pkgfile=$(readlink -f $1)
-	export pkgname=""
-	pkgver=""
-	pkgdesc=""
-	url=""
-	builddate=""
-	packager=""
-	csize=""
-	size=""
-	_groups=""
-	_depends=""
-	_backups=""
-	_licenses=""
-	_replaces=""
-	_provides=""
-	_conflicts=""
-
-	OLDIFS="$IFS"
+	local pkgfile=$(readlink -f "$1")
+	local pkgname pkgver pkgdesc url builddate packager csize size \
+		group depend backup license replaces provides conflict \
+		_groups _depends _backups _licenses _replaces _provides _conflicts
+
+	local OLDIFS="$IFS"
 	# IFS (field seperator) is only the newline character
 	IFS="
 "
 
 	# read info from the zipped package
-	for i in $(bsdtar -xOf "$pkgfile" .PKGINFO | grep -v "^#" |sed 's|\(\w*\)\s*=\s*\(.*\)|\1="\2"|'); do
-		eval "${i}"
-		case "$i" in
-			group=*) _groups="$_groups $group" ;;
-			depend=*) _depends="$_depends $depend" ;;
-			backup=*) _backups="$_backups $backup" ;;
-			license=*) _licenses="$_licenses $license" ;;
+	local line
+	for line in $(bsdtar -xOf "$pkgfile" .PKGINFO | \
+		grep -v "^#" | sed 's|\(\w*\)\s*=\s*\(.*\)|\1="\2"|'); do
+		eval "$line"
+		case "$line" in
+			group=*)    _groups="$_groups $group" ;;
+			depend=*)   _depends="$_depends $depend" ;;
+			backup=*)   _backups="$_backups $backup" ;;
+			license=*)  _licenses="$_licenses $license" ;;
 			replaces=*) _replaces="$_replaces $replaces" ;;
 			provides=*) _provides="$_provides $provides" ;;
 			conflict=*) _conflicts="$_conflicts $conflict" ;;
@@ -129,21 +146,23 @@ db_write_entry()
 	IFS=$OLDIFS
 
 	# get compressed size of package
-	csize="$(du -b -L $pkgfile | cut -f1)"
+	csize=$(du -b -L "$pkgfile" | cut -f 1)
 
-	cd $gstmpdir
+	pushd "$gstmpdir" 2>&1 >/dev/null
 
 	# ensure $pkgname and $pkgver variables were found
 	if [ -z "$pkgname" -o -z "$pkgver" ]; then
-		printf "$(gettext "   error: invalid package file")\n"
+		error "$(gettext "ERROR: Invalid package file '%s'.")" "$pkgfile"
+		popd 2>&1 >/dev/null
 		return 1
 	fi
 
 	# remove any other package in the DB with same name
+	local existing
 	for existing in *; do
 		if [ "${existing%-*-*}" = "$pkgname" ]; then
-			printf "$(gettext ":: removing existing package '%s'")\n" "$existing"
-			rm -rf $existing
+			msg2 "$(gettext "Removing existing package '%s'...")" "$existing"
+			rm -rf "$existing"
 		fi
 	done
 
@@ -152,67 +171,37 @@ db_write_entry()
 	cd "$pkgname-$pkgver"
 
 	# create desc entry
-	printf "$(gettext ":: creating 'desc' db entry")\n"
-	echo -e "%FILENAME%\n$(basename $1)\n" >>desc
+	msg2 "$(gettext "Creating 'desc' db entry...")"
+	echo -e "%FILENAME%\n$(basename "$1")\n" >>desc
 	echo -e "%NAME%\n$pkgname\n" >>desc
 	echo -e "%VERSION%\n$pkgver\n" >>desc
-	if [ -n "$pkgdesc" ]; then
-		echo -e "%DESC%\n$pkgdesc\n" >>desc
-	fi
-	if [ -n "$_groups" ]; then
-		echo "%GROUPS%" >>desc
-		echo $_groups | tr -s ' ' '\n' >>desc
-		echo "" >>desc
-	fi
+	[ -n "$pkgdesc" ] && echo -e "%DESC%\n$pkgdesc\n" >>desc
+	write_list_entry "GROUPS" "$_groups" "desc"
 	[ -n $csize ] && echo -e "%CSIZE%\n$csize\n" >>desc
 	[ -n $size ] && echo -e "%ISIZE%\n$size\n" >>desc
 
 	# compute checksums
-	for chk in ${DB_CHECKSUMS[@]}; do
-		name="$(checksum_name $chk)"
-		printf "$(gettext ":: computing %s checksums")\n" "$name"
-		if [ -n "$name" ]; then
-			echo -e "%$name%\n$(get_checksum $chk $pkgfile)\n" >>desc
-		fi
-	done
+	msg2 "$(gettext "Computing md5 checksums...")"
+	echo -e "%MD5SUM%\n$(md5sum "$pkgfile" | cut -d ' ' -f 1)\n" >>desc
 
 	[ -n "$url" ] && echo -e "%URL%\n$url\n" >>desc
-	if [ -n "$_licenses" ]; then
-		echo "%LICENSE%" >>desc
-		echo $_licenses | tr -s ' ' '\n' >>desc
-		echo "" >>desc
-	fi
+	write_list_entry "LICENSE" "$_licenses" "desc"
 	[ -n "$arch" ] && echo -e "%ARCH%\n$arch\n" >>desc
 	[ -n "$builddate" ] && echo -e "%BUILDDATE%\n$builddate\n" >>desc
 	[ -n "$packager" ] && echo -e "%PACKAGER%\n$packager\n" >>desc
-
-	if [ -n "$_replaces" ]; then
-		echo "%REPLACES%" >>desc
-		echo $_replaces | tr -s ' ' '\n' >>desc
-		echo "" >>desc
-	fi
-	[ "$FORCE" = "1" ] && echo -e "%FORCE%\n" >>desc
+	write_list_entry "REPLACES" "$_replaces" "desc"
+	[ $FORCE -eq 1 ] && echo -e "%FORCE%\n" >>desc
 
 	# create depends entry
-	echo ":: creating 'depends' db entry"
-	if [ -n "$_depends" ]; then
-		echo "%DEPENDS%" >>depends
-		echo $_depends | tr -s ' ' '\n' >>depends
-		echo "" >>depends
-	fi
-	if [ -n "$_conflicts" ]; then
-		echo "%CONFLICTS%" >>depends
-		echo $_conflicts | tr -s ' ' '\n' >>depends
-		echo "" >>depends
-	fi
-	if [ -n "$_provides" ]; then
-		echo "%PROVIDES%" >>depends
-		echo $_provides | tr -s ' ' '\n' >>depends
-		echo "" >>depends
-	fi
+	msg2 "$(gettext "Creating 'depends' db entry...")"
+	write_list_entry "DEPENDS" "$_depends" "depends"
+	write_list_entry "CONFLICTS" "$_conflicts" "depends"
+	write_list_entry "PROVIDES" "$_provides" "depends"
 
 	# preserve the modification time
 	touch -r "$pkgfile" desc depends
+
+	popd 2>&1 >/dev/null
 } # end db_write_entry
 
 # PROGRAM START
@@ -236,10 +225,10 @@ if [ $# -lt 2 ]; then
 fi
 
 # source system and user makepkg.conf
-if [ -r @sysconfdir@/makepkg.conf ]; then
-	source @sysconfdir@/makepkg.conf
+if [ -r "@sysconfdir@/makepkg.conf" ]; then
+	source "@sysconfdir@/makepkg.conf"
 else
-	printf "$(gettext "ERROR: /etc/makepkg.conf not found. Can not continue.")\n" >&2
+	error "$(gettext "/etc/makepkg.conf not found. Can not continue.")" "@sysconfdir@/makepkg.conf"
 	exit 1 # $E_CONFIG_ERROR
 fi
 
@@ -248,64 +237,65 @@ if [ -r ~/.makepkg.conf ]; then
 fi
 
 # main routine
-if [ $# -gt 1 ]; then
-	gstmpdir=$(mktemp -d /tmp/gensync.XXXXXXXXXX) || (\
-	printf "$(gettext "cannot create temp directory for database building")\n"; \
+gstmpdir=$(mktemp -d /tmp/gensync.XXXXXXXXXX) || (\
+	error "$(gettext "Can not create temp directory for database building.")"; \
 	exit 1)
 
-	success=0
-	# parse arguements
-	for arg in $@; do
-		if [ "$arg" == "--force" -o "$arg" == "-f" ]; then
-			FORCE=1
-		elif [ -z "$REPO_DB_FILE" ]; then
-			REPO_DB_FILE="$(readlink -f $arg)"
-			if ! test_repo_db_file; then
-				printf "$(gettext "error: repository file '%s' is not a proper pacman db")\n" "$REPO_DB_FILE"
-				exit 1
-			elif [ -f "$REPO_DB_FILE" ]; then
-				printf "$(gettext ":: extracting database to a temporary location")\n"
-				bsdtar -xf "$REPO_DB_FILE" -C "$gstmpdir"
-			fi
-		else
-			if [ -f "$arg" ]; then
-				if ! bsdtar -tf "$arg" .PKGINFO 2>&1 >/dev/null; then
-					printf "$(gettext "error: '%s' is not a package file, skipping")\n" "$arg"
-				else
-					printf "$(gettext ":: adding package '%s'")\n" "$arg"
-
-					this_dir="$(pwd)"
-					if db_write_entry "$arg"; then
-						success=1
-					fi
-					cd $this_dir
-				fi
+success=0
+# parse arguements
+for arg in "$@"; do
+	if [ "$arg" == "--force" -o "$arg" == "-f" ]; then
+		FORCE=1
+	elif [ -z "$REPO_DB_FILE" ]; then
+		REPO_DB_FILE=$(readlink -f "$arg")
+		if ! test_repo_db_file; then
+			error "$(gettext "Repository file '%s' is not a proper pacman database.")" "$REPO_DB_FILE"
+			exit 1
+		elif [ -f "$REPO_DB_FILE" ]; then
+			msg "$(gettext "Extracting database to a temporary location...")"
+			bsdtar -xf "$REPO_DB_FILE" -C "$gstmpdir"
+		fi
+	else
+		if [ -f "$arg" ]; then
+			if ! bsdtar -tf "$arg" .PKGINFO 2>&1 >/dev/null; then
+				error "$(gettext "'%s' is not a package file, skipping")" "$arg"
 			else
-				printf "$(gettext "error: package '%s' not found")\n" "$arg"
+				msg "$(gettext "Adding package '%s'")" "$arg"
+
+				if db_write_entry "$arg"; then
+					success=1
+				fi
 			fi
+		else
+			error "$(gettext "Package '%s' not found.")" "$arg"
 		fi
-	done
+	fi
+done
 
-	# if all operations were a success, rezip database
-	if [ "$success" = "1" ]; then
-		printf "$(gettext ":: creating updated database file %s")\n" "$REPO_DB_FILE"
-		cd $gstmpdir
-		if [ -n "$(ls)" ]; then
-			[ -f "${REPO_DB_FILE}.old" ] && rm "${REPO_DB_FILE}.old"
-			[ -f "$REPO_DB_FILE" ] && mv "$REPO_DB_FILE" "${REPO_DB_FILE}.old"
-			case "$DB_COMPRESSION" in
-				gz) bsdtar -c * | gzip -9 >$REPO_DB_FILE ;;
-				bz2) bsdtar -c * | bzip2 -9 >$REPO_DB_FILE ;;
-				*) printf "$(gettext "warning: no compression set")\n"
-				bsdtar -c * >$REPO_DB_FILE;;
-			esac
-		fi
-	else
-		printf "$(gettext ":: no packages modified, nothing to do")\n"
+# if all operations were a success, rezip database
+if [ $success -eq 1 ]; then
+	msg "$(gettext "Creating updated database file %s")" "$REPO_DB_FILE"
+	pushd "$gstmpdir" 2>&1 >/dev/null
+
+	if [ -n "$(ls)" ]; then
+		[ -f "${REPO_DB_FILE}.old" ] && rm "${REPO_DB_FILE}.old"
+		[ -f "$REPO_DB_FILE" ] && mv "$REPO_DB_FILE" "${REPO_DB_FILE}.old"
+
+		case "$DB_COMPRESSION" in
+			gz)  TAR_OPT="z" ;;
+			bz2) TAR_OPT="j" ;;
+			*)   warning "$(gettext "No compression set.")" ;;
+		esac
+
+		bsdtar -c${TAR_OPT}f "$REPO_DB_FILE" *
 	fi
+
+	popd 2>&1 >/dev/null
+else
+	msg "$(gettext "No packages modified, nothing to do.")"
 fi
 
 # remove the temp directory used to unzip
-[ -d "$gstmpdir" ] && rm -rf $gstmpdir
+[ -d "$gstmpdir" ] && rm -rf "$gstmpdir"
 
 # vim: set ts=2 sw=2 noet:
diff --git a/scripts/repo-remove.sh.in b/scripts/repo-remove.sh.in
index 49ff358..ee0c16c 100644
--- a/scripts/repo-remove.sh.in
+++ b/scripts/repo-remove.sh.in
@@ -29,6 +29,42 @@ myver='@PACKAGE_VERSION@'
 FORCE=0
 REPO_DB_FILE=""
 
+msg() {
+	local mesg=$1; shift
+	if [ -t 1 ]; then
+		printf "\033[1;32m==>\033[1;37m ${mesg}\033[0m\n" "$@" >&1
+	else
+		printf "==> ${mesg}\n" "$@" >&1
+	fi
+}
+
+msg2() {
+	local mesg=$1; shift
+	if [ -t 1 ]; then
+		printf "\033[1;34m  ->\033[1;37m ${mesg}\033[0m\n" "$@" >&1
+	else
+		printf "  -> ${mesg}\n" "$@" >&1
+	fi
+}
+
+warning() {
+	local mesg=$1; shift
+	if [ -t 2 ]; then
+		printf "\033[1;33m==> $(gettext "WARNING:")\033[1;37m ${mesg}\033[0m\n" "$@" >&2
+	else
+		printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2
+	fi
+}
+
+error() {
+	local mesg=$1; shift
+	if [ -t 2 ]; then
+		printf "\033[1;31m==> $(gettext "ERROR:")\033[1;37m ${mesg}\033[0m\n" "$@" >&2
+	else
+		printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2
+	fi
+}
+
 # print usage instructions
 usage() {
 	printf "$(gettext "repo-remove %s\n\n")" $myver
@@ -51,24 +87,28 @@ There is NO WARRANTY, to the extent permitted by law.\n")"
 # test if a file is a repository DB
 test_repo_db_file () {
 	if [ -f "$REPO_DB_FILE" ]; then
-		[ "$(bsdtar -tf "$REPO_DB_FILE" | grep -c "/desc")" -gt 0 ] || return 1
-	else
-		true
+		if bsdtar -tf "$REPO_DB_FILE" | grep -q "/desc"; then
+			return 0 # YES
+		fi
 	fi
+
+	return 1 # NO
 }
 
 # remove existing entries from the DB
-db_remove_entry()
-{
-	cd $gstmpdir
+db_remove_entry() {
+	pushd "$gstmpdir" 2>&1 >/dev/null
 
 	# remove any other package in the DB with same name
+	local existing
 	for existing in *; do
 		if [ "${existing%-*-*}" = "$1" ]; then
-			printf "$(gettext ":: removing existing package '%s'")\n" "$existing"
-			rm -rf $existing
+			msg2 "$(gettext "Removing existing package '%s'...")" "$existing"
+			rm -rf "$existing"
 		fi
 	done
+
+	popd &>/dev/null
 } # end db_remove_entry
 
 # PROGRAM START
@@ -92,10 +132,10 @@ if [ $# -lt 2 ]; then
 fi
 
 # source system and user makepkg.conf
-if [ -r @sysconfdir@/makepkg.conf ]; then
-	source @sysconfdir@/makepkg.conf
+if [ -r "@sysconfdir@/makepkg.conf" ]; then
+	source "@sysconfdir@/makepkg.conf"
 else
-	printf "$(gettext "ERROR: /etc/makepkg.conf not found. Can not continue.")\n" >&2
+	error "$(gettext "%s not found. Cannot continue.")" "@sysconfdir@/makepkg.conf"
 	exit 1 # $E_CONFIG_ERROR
 fi
 
@@ -104,53 +144,53 @@ if [ -r ~/.makepkg.conf ]; then
 fi
 
 # main routine
-if [ $# -gt 1 ]; then
-	gstmpdir=$(mktemp -d /tmp/gensync.XXXXXXXXXX) || (\
-	printf "$(gettext "cannot create temp directory for database building")\n"; \
+gstmpdir=$(mktemp -d /tmp/gensync.XXXXXXXXXX) || (\
+	error "$(gettext "Cannot create temp directory for database building.")"; \
 	exit 1)
 
-	success=0
-	# parse arguements
-	for arg in $@; do
-		if [ -z "$REPO_DB_FILE" ]; then
-			REPO_DB_FILE="$(readlink -f $arg)"
-			if ! test_repo_db_file; then
-				printf "$(gettext "error: repository file '%s' is not a proper pacman db")\n" "$REPO_DB_FILE"
-				exit 1
-			elif [ -f "$REPO_DB_FILE" ]; then
-				printf "$(gettext ":: extracting database to a temporary location")\n"
-				bsdtar -xf "$REPO_DB_FILE" -C "$gstmpdir"
-			fi
-		else
-			printf "$(gettext ":: searching for package '%s'")\n"
-
-			this_dir="$(pwd)"
-			if db_remove_entry "$arg"; then
-				success=1
-			else
-				printf "$(gettext "error: package matching '%s' not found")\n" "$arg"
-			fi
-			cd $this_dir
+success=0
+# parse arguements
+for arg in "$@"; do
+	if [ -z "$REPO_DB_FILE" ]; then
+		REPO_DB_FILE=$(readlink -f "$arg")
+		if ! test_repo_db_file; then
+			error "$(gettext "Repository file '%s' is not a proper pacman database.")\n" "$REPO_DB_FILE"
+			exit 1
+		elif [ -f "$REPO_DB_FILE" ]; then
+			msg "$(gettext "Extracting database to a temporary location...")"
+			bsdtar -xf "$REPO_DB_FILE" -C "$gstmpdir"
 		fi
-	done
+	else
+		msg "$(gettext "Searching for package '%s'...")" "$arg"
 
-	# if all operations were a success, rezip database
-	if [ "$success" = "1" ]; then
-		printf "$(gettext ":: creating updated database file %s")\n" "$REPO_DB_FILE"
-		cd $gstmpdir
-		if [ -n "$(ls)" ]; then
-			[ -f "${REPO_DB_FILE}.old" ] && rm "${REPO_DB_FILE}.old"
-			[ -f "$REPO_DB_FILE" ] && mv "$REPO_DB_FILE" "${REPO_DB_FILE}.old"
-			case "$DB_COMPRESSION" in
-				gz) bsdtar -c * | gzip -9 >$REPO_DB_FILE ;;
-				bz2) bsdtar -c * | bzip2 -9 >$REPO_DB_FILE ;;
-				*) printf "$(gettext "warning: no compression set")\n"
-				bsdtar -c * >$REPO_DB_FILE;;
-			esac
+		if db_remove_entry "$arg"; then
+			success=1
+		else
+			error "$(gettext "Package matching '%s' not found.")" "$arg"
 		fi
-	else
-		printf "$(gettext ":: no packages modified, nothing to do")\n"
 	fi
+done
+
+# if all operations were a success, rezip database
+if [ $success -eq 1 ]; then
+	msg "$(gettext "Creating updated database file '%s'...")" "$REPO_DB_FILE"
+	pushd "$gstmpdir" 2>&1 >/dev/null
+
+	if [ -n "$(ls)" ]; then
+		[ -f "${REPO_DB_FILE}.old" ] && rm "${REPO_DB_FILE}.old"
+		[ -f "$REPO_DB_FILE" ] && mv "$REPO_DB_FILE" "${REPO_DB_FILE}.old"
+		case "$DB_COMPRESSION" in
+			gz)  TAR_OPT="z" ;;
+			bz2) TAR_OPT="j" ;;
+			*)   warning "$(gettext "No compression set.")" ;;
+		esac
+
+		bsdtar -c${TAR_OPT}f "$REPO_DB_FILE" *
+	fi
+
+	popd 2>&1 >/dev/null
+else
+	msg "$(gettext "No packages modified, nothing to do.")"
 fi
 
 # remove the temp directory used to unzip
-- 
1.5.2.5





More information about the pacman-dev mailing list