-
Notifications
You must be signed in to change notification settings - Fork 9
/
di-auto.sh
executable file
·276 lines (181 loc) · 5 KB
/
di-auto.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env zsh -f
# Purpose: Checks all di-*.sh scripts in same directory to see if the corresponding apps are installed.
#
# From: Alister Forbes
# Mail: di at superbimble dot com
# Date: 2016-05-21
#
# Updated by TJ Luoma (luomat at gmail dot com) on 2018-08-06
#
# See 'di-auto.txt' for details about significant changes to this script:
# https://github.com/tjluoma/di/blob/master/di-auto.txt
if [[ -e "$HOME/.path" ]]
then
source "$HOME/.path"
fi
VIA_MACUPDATER='no'
for ARGS in "$@"
do
case "$ARGS" in
--macupdater)
VIA_MACUPDATER='yes'
shift
;;
# -|--)
#
# :
# shift
# ;;
-*|--*)
echo " $NAME [warning]: Don't know what to do with arg: $1"
shift
;;
esac
done # for args
PPID_NAME=$(/bin/ps -p $PPID | fgrep '/sbin/launchd' | awk '{print $NF}')
if [ "$PPID_NAME" = "/sbin/launchd" ]
then
# this was launched via launchd
IS_LAUNCHD='yes'
else
IS_LAUNCHD='no'
fi
NAME="$0:t:r"
zmodload zsh/datetime
function timestamp { strftime "%Y-%m-%d @ %H:%M:%S" "$EPOCHSECONDS" }
LASTRUN="$HOME/.$NAME.lastrun.txt"
LOCKFILE="/tmp/.${NAME}.${LOGNAME}.lock"
[[ ! -e "$LASTRUN" ]] && echo '0' > "$LASTRUN"
[[ "$1" == "--force" ]] && echo '0' > "$LASTRUN" && rm -f "$LOCKFILE"
LASTRUN_TIME=$(tail "$LASTRUN" | egrep '[0-9]' | tail -1 | awk '{print $1}')
DIFF=$(($EPOCHSECONDS - $LASTRUN_TIME))
# unless "forced" don't run if we've run in the last 59 minutes (3540 seconds)
# now 30 minutes (1800 seconds)
if [ "$DIFF" -lt "1799" ]
then
TIME_AGO_READABLE=$(seconds2readable.sh "$DIFF")
growlnotify \
--appIcon "iTerm" \
--identifier "$NAME" \
--message "${TIME_AGO_READABLE} ago" \
--title "$NAME not running.
Last ran: "
MSG="$NAME not running. Ran $TIME_AGO_READABLE ago."
echo "$MSG" >>/dev/stderr
[[ "$VIA_MACUPDATER" == "no" ]] && po.sh "$MSG"
exit 0
fi
DATE=`strftime "%Y-%m-%d" "$EPOCHSECONDS"`
LOG="$HOME/Library/Logs/$NAME.$DATE.log"
[[ -e "$LOG" ]] || touch "$LOG" # Create log file if needed
function log { echo "$NAME [`timestamp`]: $@" | tee -a "$LOG" }
if [[ -e "$LOCKFILE" ]]
then
LOCKED_TIME=$(cat "$LOCKFILE" | sed 's#.* -- ##g')
growlnotify \
--appIcon "iTerm" \
--identifier "$NAME LOCKFILE" \
--message "Locked since
$LOCKED_TIME" \
--title "$NAME: found LOCKFILE"
echo "$NAME: Lockfile found at ${LOCKFILE}. Locked since ${LOCKED_TIME}. Exiting."
po.sh "$NAME: Locked since $LOCKED_TIME"
exit 0
else
START_TIME="$EPOCHSECONDS"
echo "$EPOCHSECONDS ($$) -- `timestamp`" >| "$LOCKFILE"
echo "$EPOCHSECONDS `timestamp`" >>| "$LASTRUN"
fi
COUNT=0
# chdir to the directory where this script is found
cd "$0:h"
log "------------- STARTING AT `timestamp` -------------"
for i in di-*sh
do
#check to ignore di-all.sh and di-auto.sh
if ! [[ "$i:t" =~ "di-(all|auto)\.sh" ]]
then
# echo "\n\n---: [Debug] Starting $i: \n"
# get the full path to $i
i=($i(:A))
unset INSTALL_TO
IFS=$'\n' ALL_INSTALLS=($(fgrep 'INSTALL_TO=' "$i" | fgrep -v '#' | fgrep -v 'INSTALL_TO="$' | sort -u))
if [[ "$ALL_INSTALLS" == "" ]]
then
IFS=$'\n' ALL_INSTALLS=($(fgrep 'INSTALL_TO=' "$i" | fgrep -v '#'))
fi
if [[ "$ALL_INSTALLS" == "" ]]
then
log "[ERROR!] No 'INSTALL_TO=' found in '$i'."
else
echo "$ALL_INSTALLS" | while read line
do
unset INSTALL_TO
eval $line
if [[ "$INSTALL_TO" != "" ]]
then
if [[ -e "$INSTALL_TO" ]]
then
[[ "$VERBOSE" == "yes" ]] && log "Running '$i'"
"$i" 2>&1 | tee -a "$LOG"
EXIT="$?"
if [[ "$EXIT" != "0" ]]
then
growlnotify --sticky \
--appIcon "$INSTALL_TO:t:r" \
--identifier "$i" \
--message "Exit With Error: $EXIT" \
--title "$i"
((COUNT++))
fi
else
## Uncomment the line below for more verbose output
[[ "$VERBOSE" == "yes" ]] && log "'$INSTALL_TO' is not installed (does not exist) on this computer."
fi # if INSTALL_TO exists
fi
done
fi
fi # neither "di-all.sh nor di-auto.sh"
done
END_TIME="$EPOCHSECONDS"
DIFF=$(($END_TIME - $START_TIME))
if (( $+commands[seconds2readable.sh] ))
then
ELAPSED_TIME=$(seconds2readable.sh "$DIFF")
else
ELAPSED_TIME="$DIFF seconds"
fi
log "Finished. ${ELAPSED_TIME}. Error count is ${COUNT}."
if [[ "IS_LAUNCHD" == "no" ]]
then
if [[ "$COUNT" != "0" ]]
then
growlnotify --sticky \
--appIcon "Console" \
--identifier "di-auto-errors" \
--message "$ELAPSED_TIME" \
--title "$NAME: With Errors ($COUNT)"
open -g -j -a Console "$LOG"
else
growlnotify \
--appIcon "Console" \
--identifier "di-auto-errors" \
--message "$ELAPSED_TIME" \
--title "$NAME: No Errors"
fi
fi
if (( $+commands[di-local.sh] ))
then
# if the command 'di-local.sh' is found in $PATH, run it.
# This is intended to allow users to have their own 'di-'
# scripts which are not part of the official repo
# but which are triggered as part of 'di-auto.sh'
log "Found 'di-local.sh'. Running it"
di-local.sh
fi
rm -f "$LOCKFILE"
if (( $+commands[macupdater.sh] ))
then
macupdater.sh
fi
exit 0