base-files: add function to remove devicename from LED labels
[openwrt/staging/dedeckeh.git] / package / base-files / files / lib / functions / migrations.sh
1 #!/bin/sh
2
3 . /lib/functions.sh
4
5 migrate_led_sysfs() {
6 local cfg="$1"; shift
7 local tuples="$@"
8 local sysfs
9 local name
10
11 config_get sysfs ${cfg} sysfs
12 config_get name ${cfg} name
13
14 [ -z "${sysfs}" ] && return
15
16 for tuple in ${tuples}; do
17 local old=${tuple%=*}
18 local new=${tuple#*=}
19 local new_sysfs
20
21 new_sysfs=$(echo ${sysfs} | sed "s/${old}/${new}/")
22
23 [ "${new_sysfs}" = "${sysfs}" ] && continue
24
25 uci set system.${cfg}.sysfs="${new_sysfs}"
26
27 logger -t led-migration "sysfs option of LED \"${name}\" updated to ${new_sysfs}"
28 done;
29 }
30
31 remove_devicename_led_sysfs() {
32 local cfg="$1"
33 local sysfs
34 local name
35 local new_sysfs
36
37 config_get sysfs ${cfg} sysfs
38 config_get name ${cfg} name
39
40 echo "${sysfs}" | grep -q ":.*:" || return
41
42 new_sysfs=$(echo ${sysfs} | sed "s/^[^:]*://")
43
44 uci set system.${cfg}.sysfs="${new_sysfs}"
45
46 logger -t led-migration "sysfs option of LED \"${name}\" updated to ${new_sysfs}"
47 }
48
49 migrate_leds() {
50 config_load system
51 config_foreach migrate_led_sysfs led "$@"
52 }
53
54 remove_devicename_leds() {
55 config_load system
56 config_foreach remove_devicename_led_sysfs led
57 }
58
59 migrations_apply() {
60 local realm="$1"
61 [ -n "$(uci changes ${realm})" ] && uci -q commit ${realm}
62 }