-
Notifications
You must be signed in to change notification settings - Fork 0
/
homy.sh
executable file
·175 lines (134 loc) · 4.52 KB
/
homy.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
170
171
172
173
174
175
#!/bin/bash
# homy - location aware configuration management
# homy -c PATH | [-h]
# We use Apple's own WiFi status tool - not sure if that is supported
# officially.
AIRPORT=/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport
templates=/usr/local/share/homy
config=/usr/local/etc/homy.json
state=/usr/local/var/run/homy/state
delay=10
home_ssid=""
work_ssid=""
jq_path="/usr/local/bin/jq"
last_location=""
configs=()
setups=()
teardowns=()
function usage() {
echo "usage: $0 -o SSID -w SSID [-t PATH] [-d DURATION] | [-h]"
}
function dumpy() {
echo "$(date +%FT%T): $1"
}
# Runs `airport -I` to get the currently connected WiFi SSID and uses that
# information to determine a location.
function location() {
local location=""
local wifi=$($AIRPORT -I | awk '{if($1 == "SSID:"){print $2}}')
case "$wifi" in
$home_ssid)
location=home
;;
$work_ssid)
location=work
;;
*)
location=unknown
;;
esac
echo "$location"
}
function updateLocationState() {
# We default to store the location in "/usr/local/var/run/homy/state".
echo -n $1 > $state
# Tell the status item app about the new state.
osascript <<APPLE_SCRIPT_NOTIFY
use framework "Foundation"
set myCenter to current application's NSDistributedNotificationCenter's defaultCenter()
myCenter's postNotificationName:"TTHomyStatusUpdate" object:(missing value) userInfo:(missing value) options:3
APPLE_SCRIPT_NOTIFY
}
function process() {
local location=$(location)
if [ "$location" != "$last_location" ]; then
dumpy "--- $location --------------------------------------------------"
updateLocationState $location
typeset -i i=0 max=${#configs[*]}
while (( i < max ))
do
destination_path="${configs[$i]}"
destination_name=$(basename $destination_path)
source_path=$templates/$destination_name.$location
if [ -f "$source_path" ]; then
dumpy "---- $destination_name ----"
teardown="${teardowns[$i]}"
if [[ ! -z $teardown ]]; then
dumpy "Making previously reachable resources unavailable: $teardown"
$($teardown)
fi
dumpy "Updating configuration $destination_path"
cp "$source_path" "$destination_path"
setup="${setups[$i]}"
if [[ ! -z $setup ]]; then
dumpy "Making location specific resources available: $setup"
$($setup)
fi
fi
i=i+1
done
dumpy "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
dumpy "Awaiting next location change..."
fi
last_location=$location
}
function init() {
IFS=$'\n' read -r -d '' -a configs \
< <(set -o pipefail; cat $config | \
$jq_path -r '.configurations[].path' && printf '\0')
IFS=$'\n' read -r -d '' -a setups \
< <(set -o pipefail; cat $config | \
$jq_path -r '.configurations[].setup' && printf '\0')
IFS=$'\n' read -r -d '' -a teardowns \
< <(set -o pipefail; cat $config | \
$jq_path -r '.configurations[].teardown' && printf '\0')
home_ssid=$(cat $config | $jq_path -r '.home_ssid')
work_ssid=$(cat $config | $jq_path -r '.work_ssid')
delay=$(cat $config | $jq_path -r '.delay')
templates=$(cat $config | $jq_path -r '.templates')
state=$(cat $config | $jq_path -r '.state')
if [ -z $home_ssid ] || [ -z $work_ssid ]; then
usage
exit
fi
}
function main() {
dumpy "= homy starting ===================================================="
dumpy "== initialize configuration ========================================"
init
dumpy "Home SSID: ${home_ssid}"
dumpy "Work SSID: ${work_ssid}"
dumpy "Templates path: ${templates}"
dumpy "Delay: ${delay}"
dumpy "Configuration paths: [${configs[*]}]"
dumpy "== daemon loop ====================================================="
while :
do
process
sleep $delay
done
}
while [ "$1" != "" ]; do
case $1 in
-c | --config ) shift
config=$1
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
main