d2f1bc19aac055c9475a27d25350eea0eb09cd82
[openwrt/openwrt.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"; shift
33 local exceptions="$@"
34 local sysfs
35 local name
36 local new_sysfs
37
38 config_get sysfs ${cfg} sysfs
39 config_get name ${cfg} name
40
41 # only continue if two or more colons are present
42 echo "${sysfs}" | grep -q ":.*:" || return
43
44 for exception in ${exceptions}; do
45 # no change if exceptions provided as argument are found for devicename
46 echo "${sysfs}" | grep -q "^${exception}:" && return
47 done
48
49 new_sysfs=$(echo ${sysfs} | sed "s/^[^:]*://")
50
51 uci set system.${cfg}.sysfs="${new_sysfs}"
52
53 logger -t led-migration "sysfs option of LED \"${name}\" updated to ${new_sysfs}"
54 }
55
56 migrate_leds() {
57 config_load system
58 config_foreach migrate_led_sysfs led "$@"
59 }
60
61 remove_devicename_leds() {
62 config_load system
63 config_foreach remove_devicename_led_sysfs led "$@"
64 }
65
66 migrations_apply() {
67 local realm="$1"
68 [ -n "$(uci changes ${realm})" ] && uci -q commit ${realm}
69 }