-
Notifications
You must be signed in to change notification settings - Fork 100
/
set_configs.sh
executable file
·134 lines (116 loc) · 3.51 KB
/
set_configs.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
#!/bin/sh
YI_HACK_PREFIX="/tmp/sd/yi-hack"
sedencode(){
# echo -e "$(sed 's/\\/\\\\\\/g;s/\&/\\\&/g;s/\//\\\//g;')"
echo "$(sed 's/\\/\\\\/g;s/\"/\\\"/g;s/\&/\\\&/g;s/\//\\\//g;')"
}
removedoublequotes(){
echo "$(sed 's/^"//g;s/"$//g')"
}
validateDT()
{
for x in 1 2 3 4 5 6 10 15 20 30 60 120 180 240 360 1440; do
if [ "$x" == "$1" ]; then
return 0
fi
done
if [ "${1:0:5}" == "1440+" ]; then
OFF="${1:5:10}"
if [ ! -z $OFF ]; then
case $OFF in
''|*[!0-9]* )
OFF_OK=0;;
* )
OFF_OK=1;;
esac
if [ "$OFF_OK" == "1" ] && [ $OFF -le 1440 ]; then
return 0
fi
fi
fi
return 1
}
get_conf_type()
{
CONF="$(echo $QUERY_STRING | cut -d'=' -f1)"
VAL="$(echo $QUERY_STRING | cut -d'=' -f2)"
if [ $CONF == "conf" ] ; then
echo $VAL
fi
}
. $YI_HACK_PREFIX/www/cgi-bin/validate.sh
if ! $(validateQueryString $QUERY_STRING); then
printf "Content-type: application/json\r\n\r\n"
printf "{\n"
printf "\"%s\":\"%s\"\\n" "error" "true"
printf "}"
exit
fi
CONF_TYPE="$(get_conf_type)"
CONF_FILE=""
if [ "$CONF_TYPE" == "mqtt" ] ; then
CONF_FILE="$YI_HACK_PREFIX/etc/mqttv4.conf"
else
CONF_FILE="$YI_HACK_PREFIX/etc/$CONF_TYPE.conf"
fi
read -r POST_DATA
# Validate json
VALID=$(echo "$POST_DATA" | jq -e . >/dev/null 2>&1; echo $?)
if [ "$VALID" != "0" ]; then
printf "Content-type: application/json\r\n\r\n"
printf "{\n"
printf "\"%s\":\"%s\"\\n" "error" "true"
printf "}"
exit
fi
# Change temporarily \n with \t (2 bytes)
POST_DATA="${POST_DATA//\\n/\\t}"
IFS=$(echo -en "\n\b")
ROWS=$(echo "$POST_DATA" | jq -r '. | keys[] as $k | "\($k)=\(.[$k])"')
for ROW in $ROWS; do
ROW=$(echo "$ROW" | removedoublequotes)
KEY=$(echo "$ROW" | cut -d'=' -f1)
# Change back tab with \n
VALUE=$(echo "$ROW" | cut -d'=' -f2 | sed 's/\t/\\n/g')
if [ "$KEY" == "HOSTNAME" ] ; then
if [ -z $VALUE ] ; then
# Use 2 last MAC address numbers to set a different hostname
MAC=$(cat /sys/class/net/wlan0/address|cut -d ':' -f 5,6|sed 's/://g')
if [ "$MAC" != "" ]; then
hostname yi-$MAC
else
hostname yi-hack
fi
hostname > $YI_HACK_PREFIX/etc/hostname
else
hostname $VALUE
echo "$VALUE" > $YI_HACK_PREFIX/etc/hostname
fi
elif [ "$KEY" == "MOTION_IMAGE_DELAY" ] ; then
if $(validateNumber $VALUE); then
VALUE=$(echo $VALUE | sed 's/,/./g')
VAR=$(awk 'BEGIN{ print "'$VALUE'"<="'5.0'" }')
if [ "$VAR" == "1" ]; then
sed -i "s/^\(${KEY}\s*=\s*\).*$/\1${VALUE}/" $CONF_FILE
fi
fi
elif [ "$KEY" == "PROXYCHAINS_SERVERS" ] ; then
VALUE=$(echo $VALUE | sed 's/^\"//g')
VALUE=$(echo $VALUE | sed 's/\"$//g')
VALUE=$(echo $VALUE | sed 's/;/\\n/g')
cat $CONF_FILE.template > $CONF_FILE
echo -e $VALUE >> $CONF_FILE
elif [ "$KEY" == "TIMELAPSE_DT" ] ; then
if $(validateDT $VALUE); then
sed -i "s/^\(${KEY}\s*=\s*\).*$/\1${VALUE}/" $CONF_FILE
fi
else
VALUE=$(echo "$VALUE" | sedencode)
sed -i "s/^\(${KEY}\s*=\s*\).*$/\1${VALUE}/" $CONF_FILE
fi
done
# Yeah, it's pretty ugly.
printf "Content-type: application/json\r\n\r\n"
printf "{\n"
printf "\"%s\":\"%s\"\\n" "error" "false"
printf "}"