* Add option for printing fewer status updates
* Add option for controlling the output directory
* Add option for specyfying the the maximum number of jobs
* Adjust output to the number of jobs
* Rewrite usage page
* Alter version information

Signed-off-by: Gordian Edenhofer <[email protected]>
---
 contrib/bacman.sh.in | 154 +++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 113 insertions(+), 41 deletions(-)

diff --git a/contrib/bacman.sh.in b/contrib/bacman.sh.in
index 4cd78e4..1df84db 100755
--- a/contrib/bacman.sh.in
+++ b/contrib/bacman.sh.in
@@ -28,6 +28,7 @@ declare -r myname='bacman'
 declare -r myver='@PACKAGE_VERSION@'
 USE_COLOR='y'
 INCLUDE_PACNEW='n'
+QUIET='n'
 # Required for fakeroot because options are shifted off the array.
 ARGS=("$@")
 
@@ -40,57 +41,111 @@ clean_up() {
        echo
        exit
 }
-
 # Trap termination signals
 trap clean_up SIGHUP SIGINT SIGTERM
 
 # Print usage information
 usage() {
-       echo "${myname} (pacman) v${myver}"
-       echo
-       echo "Recreate a package using pacman's database and system files"
-       echo
-       echo "Usage: ${myname} [--nocolor] [--pacnew] <installed package name>"
-       echo
-       echo "Example: ${myname} linux-headers"
+       cat <<-EOF
+       $myname (pacman) v$myver
+       This script was design to reassemble installed packages from its 
deliverd files.
+       It comes in handy if you have no access to an up-to-date package cache.
+
+       Usage: $myname [-v] [-p] [-n] [-j <jobs>] [-o <dir>] <package(s)>
+           -h, --help              Display this help message and exit
+           -q, --quiet             Silence most of the status reporting
+           -n, --nocolor           Disable colored output
+           -p, --pacnew            Package .pacnew files
+           -j, --jobs <jobs>       Build in parallel - you may want to set 
XZ_OPT
+           -o, --out  <dir>        Write output to <dir>
+
+       Examples:   # $myname linux-headers
+                   # $myname gzip munge binutils -o ~/Downloads
+                   # $myname -pno /tmp -j 5 gzip munge binutils
+                   # $myname \$(pacman -Qsq)
+       EOF
 }
 
 # Print version information
 version() {
-       printf "%s %s\n" "$myname" "$myver"
-       echo 'Copyright (C) 2008 locci <carlocci_at_gmail_dot_com>'
-       echo 'Copyright (C) 2008-2016 Pacman Development Team 
<[email protected]>'
-}
+       cat <<-EOF
+       $myname (pacman) v$myver
 
-# Check for specified arguments
-while [[ ! -z $1 ]]; do
-       if [[ $1 == "--nocolor" ]]; then
-               USE_COLOR='n'
-               shift
-       elif [[ $1 == "--pacnew" ]]; then
-               INCLUDE_PACNEW='y'
-               shift
-       else
-               break
-       fi
-done
+       Copyright (C) 2008 locci <carlocci_at_gmail_dot_com>
+       Copyright (C) 2016 Gordian Edenhofer <[email protected]>
+       Copyright (C) 2008-2016 Pacman Development Team 
<[email protected]>
+       EOF
+}
 
 # Configure colored output
 m4_include(../scripts/library/term_colors.sh)
 
 # Break if no argument was given
