123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- #!/bin/bash
-
-
-
- __ini_cat() {
-
-
-
- local line
- while read -r line; do
- printf -- "%s\n" "$line"
- done
- }
-
- __ini_expand() {
-
-
-
- local line
- local suffix
- local ref
- local value
- while read -r line; do
- suffix="${line#\[*\]}"
- ref="${line%$suffix}"
- ref="${ref%\]}"
- ref="${ref#\[}"
- value="$(ini 1value "$ref")"
- printf -- "%s\n" "$value$suffix"
- done
- }
-
- __ini_grepcmt() {
-
-
-
- grep -v '^[[:space:]]*#'
- }
-
- __ini_grepkey() {
-
-
-
- local wnt=$1
- grep '.' \
- | sed -e 's/ *= */=/; s/ +$//; s/^//;' \
- | grep -e "^$wnt=" \
- | cut -d= -f2- \
- | __ini_maybe_expand
- }
-
- __ini_greppath() {
-
-
-
-
-
-
-
-
- local wnt=$1
- local wntkey=${wnt##*:}
- local wntsec=${wnt%:$wntkey}
- local override
- if test "$wntsec" = 'ENV'; then
- override=${!wntkey}
- test -n "$override" \
- && echo "$override" \
- && return
- fi
- __ini_grepsec "$wntsec" | __ini_grepkey "$wntkey"
- }
-
- __ini_grepsec() {
-
-
-
- local wnt=$1
- local ok=false
- local line
- grep '.' \
- | while read -r line; do
- case "$line" in
- \[$wnt\]) ok=true; continue ;;
- \[*\]) ok=false; continue ;;
- esac
- $ok || continue
- printf -- "%s\n" "$line"
- done \
- | sed -e 's/ *= */=/; s/ +$//; s/^//;'
- }
-
- __ini_lskeys() {
-
-
-
- local sct=$1
- __ini_grepsec "$sct" | cut -d= -f1 | awk '!x[$0]++'
- }
-
- __ini_lssect() {
-
-
-
- local arg=$1
- grep -x '\[.*\]' | sed 's/^.//; s/.$//'
- }
-
- __ini_maybe_expand() {
-
-
-
- if test "$MKIT_INI_EXPAND" -gt 0; then
- MKIT_INI_EXPAND=$(( --MKIT_INI_EXPAND )) __ini_expand
- else
- __ini_cat
- fi
- }
-
- __ini_body() {
-
-
-
-
-
- local inc
- local incre='\[INCLUDE:.*\]'
- local iline
- if iline=$(grep -m1 -x "$incre" "$MKIT_INI"); then
- inc=${iline#*:}; inc=${inc%]}
- grep -vx "$incre" "$inc"
- grep -vx "$incre" "$MKIT_INI"
- else
- cat "$MKIT_INI"
- fi | __ini_grepcmt
- }
-
- ini() {
-
-
-
- local op=$1
- local arg=$2
- local fn
- local limit=__ini_cat
- case $op in
- lskeys) fn=__ini_lskeys ;;
- lssect) fn=__ini_lssect ;;
- sec) fn=__ini_grepsec ;;
- values) fn=__ini_greppath ;;
- 1value) fn=__ini_greppath; limit="tail -1" ;;
- *) die "incorrect use of \`ini()\`"
- esac
- __ini_body | $fn "$arg" | $limit
- }
-
- update_version() {
-
-
-
- local version=$1
- local inifile=$2
- local tmp
- tmp=$(mktemp -t mkit.update_version.XXXXXXXX)
- <"$inifile" perl -e '
- my $hit = 0;
- my $done = 0;
- foreach (<STDIN>) {
- if ($done) { print; next; }
- elsif (m/\[project\]/) { $hit++; print; next; }
- elsif (m/\[/) { $hit = 0; print; next; }
- elsif ($hit) { s/\bversion\b( *)=( *).*/version$1=$2$ARGV[0]/ and $done++; print; }
- else { print; next; }
- }
- ' "$version" >"$tmp" || die "failed to update version in mkit.ini"
- mv "$tmp" "$inifile"
- }
|