From 9b6b6dad291e2d62a957a38ace952d98147a83d2 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 1 Oct 2014 03:16:30 -0400 Subject: conf.sh: don't hardcode /etc, get it during build from $(sysconfdir) --- common.mk | 9 ++- src/lib/.gitignore | 1 + src/lib/Makefile | 2 +- src/lib/conf.sh | 207 ----------------------------------------------------- src/lib/conf.sh.in | 207 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 217 insertions(+), 209 deletions(-) delete mode 100644 src/lib/conf.sh create mode 100644 src/lib/conf.sh.in diff --git a/common.mk b/common.mk index bd0b0d5..e1051de 100644 --- a/common.mk +++ b/common.mk @@ -9,7 +9,10 @@ pkglibexecdir ?= $(libexecdir)/libretools # Usage: OUTPUT # Expand m4_include macros to use librelib # This is used when using sources grabbed from devtools -edit = sed -e 's|^\#!\s*/bin/bash|\#!/usr/bin/env bash|' -e 's|m4_include(lib/\(.*\))|. "$$(librelib \1)"|' +edit = sed \ + -e 's|^\#!\s*/bin/bash|\#!/usr/bin/env bash|' \ + -e 's|m4_include(lib/\(.*\))|. "$$(librelib \1)"|' \ + -e 's|@sysconfdir@|$(sysconfdir)|g' # Usage: $(call indent,FILENAME) # Command to auto-indent a file. @@ -94,6 +97,10 @@ $(sort $(_do_patch)): %: %.ugly Makefile %: %.in @echo 'EDIT < $< > $@'; $(edit) <"$<" >"$@" $(if $(filter-out %.sh,$@),chmod 755 "$@") +# work-around for what I currently believe to be a regression in Make +%.sh: %.sh.in + @echo 'EDIT < $< > $@'; $(edit) <"$<" >"$@" + $(if $(filter-out %.sh,$@),chmod 755 "$@") # Build ######################################################################## diff --git a/src/lib/.gitignore b/src/lib/.gitignore index 9a0c402..e769566 100644 --- a/src/lib/.gitignore +++ b/src/lib/.gitignore @@ -1,3 +1,4 @@ common.sh common.sh.in common.sh.top +conf.sh diff --git a/src/lib/Makefile b/src/lib/Makefile index 5226dda..97b30eb 100644 --- a/src/lib/Makefile +++ b/src/lib/Makefile @@ -2,7 +2,7 @@ copy_files = common.sh.in libexecs = $(filter-out librelib,$(progs)) # include common.sh in libs explicitly, because it might not exist yet # when the wildcard is performed -libs = $(sort $(wildcard *.sh) common.sh) +libs = $(sort $(wildcard *.sh) common.sh conf.sh) include ../../common.mk diff --git a/src/lib/conf.sh b/src/lib/conf.sh deleted file mode 100644 index 3c4234f..0000000 --- a/src/lib/conf.sh +++ /dev/null @@ -1,207 +0,0 @@ -#!/usr/bin/env bash # non-executable, but put this there as a hint to text editors -# This may be included with or without `set -euE` - -# Copyright (C) 2012-2014 Luke Shumaker -# -# License: GNU GPLv2+ -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -LIBREUSER="${SUDO_USER:-$USER}" -if [[ $LIBREUSER == $USER ]]; then - LIBREHOME=$HOME -else - LIBREHOME="$(eval echo ~$LIBREUSER)" -fi -if [[ -z ${XDG_CONFIG_HOME:-} ]]; then - export XDG_CONFIG_HOME="${LIBREHOME}/.config" -fi -if [[ -z ${XDG_CACHE_HOME:-} ]]; then - export XDG_CACHE_HOME="${LIBREHOME}/.cache" -fi - -# Low-level generic functions ################################################## - -# Usage: list_files $slug -# Lists the configuration files to be considered for $slug. -# Later files should take precedence over earlier files. -list_files() { - local slug=$1 - case $slug in - abs) - echo /etc/$slug.conf - echo "$LIBREHOME/.$slug.conf" - ;; - makepkg) - if [[ ${MAKEPKG_CONF:-} != /etc/$slug.conf && -r ${MAKEPKG_CONF:-} ]]; then - echo "$MAKEPKG_CONF" - else - echo /etc/$slug.conf - echo "$LIBREHOME/.$slug.conf" - fi - ;; - xbs*) - echo /etc/xbs/$slug.conf - echo "$XDG_CONFIG_HOME/xbs/$slug.conf" - ;; - libretools) - echo /etc/$slug.conf - echo "$XDG_CONFIG_HOME/libretools/$slug.conf" - ;; - *) - echo /etc/libretools.d/$slug.conf - echo "$XDG_CONFIG_HOME/libretools/$slug.conf" - ;; - esac -} - -# Usage: list_envvars $slug -# Lists the environmental variables that take precedence over the configuration -# files for $slug. -list_envvars() { - local slug=$1 - case $slug in - makepkg) - printf '%s\n' \ - PKGDEST SRCDEST SRCPKGDEST LOGDEST \ - BUILDDIR \ - PKGEXT SRCEXT \ - GPGKEY PACKAGER - ;; - libretools) - printf '%s\n' DIFFPROG - ;; - xbs) - printf '%s\n' BUILDSYSTEM - ;; - *) :;; - esac -} - -# High-level generic functions ################################################# - -# Usage: load_files $slug -# Loads the configuration files for $slug in the proper order. -load_files() { - local slug=$1 - local var - local file - - # Save the existing versions at _VARNAME - for var in $(list_envvars $slug); do - [[ -n ${!var:-} ]] && eval "_$var=\${$var}" - done - - # Load the files - for file in $(list_files $slug); do - if [[ -r $file ]]; then - . "$file" || return 1 - fi - done - - # Restore the _SAVED versions - for var in $(list_envvars $slug); do - eval "$var=\${_$var:-\${$var:-}}" - done -} - -# Usage: check_vars $slug VAR1 VAR2... -# Check whether the variables listed are properly set. -# If not, it prints a message saying to set them in the configuration file(s) -# for $slug. -check_vars() ( - local slug=$1; shift - - local ret=0 - - local VAR - for VAR in "$@"; do - if [[ -z ${!VAR:-} ]]; then - type print &>/dev/null || . libremessages - if [[ $(list_files $slug|wc -l) -gt 1 ]]; then - _l print "Configure '%s' in one of:" "$VAR" - list_files $slug | sed 's/./ -> &/' - else - _l print "Configure '%s' in '%s'" "$VAR" "$(list_files $slug)" - fi - ret=1 - fi - done >&2 - - if [[ $ret != 0 ]]; then - return 1 - fi -) - -# Usage: get_var -# Does not work with arrays -get_var() ( - set +euE - local slug=$1 - local setting=$2 - local default=$3 - load_files "$slug" - printf '%s' "${!setting:-${default}}" -) - -# Usage: set_var -# Does not work with arrays -set_var() { - local slug=$1 - local key=$2 - local val=$3 - local file - for file in $(list_files "$slug"|tac); do - if [[ -w $file ]]; then - sed -i "/^\s*$key=/d" "$file" - printf '%s=%q\n' "$key" "$val" >> "$file" - return 0 - fi - done - return 1 -} - -# PKGBUILD (not configuration, per se) ######################################### - -unset_PKGBUILD() { - # This routine is based primarily off of the PKGBUILD(5) man-page, - # version 4.1.2, dated 2013-06-18. - - # From the "OPTIONS AND DIRECTIVES" section (in order of mention) - unset -v pkgname pkgver - unset -f pkgver - unset -v pkgrel pkgdesc epoch url license install changelog source - unset -v noextract md5sums sha{1,256,384,512}sums groups arch backup - unset -v depends makedepends checkdepends optdepends conflicts provides - unset -v replaces options - - # From the "PACKAGING FUNCTIONS" section (in order of mention) - unset -f package prepare build check - - # From the "PACKAGE SPLITTING" section - unset -f $(declare -f|sed -n 's/^\(package_\S*\) ()\s*$/\1/p') - unset -v pkgbase - - # These are used by the `librefetch` program - unset -v mksource mknoextract mkmd5sums mksha{1,256,384,512}sums - unset -v mkdepends - unset -f mksource -} - -load_PKGBUILD() { - local file=${1:-./PKGBUILD} - unset_PKGBUILD - CARCH="$(get_var makepkg CARCH "`uname -m`")" - . "$file" -} diff --git a/src/lib/conf.sh.in b/src/lib/conf.sh.in new file mode 100644 index 0000000..fa66016 --- /dev/null +++ b/src/lib/conf.sh.in @@ -0,0 +1,207 @@ +#!/usr/bin/env bash # non-executable, but put this there as a hint to text editors +# This may be included with or without `set -euE` + +# Copyright (C) 2012-2014 Luke Shumaker +# +# License: GNU GPLv2+ +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +LIBREUSER="${SUDO_USER:-$USER}" +if [[ $LIBREUSER == $USER ]]; then + LIBREHOME=$HOME +else + LIBREHOME="$(eval echo ~$LIBREUSER)" +fi +if [[ -z ${XDG_CONFIG_HOME:-} ]]; then + export XDG_CONFIG_HOME="${LIBREHOME}/.config" +fi +if [[ -z ${XDG_CACHE_HOME:-} ]]; then + export XDG_CACHE_HOME="${LIBREHOME}/.cache" +fi + +# Low-level generic functions ################################################## + +# Usage: list_files $slug +# Lists the configuration files to be considered for $slug. +# Later files should take precedence over earlier files. +list_files() { + local slug=$1 + case $slug in + abs) + echo @sysconfdir@/$slug.conf + echo "$LIBREHOME/.$slug.conf" + ;; + makepkg) + if [[ ${MAKEPKG_CONF:-} != @sysconfdir@/$slug.conf && -r ${MAKEPKG_CONF:-} ]]; then + echo "$MAKEPKG_CONF" + else + echo @sysconfdir@/$slug.conf + echo "$LIBREHOME/.$slug.conf" + fi + ;; + xbs*) + echo @sysconfdir@/xbs/$slug.conf + echo "$XDG_CONFIG_HOME/xbs/$slug.conf" + ;; + libretools) + echo @sysconfdir@/$slug.conf + echo "$XDG_CONFIG_HOME/libretools/$slug.conf" + ;; + *) + echo @sysconfdir@/libretools.d/$slug.conf + echo "$XDG_CONFIG_HOME/libretools/$slug.conf" + ;; + esac +} + +# Usage: list_envvars $slug +# Lists the environmental variables that take precedence over the configuration +# files for $slug. +list_envvars() { + local slug=$1 + case $slug in + makepkg) + printf '%s\n' \ + PKGDEST SRCDEST SRCPKGDEST LOGDEST \ + BUILDDIR \ + PKGEXT SRCEXT \ + GPGKEY PACKAGER + ;; + libretools) + printf '%s\n' DIFFPROG + ;; + xbs) + printf '%s\n' BUILDSYSTEM + ;; + *) :;; + esac +} + +# High-level generic functions ################################################# + +# Usage: load_files $slug +# Loads the configuration files for $slug in the proper order. +load_files() { + local slug=$1 + local var + local file + + # Save the existing versions at _VARNAME + for var in $(list_envvars $slug); do + [[ -n ${!var:-} ]] && eval "_$var=\${$var}" + done + + # Load the files + for file in $(list_files $slug); do + if [[ -r $file ]]; then + . "$file" || return 1 + fi + done + + # Restore the _SAVED versions + for var in $(list_envvars $slug); do + eval "$var=\${_$var:-\${$var:-}}" + done +} + +# Usage: check_vars $slug VAR1 VAR2... +# Check whether the variables listed are properly set. +# If not, it prints a message saying to set them in the configuration file(s) +# for $slug. +check_vars() ( + local slug=$1; shift + + local ret=0 + + local VAR + for VAR in "$@"; do + if [[ -z ${!VAR:-} ]]; then + type print &>/dev/null || . libremessages + if [[ $(list_files $slug|wc -l) -gt 1 ]]; then + _l print "Configure '%s' in one of:" "$VAR" + list_files $slug | sed 's/./ -> &/' + else + _l print "Configure '%s' in '%s'" "$VAR" "$(list_files $slug)" + fi + ret=1 + fi + done >&2 + + if [[ $ret != 0 ]]; then + return 1 + fi +) + +# Usage: get_var +# Does not work with arrays +get_var() ( + set +euE + local slug=$1 + local setting=$2 + local default=$3 + load_files "$slug" + printf '%s' "${!setting:-${default}}" +) + +# Usage: set_var +# Does not work with arrays +set_var() { + local slug=$1 + local key=$2 + local val=$3 + local file + for file in $(list_files "$slug"|tac); do + if [[ -w $file ]]; then + sed -i "/^\s*$key=/d" "$file" + printf '%s=%q\n' "$key" "$val" >> "$file" + return 0 + fi + done + return 1 +} + +# PKGBUILD (not configuration, per se) ######################################### + +unset_PKGBUILD() { + # This routine is based primarily off of the PKGBUILD(5) man-page, + # version 4.1.2, dated 2013-06-18. + + # From the "OPTIONS AND DIRECTIVES" section (in order of mention) + unset -v pkgname pkgver + unset -f pkgver + unset -v pkgrel pkgdesc epoch url license install changelog source + unset -v noextract md5sums sha{1,256,384,512}sums groups arch backup + unset -v depends makedepends checkdepends optdepends conflicts provides + unset -v replaces options + + # From the "PACKAGING FUNCTIONS" section (in order of mention) + unset -f package prepare build check + + # From the "PACKAGE SPLITTING" section + unset -f $(declare -f|sed -n 's/^\(package_\S*\) ()\s*$/\1/p') + unset -v pkgbase + + # These are used by the `librefetch` program + unset -v mksource mknoextract mkmd5sums mksha{1,256,384,512}sums + unset -v mkdepends + unset -f mksource +} + +load_PKGBUILD() { + local file=${1:-./PKGBUILD} + unset_PKGBUILD + CARCH="$(get_var makepkg CARCH "`uname -m`")" + . "$file" +} -- cgit v1.2.2