mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-pkg
synced 2026-01-15 12:03:31 +01:00
394 lines
6.7 KiB
Bash
394 lines
6.7 KiB
Bash
export PATH=$PATH:/opt/jwbuild/bin
|
|
|
|
# -- private stuff not intended for use outside of this script
|
|
_jwbuild_check_config_present()
|
|
{
|
|
if [ -z "$jwbuild_config_files" ]; then
|
|
if [ "$1" = true ]; then
|
|
jwbuild_log "warning: $0 tries to access configuration witout having a config file"
|
|
fi
|
|
return 1;
|
|
fi
|
|
return 0;
|
|
}
|
|
|
|
|
|
_jwbuild_check_config_files_readable()
|
|
{
|
|
local new=""
|
|
for f in $jwbuild_config_files; do
|
|
[ -f "$f" -a -r "$f" ] && new="$new $f"
|
|
done
|
|
jwbuild_config_files="$new"
|
|
}
|
|
|
|
_jwbuild_probe_config_paths()
|
|
{
|
|
local f new exp dir
|
|
[ ! "$jwbuild_config_files" ] && jwbuild_config_files="
|
|
/etc/opt/$jwbuild_project/$jwbuild_basename.conf
|
|
$HOME/.$jwbuild_project/$jwbuild_basename.conf
|
|
$HOME/.$jwbuild_project/$rc
|
|
$HOME/.$jwbuild_basename/$jwbuild_basename.conf
|
|
$HOME/.$jwbuild_basename/$rc
|
|
$HOME/.$rc
|
|
"
|
|
_jwbuild_check_config_files_readable
|
|
|
|
# add includes
|
|
new=""
|
|
for f in $jwbuild_config_files; do
|
|
|
|
exp=`sed '
|
|
/^[ ]*include[ ]/ !d
|
|
s/^[ ]*include[ ]*//g
|
|
' $f`
|
|
|
|
if [ "$exp" ]; then
|
|
if [ "${exp:0:1}" != '/' ]; then
|
|
dir=`dirname $f`
|
|
exp=$dir/$exp
|
|
fi
|
|
exp=`ls -d $exp 2>/dev/null`
|
|
new="$new $f $exp"
|
|
fi
|
|
done
|
|
|
|
[ "$new" ] && jwbuild_config_files="$new"
|
|
|
|
_jwbuild_check_config_files_readable
|
|
}
|
|
|
|
_jwbuild_cat_section()
|
|
{
|
|
if [ "$2" ]; then
|
|
sed -n "/\[$2\]/,/[^ ]*\[/ p" $1 | sed '/[^ ]*\[/ d; /^[ ]*include[ ]/ d'
|
|
return
|
|
fi
|
|
|
|
sed -n '0,/[^ ]*\[/ p' $1 | sed '/[^ ]*\[/ d; /^[ ]*include[ ]/ d'
|
|
}
|
|
|
|
_jwbuild_format_config()
|
|
{
|
|
sed '/^ *$/d; s/$/;/g' | sed 's/ *= */=/'
|
|
}
|
|
|
|
_jwbuild_format_section()
|
|
{
|
|
_jwbuild_cat_section $* | _jwbuild_format_config
|
|
}
|
|
|
|
_jwbuild_source_section()
|
|
{
|
|
eval `_jwbuild_cat_section $* | _jwbuild_format_config`
|
|
}
|
|
|
|
_uniq()
|
|
{
|
|
sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'
|
|
}
|
|
|
|
# -- exported utilities
|
|
jwbuild_log_err()
|
|
{
|
|
echo "# $@" >&2
|
|
}
|
|
|
|
jwbuild_log_stdout()
|
|
{
|
|
echo "# $@"
|
|
}
|
|
|
|
jwbuild_log_stderr()
|
|
{
|
|
echo "# $@" >&2
|
|
}
|
|
|
|
jwbuild_log_syslog()
|
|
{
|
|
logger -t $jwbuild_basename "$*"
|
|
}
|
|
|
|
jwbuild_log()
|
|
{
|
|
local logger
|
|
for logger in $jwbuild_loggers; do
|
|
eval jwbuild_log_$logger "$@"
|
|
done
|
|
}
|
|
|
|
jwbuild_empty_config()
|
|
{
|
|
local o_verbose=""
|
|
local o_section
|
|
|
|
set -- `getopt 'vs:' $*`
|
|
|
|
while [ "$1" != -- ]; do
|
|
case $1 in
|
|
-v)
|
|
o_verbose=true;;
|
|
-s)
|
|
o_section=$2; shift;;
|
|
*)
|
|
jwbuild_log_err unknown option $1
|
|
exit 1;;
|
|
esac
|
|
shift
|
|
done
|
|
shift
|
|
|
|
_jwbuild_check_config_present $o_verbose || return 1
|
|
|
|
[ ! -r "$1" ] && return
|
|
[ "$o_verbose" ] && jwbuild_log o resetting config "\"$1\""
|
|
if [ "$o_section" ]; then
|
|
[ "$o_verbose" ] && jwbuild_log o resetting section "[$o_section]" of config "\"$1\""
|
|
fi
|
|
|
|
eval `_jwbuild_format_section $1 $o_section | sed 's/=[^;]*;/=;/g'`
|
|
}
|
|
|
|
jwbuild_empty_configs()
|
|
{
|
|
local section
|
|
local c
|
|
local o_verbose=""
|
|
local o_section
|
|
local opts=""
|
|
|
|
set -- `getopt 'vs:' $*`
|
|
while [ "$1" != -- ]; do
|
|
case $1 in
|
|
-v)
|
|
o_verbose=true
|
|
opts="$opts -v"
|
|
;;
|
|
-s)
|
|
o_section="-s $2"; shift;;
|
|
*)
|
|
jwbuild_log_err unknown option $1
|
|
exit 1;;
|
|
esac
|
|
shift
|
|
done
|
|
shift
|
|
|
|
_jwbuild_check_config_present $o_verbose || return 1
|
|
|
|
for section in "" `jwbuild_config_sections`; do
|
|
|
|
for c in $jwbuild_config_files; do
|
|
|
|
jwbuild_empty_config -s "$section" section $opts $c
|
|
|
|
if [ -d $c.d ]; then
|
|
|
|
local dirconfs=`find $c.d -maxdepth 1 -type f`
|
|
local f
|
|
for f in $dirconfs; do
|
|
jwbuild_empty_config -s "$section" $opts $f
|
|
done
|
|
fi
|
|
done
|
|
done
|
|
|
|
return 0
|
|
}
|
|
|
|
jwbuild_source_config()
|
|
{
|
|
local o_verbose=""
|
|
local o_section
|
|
|
|
set -- `getopt 'vs:' $*`
|
|
|
|
while [ "$1" != -- ]; do
|
|
case $1 in
|
|
-v)
|
|
o_verbose=true
|
|
;;
|
|
-s)
|
|
o_section=$2; shift;;
|
|
*)
|
|
jwbuild_log_err unknown option $1
|
|
exit 1;;
|
|
esac
|
|
shift
|
|
done
|
|
shift
|
|
|
|
_jwbuild_check_config_present $o_verbose || return 1
|
|
|
|
if [ "$o_verbose" = true ]; then
|
|
if [ "$o_section" ]; then
|
|
jwbuild_log o sourcing section "[$o_section]" from config file "\"$1\""
|
|
else
|
|
jwbuild_log o sourcing config "\"$1\""
|
|
fi
|
|
|
|
_jwbuild_cat_section $1 $o_section
|
|
fi
|
|
|
|
_jwbuild_source_section $1 $o_section
|
|
}
|
|
|
|
jwbuild_source_configs()
|
|
{
|
|
local c
|
|
local source_opts=""
|
|
local o_verbose=""
|
|
|
|
set -- `getopt 'vs:' $*`
|
|
|
|
while [ "$1" != -- ]; do
|
|
case $1 in
|
|
-v)
|
|
o_verbose=true
|
|
source_opts="$source_opts -v"
|
|
;;
|
|
-s)
|
|
source_opts="$source_opts -s $2"
|
|
shift;;
|
|
*)
|
|
jwbuild_log_err unknown option $1
|
|
exit 1;;
|
|
esac
|
|
shift
|
|
done
|
|
shift
|
|
|
|
_jwbuild_check_config_present $o_verbose || return 1
|
|
|
|
for c in $jwbuild_config_files; do
|
|
jwbuild_source_config $source_opts $c
|
|
if [ -d $c.d ]; then
|
|
local dirconfs=`find $c.d -maxdepth 1 -type f`
|
|
local f
|
|
for f in $dirconfs; do
|
|
jwbuild_source_config $source_opts $f
|
|
done
|
|
fi
|
|
done
|
|
}
|
|
|
|
jwbuild_config_sections()
|
|
{
|
|
local o_verbose=""
|
|
local o_section
|
|
local o_file
|
|
local o_exact=false
|
|
|
|
OPTIND=1
|
|
while getopts ves:f: flag; do
|
|
|
|
case $flag in
|
|
v)
|
|
o_verbose=true;;
|
|
s)
|
|
o_section="$OPTARG";;
|
|
f)
|
|
o_file="$OPTARG";;
|
|
e)
|
|
o_exact=true;;
|
|
*)
|
|
jwbuild_log_err "unknown option -$flag"
|
|
exit 1;;
|
|
esac
|
|
|
|
done
|
|
|
|
shift $(($OPTIND - 1))
|
|
|
|
_jwbuild_check_config_present $o_verbose || return 1
|
|
|
|
[ "$o_file" ] || o_file="$jwbuild_config_files"
|
|
|
|
if [ "$o_exact" = false ]; then
|
|
|
|
if [ "$o_section" ]; then
|
|
sed "
|
|
/^[ ]*\[$o_section\.\(.*\)\]/ !d
|
|
s/^[ ]*\[$o_section\.\(.*\)\].*/\1/
|
|
" $o_file 2>/dev/null | _uniq
|
|
else
|
|
sed "
|
|
/^[ ]*\[\(.*\)\]/ !d
|
|
s/^[ ]*\[\(.*\)\].*/\1/
|
|
" $o_file 2>/dev/null | _uniq
|
|
fi
|
|
|
|
else
|
|
|
|
if [ "$o_section" ]; then
|
|
sed "
|
|
/^[ ]*\[$o_section\.\([^.]*\)\]/ !d
|
|
s/^[ ]*\[$o_section\.\([^.]*\)\].*/\1/
|
|
" $o_file 2>/dev/null | _uniq
|
|
else
|
|
sed "
|
|
/^[ ]*\[\([^.]*\)\]/ !d
|
|
s/^[ ]*\[\([^.]*\)\].*/\1/
|
|
" $o_file 2>/dev/null | _uniq
|
|
fi
|
|
fi
|
|
|
|
return
|
|
}
|
|
|
|
jwbuild_cat()
|
|
{
|
|
cat | sed 's/^[ ]*|//'
|
|
}
|
|
|
|
jwbuild_waitpid()
|
|
{
|
|
local pid=$1
|
|
local t=5
|
|
[ "$2" ] && t=$2
|
|
|
|
while ((t > 0)); do
|
|
[ -f /proc/$pid/status ] || {
|
|
wait $pid
|
|
return 0
|
|
}
|
|
sleep 1
|
|
((t -= 1))
|
|
done
|
|
return 1
|
|
}
|
|
|
|
jwbuild_terminate()
|
|
{
|
|
local pid
|
|
for pid in $@; do
|
|
[ -f /proc/$pid/status ] && {
|
|
echo terminating process $pid
|
|
kill $pid
|
|
jwbuild_waitpid $pid || {
|
|
|
|
echo "failed to normally terminate process $pid, killing it" >&2
|
|
kill -9 $pid
|
|
|
|
jwbuild_waitpid $pid || {
|
|
echo "failed to kill process $pid" >&2
|
|
return 1
|
|
}
|
|
}
|
|
}
|
|
done
|
|
return 0
|
|
}
|
|
|
|
# -- exported variables
|
|
if [ "$jwbuild_functions_sourced" != "yes" ]; then
|
|
|
|
jwbuild_functions_sourced="yes"
|
|
|
|
[ -z "$jwbuild_basename" ] && jwbuild_basename=`echo "$0" | sed 's/^-*//' | xargs basename | sed 's/\.sh$//'`
|
|
[ -z "$jwbuild_project" ] && jwbuild_project="jwbuild"
|
|
[ -z "$jwbuild_rc" ] && jwbuild_rc="$jwbuild_basename"rc
|
|
_jwbuild_probe_config_paths
|
|
[ -z "$jwbuild_loggers" ] && jwbuild_loggers="stdout"
|
|
fi
|