aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2020-07-13 12:29:39 +0200
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2020-07-13 12:29:39 +0200
commitce2f74f9da8a2289bdec2b52b36ea220602f7ccf (patch)
tree3aacdb2e03453c23cde45d84e69e127daea41e93
parentc529c0628b08d526c30acf9d92afd0005bb0d00d (diff)
downloadltask-ce2f74f9da8a2289bdec2b52b36ea220602f7ccf.tar.gz
lib: confedit
-rw-r--r--libs/confedit47
1 files changed, 47 insertions, 0 deletions
diff --git a/libs/confedit b/libs/confedit
new file mode 100644
index 0000000..79bb1ad
--- /dev/null
+++ b/libs/confedit
@@ -0,0 +1,47 @@
+# vi: ft=sh
+
+CONF_DELIMITER="="
+CONF_SETTING=()
+CONF_VALUES=()
+
+# 1: Setting 2: Value
+function conf_set() {
+ CONF_SETTING+=("$1")
+ CONF_VALUES+=("$2")
+}
+
+# 1: File
+# Modify Configuration file
+function conf_apply() {
+ [ -z "$1" ] && return
+
+ if [ ! -f "$1" ]; then
+ >&2 echo $1 not found. Creating it
+ conf_create $@
+ return $?
+ fi
+
+ for (( i=0; i < ${#CONF_SETTING[@]}; i++ )); do
+ if grep -q "^\s*${CONF_SETTING[$i]}\s*$CONF_DELIMITER\s*.*\$" "$1"; then
+ sed -i "s/^\(\s*${CONF_SETTING[$i]}\s*$CONF_DELIMITER\s*\).*\$/\1${CONF_VALUES[$i]}/" "$1"
+ else
+ echo "${CONF_SETTING[$i]}$CONF_DELIMITER${CONF_VALUES[$i]}" >> "$1"
+ fi
+ done
+
+ CONF_SETTING=()
+ CONF_VALUES=()
+}
+
+# 1: File
+# Create configuration file, replace if existing
+function conf_create() {
+ [ -f "$1" ] && mv "$1" "$1.bak"
+
+ for (( i=0; i < ${#CONF_SETTING[@]}; i++ )); do
+ echo "${CONF_SETTING[$i]}${CONF_DELIMITER}${CONF_VALUES[$i]}" >> "$1"
+ done
+
+ CONF_SETTING=()
+ CONF_VALUES=()
+}