-
Notifications
You must be signed in to change notification settings - Fork 8
/
minmiconf
728 lines (607 loc) · 21.2 KB
/
minmiconf
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
#!/bin/sh
# Copyright (c) 2024, Mikhail Zakharov <zmey20000@yahoo.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ---------------------------------------------------------------------------- #
# Note, this is not an autotools configure script. Minimum Minimorum Configure #
# (MinMiConf) script is written for my humble needs to set values in Makefiles #
# ---------------------------------------------------------------------------- #
# -- Script variables -------------------------------------------------------- #
version="1.1.1.0"
_DEBUG="n"
_INCLUDE_PATH="" # Cache detected INCLUDE dirs
_LIB_PATH="" # Cache detected LIB directories
_TARGET="" # Cache detected TARGET
alias ESOFT="_EX=0" # return 1 on an error
alias EHARD="_EX=1" # exit 1 on an error
ESOFT
# ---------------------------------------------------------------------------- #
_YN() {
#
# Check $1 is set to:
# - "yY" or "[1-9]" and return 0
# - "nN" or 0 and return 1
# On any error return 2
#
case "$1" in
0|[nN]) return 1 ;;
[1-9]|[yY]) return 0 ;;
*) return 2 ;;
esac
}
# ---------------------------------------------------------------------------- #
_IFDEBUG() {
#
# Check debug mode - _DEBUG variable is set to:
# - "yY" or "[1-9]" and return 0
# - "nN" or 0 and return 1
# On any error return 2
#
_YN "$_DEBUG"; return $?
}
# ---------------------------------------------------------------------------- #
_DPRINTF() {
#
# Print a message from $* if in debug mode
#
_IFDEBUG && printf "DEBUG: %s\n" "$*" >&2
}
# ---------------------------------------------------------------------------- #
_SELF_CHK() {
#
# Quick mandatory selfcheck
#
EHARD; DECIDE DETECT_COMMAND "GREP" 'grep'
EHARD; DECIDE DETECT_COMMAND "SED" 'sed'
EHARD; DECIDE DETECT_COMMAND "MKTEMP" 'mktemp'
EHARD; DECIDE DETECT_COMMAND "RM" 'rm'
EHARD; DECIDE DETECT_COMMAND "UNAME" 'uname'
}
# ---------------------------------------------------------------------------- #
IF_NDEF_OR_IVAR() {
#
# IF $1 variable is NOT defined or has $2 numeric value,
# return 0 otherwise return 1
#
_var=$(eval echo '$'"$1")
_val=$2
[ -z "$_var" ] && {
_DPRINTF "IF_NNEG_IVAR(): [$1] does not exist"
return 0
}
[ $_var -eq $2 ] && {
_DPRINTF "IF_NNEG_IVAR(): [$1] == [$2]"
return 0
}
_DPRINTF "IF_NNEG_IVAR(): [$1] != [$2]"
return 1
}
# ---------------------------------------------------------------------------- #
IN() {
#
# String-in-string. Return 0 if $2 is in $1, otherwise return 1
# $3 == [yY] ignore case; $3 == [nN]||"" stay case-sensitive
#
_icase=""
[ -n "$3" ] && case "$3" in
[nN]) _icase="" ;;
[yY]) _icase="-i" ;;
*) _icase="" ;;
esac
echo "$1" | grep $(eval echo "$_icase") -q "$2" && return 0 || return 1
}
# ---------------------------------------------------------------------------- #
IN_VAR() {
#
# String-in-variable. Return 0 if $2 is in $1, otherwise return 1
# $3 == [yY] ignore case; $3 == [nN]||"" stay case-sensitive
#
_v=$(eval echo '$'"$1")
IN "$_v" "$2" "$3" && return 0 || return 1
}
# ---------------------------------------------------------------------------- #
DETECT_COMMAND() {
#
# Try to detect $2 command existence;
# Results variable: $1
# Return: 0 - on success, 1 on failure or error
#
_out=$(which "$2")
[ $? -eq 0 ] && {
_DPRINTF "DETECT_COMMAND(): Command [$1] found"
eval $1='$_out' # Return value
return 0
} || {
_DPRINTF "DETECT_COMMAND(): Command [$1] not found"
eval $1='$_out' # Return value
return 1
}
}
# ---------------------------------------------------------------------------- #
DETECT_PATH() {
#
# Detect valid path(s) from a list
#
# $1 - Results variable
# $2 - Space separated path specifications
# $3 - permissions to check (dfrwx), default: "drx"
# $4 - "all" to print all valid (default); "one" to show the 1-st one only
# Results variable: Matched path(s) or empty string on error
# Return: 0 - on success, 1 on failure or error
#
_out=''
_path="$2"
# Prepare permissions string for iteration: "DRWX" -> "d r w x "
[ -n "$3" ] &&
_prms="$(echo "$3" |
sed -e 's/\([a-z]\)/\1 /g' \
-e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/')" ||
_prms="drx"
[ -z "$4" ] && _all=1 ||
case "$4" in
[oO][nN][eE])
_all=0
_DPRINTF "DETECT_PATH(): Print all valid: disabled"
;;
[aA][lL][lL])
_all=1
_DPRINTF "DETECT_PATH(): Print all valid: enabled"
;;
*)
_DPRINTF "DETECT_PATH(): Argument \$4: [$4] is not valid"
eval $1='' # Return value
return 1
;;
esac
_DPRINTF "DETECT_PATH(): Print all valid: [$_all]"
_DPRINTF "DETECT_PATH(): Permissions: [$_prms]"
_rcode=1
for _p in $_path; do
_inp=0
_acp=0
_IFDEBUG && _dout=""
_DPRINTF "DETECT_PATH(): Checking: [$_p]"
for _prm in $_prms; do
case "$_prm" in
*d*)
_inp=$(($_inp + 1))
[ -d "$_p" ] && {
_acp=$(($_acp + 1))
_IFDEBUG && _dout="$_dout""d"
} || break;
;;
*f*)
_inp=$(($_inp + 10))
[ -f "$_p" ] && {
_acp=$(($_acp + 10))
_IFDEBUG && _dout="$_dout""f"
} || break;
;;
*r*)
_inp=$(($_inp + 100))
[ -r "$_p" ] && {
_acp=$(($_acp + 100))
_IFDEBUG && _dout="$_dout""r"
} || break;
;;
*w*)
_inp=$(($_inp + 1000))
[ -w "$_p" ] && {
_acp=$(($_acp + 1000))
_IFDEBUG && _dout="$_dout""w"
} || break;
;;
*x*)
_inp=$(($_inp + 10000))
[ -x "$_p" ] && {
_acp=$(($_acp + 10000))
_IFDEBUG && _dout="$_dout""x"
} || break;
;;
*)
_DPRINTF "DETECT_PATH(): Path: wrong permissions argument: [$_prm]"
eval $1='' # Return value
return 1
;;
esac
done
[ $_inp -eq $_acp ] && {
_rcode=0
_DPRINTF "DETECT_PATH(): Valid path: [$_dout] [$_p]"
[ -z "$_out" ] && _out="$_p" || _out="$_out $_p"
if [ $_all -eq 0 ]; then
eval $1='$_p' # Return value
return 0
fi
} || {
_DPRINTF "DETECT_PATH(): NOT valid: [$_p] Score: $_inp/$_acp"
}
done
[ $_rcode -eq 1 ] && {
_DPRINTF "DETECT_PATH(): No valid path detected"
eval $1='' # Return value
return 1
} || {
eval $1='$_out' # Return value
return 0
}
}
# ---------------------------------------------------------------------------- #
DETECT_IPATH() {
#
# Detect valid include path(s) from $2 and/or INCLUDE_PATH, INCLUDE_PATH_CANDIDATES
#
# $1 - Results variable
# $2 - Space separated path specifications
# $3 - permissions to check (dfrwx)
# $4 - 1 or "all" to print all valid; 0 or "" to show the first one only
# Output: Prints matched path(s) or empty string on error
# Return: 0 - on success, 1 on failure or error
#
[ -z "$2" -a -z "$INCLUDE_PATH" -a -z "$INCLUDE_PATH_CANDIDATES" ] && {
_DPRINTF "DETECT_IPATH(): Neither INCLUDE_PATH, INCLUDE_PATH_CANDIDATES nor \$2 list is set"
eval $1='' # Return value
return 1
}
_ipath="$2"
_scope="$4"
[ -n "$INCLUDE_PATH" -a "$_ipath" != "$INCLUDE_PATH" ] && _ipath="$_ipath $INCLUDE_PATH"
[ -n "$INCLUDE_PATH_CANDIDATES" -a "$_ipath" != "$INCLUDE_PATH_CANDIDATES" ] &&
_ipath="$_ipath $INCLUDE_PATH_CANDIDATES"
[ -n "$4" ] && _scope="$4" || _scope="all"
_dp=""
DETECT_PATH "_dp" "$_ipath" "dr" "$_scope"
[ $? -gt 0 ] && {
_DPRINTF "DETECT_IPATH(): No available include paths"
eval $1='' # Return value
return 1
}
_out="" # Clean _out spoiled by DETECT_PATH()
for i in $_dp; do
[ -z "$_out" ] && _out="-I$i" || _out="$_out -I$i"
done
_DPRINTF "DETECT_IPATH(): [$_out]"
eval $1='$_out' # Return value
_INCLUDE_PATH="$_out" # Save to reuse in detections
return 0
}
# ---------------------------------------------------------------------------- #
DETECT_LPATH() {
#
# Detect valid library path(s) from $2 and/or LIB_PATH, LIB_PATH_CANDIDATES
#
# $1 - Results variable
# $2 - Space separated path specifications
# $3 - 1 or "all" to print all valid; 0 or "" to show the first one only
# Output: Prints matched path(s) or empty string on error
# Return: 0 - on success, 1 on failure or error
#
[ -z "$2" -a -z "$LIB_PATH" -a -z "$LIB_PATH_CANDIDATES" ] && {
_DPRINTF "DETECT_LPATH(): Neither LIB_PATH, LIB_PATH_CANDIDATES nor \$2 list is set"
eval $1='' # Return value
return 1
}
_lpath="$2"
_scope="$3"
[ -n "$LIB_PATH" -a "$_lpath" != "$LIB_PATH" ] && _lpath="$_lpath $LIB_PATH"
[ -n "$LIB_PATH_CANDIDATES" -a "$_lpath" != "$LIB_PATH_CANDIDATES" ] &&
_lpath="$_lpath $LIB_PATH_CANDIDATES"
[ -n "$4" ] && _scope="$3" || _scope="all"
_dp=""
DETECT_PATH "_dp" "$_lpath" "dr" "$_scope"
[ $? -gt 0 ] && {
_DPRINTF "DETECT_LPATH(): No available library paths"
eval $1='' # Return value
return 1
}
_out="" # Clean _out spoiled by DETECT_PATH()
for l in $_dp; do
[ -z "$_out" ] && _out="-L$l" || _out="$_out -L$l"
done
_DPRINTF "DETECT_LPATH(): [$_out]"
eval $1='$_out' # Return value
_LIB_PATH="$_out" # Save to reuse in detections
return 0
}
# ---------------------------------------------------------------------------- #
DETECT_PREFIX() {
#
# Detect prefix from $2 and PREFIX_CANDIDATES, or sets forcible from PREFIX variable
#
# $1 - Results variable
# $2 - Space separated path specifications
# $3 - permissions to check (dfrwx), default: "dr"
# Return: 0 - on success, 1 on failure or error
#
[ -z "$PREFIX" -a -z "$2" -a "$PREFIX_CANDIDATES" ] && {
_DPRINTF "DETECT_PREFIX(): Neither PREFIX, PREFIX_CANDIDATES nor \$2 list is set"
eval $1='' # Return value
return 1
}
[ -n "$PREFIX" ] && {
_DPRINTF "DETECT_PREFIX(): [$_dp]"
eval $1='$PREFIX' # Return value
return 0
}
_ppath="$2"
_perms=${3-"dr"}
[ -n "$PREFIX_CANDIDATES" -a "$_ppath" != "$PREFIX_CANDIDATES" ] &&
_ppath="$_ppath $PREFIX_CANDIDATES"
_dp=""
DETECT_PATH "_dp" "$_ppath" "$_perms" "one"
[ $? -gt 0 ] && {
_DPRINTF "DETECT_PREFIX(): No prefix available"
eval $1='' # Return value
return 1
}
_DPRINTF "DETECT_PREFIX(): [$_dp]"
eval $1='$_dp' # Return value
return 0
}
# ---------------------------------------------------------------------------- #
DETECT_LIBRARY() {
#
# Detect existence of a library by its filename
#
# $1 - Results variable
# $2 - Library name to detect
# $3 - Optional path(s) in addition to LIB_PATH to detect the lib
# LIBPATH overrides all other library search paths
#
[ -n "$2" ] && _tlib="$2" || {
_DPRINTF "DETECT_LIBRARY(): Missing library to detect"
return 1
}
[ -n "$3" ] && _lpath="$3"
[ -n "$LIB_PATH" -a "$_lpath" != "$LIB_PATH" ] &&
_lpath="$_lpath $LIB_PATH"
# Cached LIB directories
[ -n "$_LIB_PATH" -a "$_lpath" != "$_LIB_PATH" ] &&
_lpath="$_lpath $_LIB_PATH"
[ -n "$LIBPATH" ] && _lpath="$LIBPATH"
[ -z "$CC" ] && {
_DPRINTF "DETECT_LIBRARY(): Missing C-compiler"
return 1
}
_tmpd=$(mktemp -d ./minmiconf.XXXXXX) || {
_DPRINTF "DETECT_LIBRARY(): Failed creating a temporary directory"
return 1
}
_testc="$_tmpd/ctest.c"
echo "int main() {return 0;}" > $_testc
$CC $_testc -o $_testc.out -l"$_tlib" $_lpath > /dev/null 2>&1
_r=$?
rm -r $_tmpd
[ $_r -eq 0 ] && {
eval $1='-l$_tlib'
_DPRINTF "DETECT_LIBRARY(): Library: [$_tlib] found"
return 0
} || {
eval $1=''
_DPRINTF "DETECT_LIBRARY(): Library: [$_tlib] not found"
return 1
}
}
# ---------------------------------------------------------------------------- #
DETECT_CC() {
#
# Detect C-compiler
# $1 - Results variable
# $2 - Compiler candidates in addition to CC_CANDIDATES variable
#
[ -n "$2" ] && _ccs="$2" || _ccs=""
[ -n "$CC_CANDIDATES" -a "$_ccs" != "$CC_CANDIDATES" ] &&
_ccs="$_ccs $CC_CANDIDATES"
_save_cc="$CC" # Save CC value
for _cc in $_ccs; do
CC="$_cc"
# Does cc really works?
which "$CC" > /dev/null 2>&1 &&
DETECT_LIBRARY LIB_NAME "c" "$LIB_PATH $_LIB_PATH" > /dev/null 2>&1 && {
eval $1='$CC'
_DPRINTF "DETECT_CC(): [$CC] found"
[ "$1" != "CC" ] && CC=$_save_cc # Restore CC
return 0
}
done
eval $1=''
_DPRINTF "DETECT_CC(): $_ccs not found"
return 1
}
# ---------------------------------------------------------------------------- #
DETECT_TARGET() {
#
# Detect target triplet: machine-vendor-os
# $1 - Results variable
#
# Very rough approach
_machine=$(uname -m)
_vendor="unknown"
_os=$(uname -s)
[ "$_machine" = "amd64" ] && _machine="x86_64"
_triplet="$_machine-$_vendor-$_os"
_DPRINTF "DETECT_TARGET(): [$_triplet]"
eval $1='$_triplet'
_TARGET="$_triplet" # Cache TARGET
return 0
}
# ---------------------------------------------------------------------------- #
DETECT_USER() {
#
# Detect current username; Respects USER and WITH_USER variables
# $1 - Results variable
#
[ -n "$USER" ] && _user="$USER"
[ -n "$WITH_USER" ] && _user="$WITH_USER"
_user=${_user=$(whoami)}
eval $1='$_user'
_DPRINTF "DETECT_USER(): detected user: [$_user]"
[ -n "$_user" ] && return 0 || return 1
}
# ---------------------------------------------------------------------------- #
DEFINE_VAR() {
#
# Define a variable and set its value
# $1 Results variable
# $2 the variable value
# Always returns 0
#
_DPRINTF "DEFINE_VAR(): setting variable: [$(eval echo "$1") = $2]"
eval $1='$2'
return 0
}
# ---------------------------------------------------------------------------- #
DECIDE() {
#
# Checking for wrapper with ok/ko result output
# $* is a function with arguments to run
# Returns 0 on success or 1 if a failure occured
case "$1" in
detect_*)
_target=$(echo $1 | cut -f 2,3 -d '_')
;;
*)
_target=$1
;;
esac
printf "Checking: %16s: " "$_target"
# In squre brackets IN() greps for '$' and decides to eval variable or not
eval $@ && {
printf "%-4s" "ok:"
eval echo $2: '$'$2 || echo $2: $2
return 0
} || {
printf "%-4s" "ko:"
[ -n "$3" ] && {
IN "$3" '\$' && eval echo $2: $3 || echo $2: $3
} || {
eval echo $2 = '$'$2
}
[ $_EX -eq 0 ] && return 1 || {
printf "FATAL: Mandatory check failed!\n"
exit 1
}
}
}
# ---------------------------------------------------------------------------- #
NOTIFY() {
#
# Print '$2' message with premble $1
# Always returns 0
#
printf "%-8s: %s\n" "$1" "$2"
return 0
}
# ---------------------------------------------------------------------------- #
CONFIG_START() {
#
# Start configuring
# $1 - Results variable
# $2 - lock-file, default .configured
# $3 == '[fF]' force!
# Returns 0 if OK, or 1 if KO
#
_lock=${2-".configured"}
[ "$3" = "f" -o "$3" = "F" ] && rm -r "$_lock"
eval $1='$_lock'
[ -f "$_lock" ] && {
_DPRINTF "CONFIG_START(): Already configured!"
return 1
}
_DPRINTF "CONFIG_START(): Not configured!"
return 0
}
# ---------------------------------------------------------------------------- #
CONFIG_FINISH() {
#
# Finish configuration
# $1 - Results variable
# $2 - lock-file, default .configured
#
_lock=${2-".configured"}
eval $1='$_lock'
touch "$_lock" && {
_DPRINTF "CONFIG_FINISH(): Configured!"
return 0
}
_DPRINTF "CONFIG_FINISH(): Failed to set Configured flag!"
return 1
}
# ---------------------------------------------------------------------------- #
CONFIG_CLEAN() {
#
# Remove configuration lock
# $1 - Results variable
# $2 - lock-file, default .configured
#
_lock=${2-".configured"}
eval $1='$_lock'
rm -r "$_lock" && {
_DPRINTF "CONFIG_CLEAN(): Lock released!"
return 0
}
_DPRINTF "CONFIG_CLEAN(): Failed to release configuration lock!"
return 1
}
# ---------------------------------------------------------------------------- #
WRITE_VARS() {
#
# Write variables and their values into a file
#
# $1 - a string containing space separated variable names
# $2 - a file to write or (update) the variables
# $3 - Assignment mod for newly created vars: ""(empty, default), "+" or "?""
# $4 - [yY] make a backup of modified file or [nN], default [nN]
#
_vars=$1
_file=$2
_mod=${3-""}
_back=${4-"N"}
_var_rgx="^[[:space:]]*"
_val_rgx="[[:space:]]*[a-zA-Z0-9[:punct:][:space:]]*"
case "$3" in
\=|\+|\?) _mod="$3" ;;
*) _mod="" ;;
esac
for i in $_vars; do
_var="$i"
_val="$(eval echo '$'{$i})"
# Find if variable is in the file
[ "$(grep "$_var_rgx$_var[[:space:]]*[\?\+]*=$_val_rgx" "$_file")" ] && {
# Update the variable value...
_DPRINTF "WRITE_VARS(): Updating [$_var] = [$_val] > [$_file] Backup: [$_back]"
_YN "$_back" && cp "$_file" "$_file.back"
IN "$TARGET" "Linux" "Y" || IN "$TARGET" "OpenBSD" "Y" && {
sed -i -e 's|\('"$_var_rgx"''"$_var"'[[:space:]]*[\?\+]*=\)'"$_val_rgx"'|\1'"$_val"'|' "$_file"
} || {
sed -i '' 's|\('"$_var_rgx"''"$_var"'[[:space:]]*[\?\+]*=\)'"$_val_rgx"'|\1'"$_val"'|' "$_file"
}
} || {
# ... or initiate it with the value
_YN "$_back" && cp "$_file" "$_file.back"
_DPRINTF "WRITE_VARS(): Creating [$_var] $_mod= [$_val] > [$_file] Backup: [$_back]"
_f=$(cat $_file)
printf "%s%s=%s\n%s" "$_var" "$_mod" "$_val" "$_f" > $_file
}
done
}
# ---------------------------------------------------------------------------- #
_SELF_CHK