* convert dbpath from argument to option * add --config and --root options * read dbpath and root from config file * if root is set but not dbpath, dbpath is set relative to root Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> --- scripts/Makefile.am | 1 + scripts/pacman-db-upgrade.sh.in | 73 +++++++++++++++++++++++++++++------------ 2 files changed, 53 insertions(+), 21 deletions(-) diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 0b756ad..bc1dc10 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -54,6 +54,7 @@ endif #### Taken from the autoconf scripts Makefile.am #### edit = sed \ + -e 's|@rootdir[@]|$(ROOTDIR)|g' \ -e 's|@localedir[@]|$(localedir)|g' \ -e 's|@sysconfdir[@]|$(sysconfdir)|g' \ -e 's|@localstatedir[@]|$(localstatedir)|g' \ diff --git a/scripts/pacman-db-upgrade.sh.in b/scripts/pacman-db-upgrade.sh.in index b0b0ac8..70562e0 100644 --- a/scripts/pacman-db-upgrade.sh.in +++ b/scripts/pacman-db-upgrade.sh.in @@ -25,19 +25,25 @@ export TEXTDOMAINDIR='@localedir@' declare -r myver='@PACKAGE_VERSION@' -eval $(awk '/DBPath/ {print $1$2$3}' @sysconfdir@/pacman.conf) -dbroot="${DBPath:-@localstatedir@/lib/pacman/}" - -USE_COLOR='y' - m4_include(library/output_format.sh) +m4_include(library/parseopts.sh) + usage() { printf "pacman-db-upgrade (pacman) %s\n" "${myver}" echo printf -- "$(gettext "Upgrade the local pacman database to a newer format")\n" echo - printf -- "$(gettext "Usage: %s [--nocolor] [pacman_db_root]")\n" "$0" + printf -- "$(gettext "Usage: %s [options]")\n" "$0" + echo + printf -- "$(gettext "options:")\n" + printf -- "$(gettext " -b, --dbpath <path> set an alternate database location")\n" + printf -- "$(gettext " -h, --help show this help message and exit")\n" + printf -- "$(gettext " -r, --root <path> set an alternate installation root")\n" + printf -- "$(gettext " -V, --version show version information and exit")\n" + printf -- "$(gettext " --config <path> set an alternate configuration file")\n" + printf -- "$(gettext " --nocolor disable colorized output messages")\n" + echo } version() { @@ -58,6 +64,18 @@ die_r() { die "$@" } +get_opt_from_config() { + local keyname="$1" conffile="$2" + local key value + + while IFS=$'= \t' read -r key value _; do + if [[ $key = $keyname ]]; then + echo "$value" + return + fi + done <"$conffile" +} + # PROGRAM START # determine whether we have gettext; make it a no-op if we do not @@ -67,26 +85,39 @@ if ! type gettext &>/dev/null; then } fi -if [[ $1 = "-h" || $1 = "--help" ]]; then - usage - exit 0 -fi +USE_COLOR='y' -if [[ $1 = "-V" || $1 = "--version" ]]; then - version - exit 0 +OPT_SHORT="d:hr:V" +OPT_LONG=('confg' 'dbpath:' 'help' 'nocolor' 'root:' 'version') +if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then + exit 1 # E_INVALID_OPTION fi - -if [[ $1 = "--nocolor" ]]; then - USE_COLOR='n' +set -- "${OPTRET[@]}" +unset OPT_SHORT OPT_LONG OPTRET + +while true; do + case "$1" in + --config) shift; conffile="$1" ;; + -d|--dbpath) shift; dbroot="$1" ;; + -r|--root) shift; pacroot="$1" ;; + -h|--help) usage; exit 0 ;; + --nocolor) USE_COLOR='n' ;; + -V|--version) version; exit 0 ;; + -- ) shift; break 2 ;; + esac shift -fi +done -m4_include(library/term_colors.sh) +conffile=${conffile:-@sysconfdir@/pacman.conf} +[[ -z $pacroot ]] && pacroot="$(get_opt_from_config "RootDir" "$conffile")" +[[ -z $dbroot ]] && dbroot="$(get_opt_from_config "DBPath" "$conffile")" -if [[ -n $1 ]]; then - dbroot="$1" -fi +[[ -z $dbroot && -n $pacroot ]] && dbroot="$pacroot/@localstatedir@/lib/pacman" + +[[ -z $pacroot ]] && pacroot="@rootdir@" +[[ -z $dbroot ]] && dbroot="@localstatedir@/lib/pacman/" + +m4_include(library/term_colors.sh) if [[ ! -d $dbroot ]]; then die "$(gettext "%s does not exist or is not a directory.")" "$dbroot" -- 2.1.0