-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmimic_batoceraSystemsetting.sh
169 lines (143 loc) · 5.33 KB
/
mimic_batoceraSystemsetting.sh
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
#!/bin/bash
# Mimic batoceraSettings.py
# goal: abbolish this python script, it's useless for the sake of the load feature only
#
# Usage of BASE COMMAND:
# -command <value> -key <value> -value <value>
#
# -command load write enable disable
# -key any key in batocera.conf (kodi.enabled...)
# -value any alphanumerical string
# use quotation marks to avoid globbing use slashe escape special characters
# This script reads only 1st occourance of string and writes only to 1st occurance
# So 10 entries overwritten with system.power.switch=RETROFLAG will never occour again.
#
# This script uses #-character to comment values
#
# If there is a bolean value (0,1) then enable and disable command will set the
# correspondending boolean value.
# Examples:
# ./batoceraSettings.sh -command load -key wifi.enabled will print out 0 or 1
# ./batoceraSettings.sh -command write -key wifi.ssid -value "This is my NET" will set wlan.ssid=This is my NET
# ./batoceraSettings.sh -command enable -key wifi.ssid will remove # from configfile (activate)
# ./batoceraSettings.sh -command disable -key wifi.enabled will set key wifi.enabled=0
# Greetings some special people:
# hiulit for his bash BOILERPLATE script which inspires me a lot in handling sed comman
# Moreover the insane coder meleu, he made my bash journey starting - hope he is well at all
readonly BATOCERA_CONFIGFILE="/userdata/system/batocera.conf"
readonly COMMENT_CHAR="#"
function get_config() {
#Will look for key.value and #key.value for only one occourance
#If the character is the COMMENT CHAR then set value to it
#Otherwise strip to equal-char
local val
val="$(grep -E -m1 ^$COMMENT_CHAR?\s*$1 $BATOCERA_CONFIGFILE)"
if [[ "${val:0:1}" == "$COMMENT_CHAR" ]]; then
val="$COMMENT_CHAR"
else
#Maybe here some finetuning to catch key.value = ENTRY without blanks
val="${val#*=}"
fi
echo "$val"
}
function set_config() {
#Will look for first key.name at line beginnng and write value to it
sed -i "1,/^\(\s*$1\s*=\).*/s//\1$2/" "$BATOCERA_CONFIGFILE"
}
function uncomment_config() {
#Will look for first Comment Char at line beginnng and remove it
sed -i "1,/^$COMMENT_CHAR\(\s*$1\)/s//\1/" "$BATOCERA_CONFIGFILE"
}
function comment_config() {
#Will look for first key.name at line beginnng and add a comment char to it
sed -i "1,/^\(\s*$1\)/s//$COMMENT_CHAR\1/" "$BATOCERA_CONFIGFILE"
}
function check_argument() {
# This method does not accept arguments starting with '-'.
if [[ -z "$2" || "$2" =~ ^- ]]; then
echo >&2
echo "ERROR: '$1' is missing an argument." >&2
echo >&2
echo "Try '$0 --help' for more info." >&2
echo >&2
return 1
fi
}
function usage() {
val=" Usage of BASE COMMAND:
-command <value> -key <value> -value <value>
-command load write enable disable
-key any key in batocera.conf (kodi.enabled...)
-value any alphanumerical string
use quotation marks to avoid globbing
For write command -value <value> must be provided"
echo "$val"
}
# MAIN
[[ -e "$BATOCERA_CONFIGFILE" ]] || exit 2
[[ "${1,,}" != "-command" ]] && usage && exit 1
check_argument $1 $2
[[ $? -eq 0 ]] || exit 1
command="$2"
shift 2
[[ "${1,,}" != "-key" ]] && usage && exit 1
check_argument $1 $2
[[ $? -eq 0 ]] || exit 1
keyvalue="$2"
shift 2
# value processing
case "$command" in
"read"|"get"|"load")
val="$(get_config $keyvalue)"
[[ -n "$val" ]] && echo "$val" || echo "$keyvalue: not found!"
;;
"set"|"write")
[[ ${1,,} != "-value" ]] && usage && exit 1
check_argument $1 $2
[[ $? -eq 0 ]] || exit 1
val="$(get_config $keyvalue)"
if [[ "$val" == "$COMMENT_CHAR" ]]; then
uncomment_config "$keyvalue"
set_config "$keyvalue" "$2"
elif [[ -z "$val" ]]; then
echo "$keyvalue: not found!"
elif [[ "$val" != "$2" ]]; then
set_config "$keyvalue" "$2"
fi
;;
"uncomment"|"enable"|"activate")
val="$(get_config $keyvalue)"
# Boolean
if [[ "$val" == "$COMMENT_CHAR" ]]; then
uncomment_config "$keyvalue"
elif [[ "$val" == "0" ]]; then
set_config "$keyvalue" "1"
elif [[ -z "$val" ]]; then
echo "$keyvalue: not found!"
fi
;;
"comment"|"disable"|"remark")
val="$(get_config $keyvalue)"
# Boolean
[[ "$val" == "$COMMENT_CHAR" || "$val" == "0" ]] && exit 0
if [[ -z "$val" ]]; then
echo "$keyvalue: not found!"
elif [[ "$val" == "1" ]]; then
set_config "$keyvalue" "0"
else
comment_config "$keyvalue"
fi
;;
"-h"|"--help")
usage
echo " Examples:
./batoceraSettings.sh -command load -key wifi.enabled will print out 0 or 1
./batoceraSettings.sh -command write -key wifi.ssid -value "This is my NET" will set wlan.ssid=This is my NET
./batoceraSettings.sh -command enable -key wifi.ssid will remove # from configfile (activate)
./batoceraSettings.sh -command disable -key wifi.enabled will set key wifi.enabled=0"
;;
*)
echo "ERROR: invalid option '$1'" >&2
exit 2
;;
esac