123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #!/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_grepkey() {
-
-
-
- local wnt=$1
- grep '.' \
- | grep -v '\s*#' \
- | 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 '.' \
- | grep -v '\s*#' \
- | 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 | sort | uniq
- }
-
- _ini_maybe_expand() {
-
-
-
- if test "$MKIT_INI_EXPAND" -gt 0; then
- MKIT_INI_EXPAND=$(( --MKIT_INI_EXPAND )) _ini_expand
- else
- _ini_cat
- fi
- }
-
- ini() {
-
-
-
- local op=$1
- local arg=$2
- local fn
- local limit=_ini_cat
- case $op in
- lskeys) fn=_ini_lskeys ;;
- sec) fn=_ini_grepsec ;;
- values) fn=_ini_greppath ;;
- 1value) fn=_ini_greppath; limit="tail -1" ;;
- *) die "incorrect use of \`ini()\`"
- esac
- <"$MKIT_INI" $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"
- }
|