autospec/plugins/config-getvar.in
2012-11-14 22:42:08 +01:00

234 lines
6.6 KiB
Bash

#!/bin/bash
# config-getvar -- plugin for @package@
# Copyright (C) 2007,2008,2010-2012 Davide Madrisan <davide.madrisan@gmail.com>
[ -z "$BASH" ] || [ ${BASH_VERSION:0:1} -lt 3 ] &&
{ echo $"this script requires bash version 3 or better" >&2 && exit 1; }
me=(${0##*/} "@version@" "@date@")
[ -r @libdir@/libmsgmng.lib ] ||
{ echo "$me: "$"library not found"": @libdir@/libmsgmng.lib" 1>&2
exit 1; }
. @libdir@/libmsgmng.lib
# default values:
# - output verbosity
let "verbose = 1"
# load the configuration file(s)
[ -r @libdir@/libcfg.lib ] ||
{ echo "$me: "$"library not found"": @libdir@/libcfg.lib" 1>&2
exit 1; }
. @libdir@/libcfg.lib
if [[ -z "$LANG" && -r /etc/sysconfig/i18n ]]; then
. /etc/sysconfig/i18n
[ "$LANG" ] && export LANG
fi
TEXTDOMAIN="${me[0]}"; export TEXTDOMAIN
function copying() {
echo "\
"$"This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License version 2 as published by the
Free Software Foundation. There is NO warranty; not even for MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE."
}
function version() {
echo "\
${me[0]} ${me[1]}
Copyright (C) 2004-2008,2010,2011 Davide Madrisan <davide.madrisan@gmail.com>"
}
# $1: optional exit code (default is '1')
function usage() {
version
echo "\
"$"Print the value of a given configuration variable"".
"$"Usage"":
@frontend@ [-C <conf_file>] --eval=<var>
"$"where the above options mean"":
--eval "$"Print the value of the configuration variable <var>""
--colors "$"Select the theme to be used for the colorized output""
-C, --config "$"Use an alternate user configuration file"" <conf_file>""
"$"Default files:"" $default_cfg_strlist""
"$"Default user files:"" $default_cfg_user_strlist""
"$"Operation modes"":
-h, --help "$"Print this help, then exit""
-V, --version "$"Print version number, then exit""
-q, --quiet "$"Run in quiet mode""
-D, --debug "$"Run in debugging mode (very verbose output)""
"$"Samples"":
@frontend@ -q -C ${default_cfg_list[0]} --eval=\"logging_dir\"
"$"Report bugs to <davide.madrisan@gmail.com>."
exit ${1:-1}
}
case $1 in
--autospec-args-file*)
if [[ "$1" =~ = ]]; then
argsfile=`echo $1 | sed 's/^--autospec-args-file=//'`
else
argsfile=$2
fi
[ -r "$argsfile" ] || notify.error $"cannot read"": \`$argsfile'"
. $argsfile && rm -f $argsfile
;;
esac
for arg in $@; do
case $arg in
-h|--help) usage 0 ;;
-V|--version) version; echo; copying; exit 0 ;;
esac
done
exec_options=`LC_ALL=C getopt \
-o C:DqrhV \
--long eval:,config:,\
debug,quiet,colors:,help,version,\
frontend_opts: \
-n "$me" -- "$@"`
[ $? = 0 ] || exit 1
notify.debug "[ ${0} ${exec_options} ]\n"
eval set -- "$exec_options"
while :; do
case $1 in
--eval)
autospecvar="$2"; shift ;;
--colors)
color_scheme="$2"; shift ;;
-C|--config)
cfg_file=$2; shift ;;
-D|--debug)
let "verbose = 2" ;;
-q|--quiet)
let "verbose = 0" ;;
-h|--help)
usage 0 ;;
-V|--version)
version; echo; copying; exit 0 ;;
--) shift; break ;;
*) notify.error $"unrecognized option"" -- \`$1'" ;;
esac
shift
done
for arg in $@; do
[[ -z "$s_rpm_pck" ]] && s_rpm_pck=$arg ||
notify.error $"unrecognized option"" -- \`$arg'"
done
notify.enable_colors "$color_scheme"
# config.getvar()
# Parameters:
# $1 : the configuration file where to look at
# $2 : the configuration variable to expand
# Description:
# Expand the configuration variable `$2' looking in the file `$1'
function config.getvar() {
notify.debug "[ ${0}${exec_options} ]\n"
local cfg_file="$1"
local autospecvar="$2"
local cfg_file_lst
if [ "$cfg_file" ]; then
[ -r $cfg_file ] ||
notify.error $"configuration file not found"" -- \`$cfg_file'"
cfg_file_lst="$cfg_file"
else
cfg_file_lst="${default_cfg_list[*]}"
fi
notify.debug "cfg_file_lst = \"$cfg_file_lst\""
unset value autospecvar_val autospecvar_file autospecvar_sed
autospecvar_sed=$(\
echo "$autospecvar" | sed 's,\[,\\[,g;s,\],\\],g')
for f in $cfg_file_lst; do
[ -r $f ] || continue
confvar_1stline=$(\
sed -n "/^[ \t]*$autospecvar_sed[[]*[0-9]*[]]*=/p" $f)
if [[ "$confvar_1stline" ]]; then
autospecvar_file="$f"
notify.debug "variable \`$autospecvar' found in \`$f'"
notify.debug "confvar_1stline = \`$confvar_1stline'"
[ "$verbose" = 0 ] || notify.note "#[$autospecvar_file]"
confvar_1stline=$(\
sed -n "/^[ \t]*$autospecvar_sed[[]*[0-9]*[]]*=/p" $autospecvar_file)
notify.debug "confvar_1stline = \`$confvar_1stline'"
case "$confvar_1stline" in
*[\[0-9*\]]=*)
# an entry like: ftp_rw_srpms_dir[0]="/devel/SRPMS"
case "$autospecvar" in
*[\[0-9*\]])
sed -n "/^$autospecvar_sed=/{s|.*|&;|p}" \
$autospecvar_file |
while read line; do notify.note "$line"; done
;;
*) sed -n "/^$autospecvar_sed\[[0-9*]\]=/{s|.*|&;|p}" \
$autospecvar_file |
while read line; do notify.note "$line"; done
;;
esac
;;
*=\"*\"|*=%*|*=[0-9a-zA-Z]*|*=\$*|*=\(*\))
# entries like:
# - rpm_macro_make="%make"
# - rpm_macro_make=%make
# - ftp_rw_rpms_dir[0]="/devel/RPMS"
# - arch_list="i586 ppc"
notify.note "${confvar_1stline};"
;;
*=\(*)
# a variable like:
# rpm_approved_licenses=(\
# ... <list of entries, one per line>
sed -e '/./{H;$!d;}' -e "x;/$autospecvar_sed=(/!d;" \
$autospecvar_file |
while read line; do
case "$line" in
\#*|"") ;;
*\)) notify.note "${line};" ;;
*) notify.note "$line" ;;
esac
done
;;
*=\"\\)
# a variable like:
# format_extra_rules="\
sed -e '/./{H;$!d;}' -e "x;/$autospecvar_sed=\"/!d;" \
$autospecvar_file |
while read line; do
case "$line" in
\#*|"") ;;
*\") notify.note "${line};" ;;
*) notify.note "$line" ;;
esac
done
;;
*) notify.error $"(bug)"" -- $FUNCNAME: "$"unknown variable type"
;;
esac
fi
done
}
config.getvar "$cfg_file" "$autospecvar"