forked from EuCaue/gnome-shell-extension-quick-lofi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-radios.sh
More file actions
executable file
·107 lines (95 loc) · 2.79 KB
/
Copy pathtest-radios.sh
File metadata and controls
executable file
·107 lines (95 loc) · 2.79 KB
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
#!/usr/bin/env bash
SCHEMA_DIR="$HOME/.local/share/gnome-shell/extensions/quick-lofi@eucaue/schemas"
SCHEMA="org.gnome.shell.extensions.quick-lofi"
RADIO_KEY="radios"
ALPHABET="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
RADIOS_JSON="./radios-test.json"
TEST_ID_PREFIX="TEST"
BOLD="\e[1m"
UNDERLINE="\e[4m"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
check_installed() {
requireds=("jq" "gsettings")
for program in "${requireds[@]}"; do
if ! command -v "$program" &>/dev/null; then
echo -e "${RED}${BOLD}$program is required.${NC} Please install it."
exit 1
fi
done
}
generate_nanoid() {
local nanoid=""
for _ in $(seq 1 10); do
# Generate a random byte and map it to a character from the alphabet
local index=$(head -c 1 /dev/urandom | od -An -tu1 | awk '{print $1 % 64}')
nanoid="${nanoid}${ALPHABET:$index:1}"
done
echo "$nanoid"
}
function add_radio() {
if [ ! -f "$RADIOS_JSON" ]; then
echo "$RADIOS_JSON not found!"
exit 1
fi
jq -c '.[]' $RADIOS_JSON | while read -r i; do
mapfile -t radio < <(echo "$i" | jq -r '[.radioName, .radioUrl] | @tsv' | tr '\t' '\n')
radio_item="${radio[0]} - ${radio[1]} - $TEST_ID_PREFIX$(generate_nanoid)"
echo "RADIO ITEM TEST: $radio_item"
current=$(gsettings --schemadir "$SCHEMA_DIR" get "$SCHEMA" "$RADIO_KEY" | sed "s/^\[//;s/\]$//;s/^ *//;s/ *$//")
gsettings --schemadir "$SCHEMA_DIR" set "$SCHEMA" "$RADIO_KEY" "[$current, '$radio_item']"
done
}
function remove_radio() {
radios_raw=$(gsettings --schemadir "$SCHEMA_DIR" get "$SCHEMA" "$RADIO_KEY" | sed "s/^\[//; s/\]$//; s/, '/\n/g; s/'//g")
radios_list=$(echo "$radios_raw" | grep -v " - TEST")
echo "RADIOS: $radios_list"
new_radios="[$(printf "%s\n" "$radios_list" | sed "s/^/'/; s/$/'/" | paste -sd, -)]"
gsettings --schemadir "$SCHEMA_DIR" set "$SCHEMA" "$RADIO_KEY" "$new_radios"
}
function list_radios() {
radios_list=$(gsettings --schemadir "$SCHEMA_DIR" get "$SCHEMA" "$RADIO_KEY" | sed "s/^\[//; s/\]$//; s/, '/\n/g; s/'//g")
echo "RADIOS: $radios_list"
}
function usage() {
echo "Usage: $0 [options]"
echo
echo "Options:"
echo " -a, --add Add test radios"
echo " -r, --remove Remove test radios"
echo " -l, --list List radios"
echo " -h, --help Show this help message"
echo
echo "Examples:"
echo " $0 -a"
echo " $0 --help"
exit 0
}
check_installed
if [ $# -eq 0 ]; then
usage
fi
while getopts "arl-:h" opt; do
case $opt in
a) add_radio ;;
r) remove_radio ;;
l) list_radios ;;
h) usage ;;
-)
case "${OPTARG}" in
add) add_radio ;;
remove) remove_radio ;;
list) list_radios ;;
help) usage ;;
*) echo "Unknown option --${OPTARG}" ;;
esac
;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
if [ $# -gt 0 ]; then
usage
fi