-if (( $# < 1 )); then
+if (( $# == 0 )); then
        usage
        exit 1
 fi
 
-# Print usage or version if requested
-if [[ $1 = -@(h|-help) ]]; then
+# Printing the usage information takes precedence over every other parameter
+for option in "$@"; do
+       [[ $option == "-h" || $option == "--help" ]] && usage && exit 0
+done
+
+# Parse arguments and create a list of packages to be assembled
+params=$(getopt -o o:j:qnpv --long out:,jobs:,quiets,nocolor,pacnew,version -n 
"$myname" -- "$@")
+[[ $? -ne 0 ]] && echo "Try '$myname --help' for more information." && exit 1
+eval set -- "$params"
+while true; do
+       case "$1" in
+               -o|--out)
+                       pkg_dest="$(readlink -e $2)"
+                       [[ $? -ne 0 ]] && echo -e "The directory 
\e[39;1m$2\e[0m does not exist!" && exit 3
+                       shift
+                       ;;
+
+               -j|--jobs)
+                       if [[ $2 =~ ^-?[0-9]+$ ]]; then
+                               MAX_JOBS=$2
+                       else
+                               echo -e "\e[39;1m$2\e[0m is not a valid 
integer!"
+                               exit -1
+                       fi
+                       shift
+                       ;;
+
+               -q|--quiet)
+                       QUIET='y'
+                       ;;
+
+               -n|--nocolor)
+                       USE_COLOR='n'
+                       ;;
+
+               -p|--pacnew)
+                       INCLUDE_PACNEW='y'
+                       ;;
+
+               -v|--version)
+                       version
+                       exit 0
+                       ;;
+
+               --)
+                       shift
+                       break
+                       ;;
+
+               *)
+                       usage
+                       exit 1
+                       ;;
+       esac
+       shift
+done
+pkg_list=($*)
+if [[ ${#pkg_list[@]} == 0 ]]; then
        usage
-       exit 0
-elif [[ $1 = -@(V|-version) ]]; then
-       version
-       exit 0
+       exit 1
 fi
 
 # Run in fakeroot if EUID is not root
@@ -106,7 +161,7 @@ if (( EUID )); then
        fi
 fi
 
-# Source environmental variables and specify fallbacks
+# Read in environmental variables
 if [[ ! -r @sysconfdir@/pacman.conf ]]; then
        error "unable to read @sysconfdir@/pacman.conf"
        exit 1
@@ -121,8 +176,9 @@ source "@sysconfdir@/makepkg.conf"
 if [[ -r ~/.makepkg.conf ]]; then
        source ~/.makepkg.conf
 fi
-pkg_dest="${PKGDEST:-$PWD}"
-pkg_pkger=${PACKAGER:-'Unknown Packager'}
+PKGDEST="${PKGDEST:-$PWD}"
+pkg_dest="${pkg_dest:-$PKGDEST}"
+pkg_pkger="${PACKAGER:-'Unknown Packager'}"
 
 # Check for an existing database
 if [[ ! -d $pac_db ]]; then
@@ -155,7 +211,12 @@ fakebuild() {
        cd "$work_dir" || exit 1
 
        # Assemble list of files which belong to the package and tar them
-       msg2 "Copying package files..."
+       if [[ $MAX_JOBS -gt 1 && "$QUIET" != "y" ]]; then
+               msg2 "${pkg_name}: Copying package files..."
+       elif [[ "$QUIET" != "y" ]]; then
+               msg2 "Copying package files..."
+       fi
+
        while read i; do
                if [[ -z $i ]]; then
                        continue
@@ -229,7 +290,11 @@ fakebuild() {
 
        # Reconstruct .PKGINFO from database
        # TODO adopt makepkg's write_pkginfo() into this or scripts/library
-       msg2 "Generating .PKGINFO metadata..."
+       if [[ $MAX_JOBS -gt 1 && "$QUIET" != "y" ]]; then
+               msg2 "${pkg_name}: Generating .PKGINFO metadata..."
+       elif [[ "$QUIET" != "y" ]]; then
+               msg2 "Generating .PKGINFO metadata..."
+       fi
        echo "# Generated by $myname $myver"    > .PKGINFO
        if [[ $INFAKEROOT == "1" ]]; then
                echo "# Using $(fakeroot -v)"    >> .PKGINFO
@@ -317,11 +382,16 @@ fakebuild() {
        chmod 644 "$work_dir"/{.PKGINFO,.CHANGELOG,.INSTALL} 2> /dev/null
 
        # Generate the package
-       msg2 "Generating the package..."
+       if [[ $MAX_JOBS -gt 1 && "$QUIET" != "y" ]]; then
+               msg2 "${pkg_name}: Generating the package..."
+       elif [[ "$QUIET" != "y" ]]; then
+               msg2 "Generating the package..."
+       fi
 
        pkg_file="$pkg_dest/$pkg_namver-$pkg_arch${PKGEXT}"
        ret=0
 
+       # Move compressed package to destination
        # TODO: Maybe this can be set globally for robustness
        shopt -s -o pipefail
        bsdtar -cf - $comp_files * |
@@ -335,7 +405,7 @@ fakebuild() {
                "$PKGEXT"; cat ;;
        esac > "${pkg_file}"; ret=$?
 
-       # Move compressed package to destination
+       # Evaluate return code
        if (( ret )); then
                error "Unable to write package to $pkg_dest"
                plain "       Maybe the disk is full or you do not have write 
access"
@@ -345,7 +415,6 @@ fakebuild() {
 
        # Clean up working directory
        rm -rf "$work_dir"
-       msg "Done."
 }
 
 # Run fakebuild in parralel with at maximum $MAX_JOBS jobs
@@ -364,11 +433,14 @@ parallelize() {
 
 # Initiate assembly function
 if [[ $MAX_JOBS -gt "1" ]]; then
-       parallelize "$@"
+       parallelize "${pkg_list[@]}"
 else
-       for PKG in $@; do fakebuild $PKG; done
+       for PKG in ${pkg_list[@]}; do
+               fakebuild $PKG
+       done
 fi
 
+msg "Done."
 exit 0
 
 # vim: set noet:
-- 
2.9.2

Reply via email to