-
Notifications
You must be signed in to change notification settings - Fork 2
/
configure
executable file
·98 lines (82 loc) · 2.51 KB
/
configure
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env sh
DEFAULT_FILE=./src/defaults.h
CONFIG_FILE=./src/config.h
if [ ! -e "$DEFAULT_FILE" ]; then
printf 'No default configuration header found\n'
exit
fi
if [ ! -e "$CONFIG_FILE" ]; then
printf 'No configuration header found\n'
exit
fi
CONFIG_KEYS=$(sed -E -n 's/^#define ([^_][^(]+) .+/\1/p' "$DEFAULT_FILE")
len=$(mktemp)
_firstlen='reset-config'
printf '%d\n' "${#_firstlen}" > "$len"
printf '%s\n' "$CONFIG_KEYS" | while IFS= read -r key; do
IFS= read -r printlen <"$len"
if [ "$((${#key}))" -gt "$printlen" ]; then
printf '%d\n' "${#key}" > "$len"
fi
done
IFS= read -r printlen <"$len"
show_config () {
printf '%s\n' "$CONFIG_KEYS" | while IFS= read -r key; do
DEFAULT=$(sed -E -n "s/^#define $key (.+)/\1/p" "$DEFAULT_FILE")
CONFIGD=$(sed -E -n "s/^#define $key (.+)/\1/p" "$CONFIG_FILE")
printf '%s = ' "$key"
if [ -n "$CONFIGD" ]; then
printf '%s (%s)' "$CONFIGD" "$DEFAULT"
else
printf '%s' "$DEFAULT"
fi; printf '\n'
done
}
optusage () {
printf ' --%-*s\t%s\n' "$printlen" "$1" "$2"
}
usage () {
printf 'Usage: %s [options]\n\nOptions:\n' "$0"
printf '%s\n' "$CONFIG_KEYS" | while IFS= read -r key; do
OPT=$(printf '%s' "$key" | tr '[:upper:]_' '[:lower:]-')
HELP=$(sed -E -n "s|^#ifndef $key // (.+)|\1|p" "$DEFAULT_FILE")
optusage "$OPT" "$HELP"
done
optusage 'reset-config' 'reset configuration to the defaults'
optusage 'help' 'print this help and exit'
}
update () {
sed -i "$CONFIG_FILE" -re "s|^(//)?#define $1 \"(.*)\"$|#define $1 \"$2\"|"
}
restore_default_config () {
sed -re 's|^#define [^_][^()]+ .+|//&|' -i "$CONFIG_FILE"
}
for setting in "$@"; do
OPT=$(printf '%s' "$setting" | cut -d' ' -f1 | sed -E -n 's/--//p')
KEY=$(printf '%s' "$OPT" | sed -E -n 's/([^=]+)=.*/\1/p' | tr '[:lower:]-' '[:upper:]_')
VAL=$(printf '%s' "$OPT" | sed -E -n 's/.*=([^[:space:]]+)/\1/p')
case "$OPT" in
*'='*)
case "$CONFIG_KEYS" in
*"$KEY"*) update "$KEY" "$VAL";;
*)
printf 'unrecognized option: --%s\n' "$OPT"
exit
;;
esac;;
help)
usage
exit
;;
reset-config)
restore_default_config
show_config
exit
;;
*)
printf 'unrecognized option: --%s\n' "$OPT"
exit
;;
esac
done
show_config