-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.bash
executable file
·419 lines (375 loc) · 9.51 KB
/
manifest.bash
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env bash
# shellcheck disable=1090
# shellcheck disable=2154
###################################
# Disclaimer: THIS IS AN ACTIVE EXPERIMENT. EXECUTE AT YOUR OWN PERIL.
###################################
# SECTION: News.
###################################
# Manifest -- a project dedicated to the paranoid.
# Manifest is written for and tested on Bash on OSX.
# Purpose: To generate and maintain 'manifests' of applications/gems/modules/packages/ports/etc.
## To mirror a user's environment.
# Execution: To be run either as a cron job or from a shell.
###################################
# SECTION: Requirements.
###################################
# Bash 4.2
# Pallet and Cask for Emacs 24
###################################
# SECTION: Variables.
###################################
checkRC () {
SOURCE="${BASH_SOURCE[-1]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
_CWD="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
#
[[ $BUILD == 'GO' ]] && return
if [[ -f "${_CWD}/${1}" ]]; then
printf '%s\n' "RC file found in current working directory."
. "${_CWD}/${1}"
return
elif [[ -f "${HOME}/${1}" ]]; then
printf '%s\n' "RC file found in home directory."
. "${HOME}/${1}"
return
else
printf '%s\n' 'Configuration file is required!'
fi
}
checkRC ".manifestrc"
source "${_CWD}/src/env"
source "${_CWD}/src/check-version"
source "${_CWD}/src/verify-path"
source "${_CWD}/src/aux"
#
#
notify () {
if ((messageStatus & verbosity)); then
msg="${1}${2}"
else
msg="${1}"
fi
if ((messageStatus & notify)); then
osascript -e "display notification \"$msg\" with title \"manifest \""
fi
if ((messageStatus & stdout)); then
printf '%s.\n' "${1}"
fi
if ((messageStatus & log)); then
echo "LOOOOL"
fi
}
notify=$((1<<0))
stdout=$((1<<1))
log=$((1<<2))
notify "balls"
messageHandler() {
n=$(($1*notify))
s=$(($2*stdout))
l=$(($3*log))
#
return $((n|s|l))
}
if ! checkVersion 42
then
printf '%s\n' "Your version of \`bash\` is too old :("
fi
messageHandler "${osxNote}" "${ttyNote}" "${logNote}"
messageStatus=1
###############################
missing () {
printf "I require %s, but it was not found. Aborting.\\n" "${1}" && exit 1;
exit 1
}
########
init () {
## terminal check
if [[ -t 0 || -t 1 ]]; then
notify "Running in Terminal."
_isTerm=1
else
notify 'Not-a-terminal.\n'
_isTerm=0
fi
##
##
[[ $BUILD == 'GO' ]] && return
verifyBase "${_BASE}"
}
# Reset log file. (user should have a choice in the matter)
#>"${_LOG}" # create/reset log, might be causing trouble with readline
###################################
# SECTION: Primary Functions.
###################################
# FUNCTION: Main
__manifest() {
notify "manifest core is running."
notify "That is all, ${_OWNER}! (^.^)"
}
#
# FUNCTION manifest of background services
__lunchy () {
type lunchy &>/dev/null && missing "lunchy (a ruby gem)"
saveFile "${USER}.background-services.txt" "`lunchy list`"
notify "Generating ${FUNCNAME[0]#__} manifest of ${_OWNER}..."
notify "${FUNCNAME[0]} is complete."
}
#
# composer global
__composer() {
# progress-bar 5
if ! type composer &>/dev/null
then
missing "${FUNCNAME[0]#__}"
fi
#
if [[ -z "${COMPOSER_HOME}" ]];then
COMPOSER_HOME="${HOME}/composer.json"
fi
if [[ -r "${COMPOSER_HOME}/composer.json" ]]; then
/bin/cp "${COMPOSER_HOME}/composer.json" "${_BASE}"
fi
}
# FUNCTION: Generate 'Gemfile' to use for reinstalling 'local' gems.
__gem() {
if [[ $1 == 'help' ]]; then
printf 'Create an execute-ready manifest of ruby gems.\n'
exit 0
fi
#
if ! type gem &>/dev/null
then
missing "${FUNCNAME[0]#__}"
fi
gemv=$(gem --version)
# need to properly parse output so that it can used on the command line
notify "Generating ${FUNCNAME[0]#__} manifest..."
#thanks to arturkomarov @ disqus
saveFile "local.gems.${gemv}.txt" \
"`gem list | sed 's/(//'|sed 's/)//'|awk '{print "gem install " $1 " --version=" $2}'`"
#
notify "${FUNCNAME[0]#__} is complete."
}
#
# FUNCTION: Generate a 'package.json' for NPM global modules.
__npm() {
if [[ $1 == 'help' ]]; then
printf 'Create a manifest of global npm modules.\n'
exit 0
fi
type npm &>/dev/null || { echo >&2 "I require npm but it's not installed. Aborting."; exit 1;}
#npmv=$(npm --version)
nodev=$(node --version)
notify "Generating ${FUNCNAME[0]#__} manifest..."
saveFile "npm-global.${nodev}.json" "`npm -g ls --json true --parseable true --depth=0`"
#
# continue
notify "${FUNCNAME[0]#__} is complete."
}
#
# FUNCTION: Emacs stuff.
__emacs() {
if [[ $1 == 'help' ]]; then
printf 'Emacs is the One True Editor.\n'
exit 0
fi
#test
type emacs &>/dev/null || { echo >&2 "I require emacs but it's not installed. Aborting."; exit 1;}
#
# generate cask file
emacs --batch --eval "(progn (package-initialize)
(if (bound-and-true-p pallet-mode)
(pallet-init)
(progn
(pallet-mode)
(pallet-init))))"
#
notify "Generating ${FUNCNAME[0]#__} (cask) manifest..."
cp -p "${HOME}/.emacs.d/Cask" "${_BASE}/cask.txt"
#
notify "${FUNCNAME[0]#__} is complete."
}
#
# Function: create system apps manifest.
__sysApps() {
if [[ $1 == 'help' ]]; then
printf 'Create a manifest of Applications.\n'
exit 0
fi
#
notify "Generating $(sw_vers -productVersion) manifest..."
saveFile "system.apps.txt" "`mdfind -onlyin /Applications/ kMDItemKind == Application | tee >(wc -l) | sort -f`"
notify "${FUNCNAME[0]} is complete."
}
#
getPYVersion() {
python << EOF
import sys
print "%s%s" % (sys.version_info[0] , sys.version_info[1])
EOF
}
pyv=$(getPYVersion)
__pip() {
if ! type pip &>/dev/null
then
missing "${FUNCNAME[0]#__}"
fi
notify "Generating ${FUNCNAME[0]#__} manifest..."
saveFile "pip.py${pyv}.txt" "`pip --isolated freeze`"
notify "done."
}
# # manifest of all installed macports ports
__port() {
if [[ $1 == 'help' ]]; then
printf 'Create a manifest of installed and requested Macports ports.\n'
exit 0
fi
if ! type port &>/dev/null
then
missing "${FUNCNAME[0]#__}"
fi
#
# Credit: Macports
notify "Generating ${FUNCNAME[0]#__} manifest..."
saveFile "system.macports.txt" "`port -qv installed`"
notify "done."
notify "Creating manifest of requested ports..."
#saveFile "system.macports-requested.txt" `port echo requested | cut -d ' ' -f 1"`
port echo requested | cut -d ' ' -f 1 > "${_BASE}/MANIFEST.system.macports-requested.txt"
#
notify "${FUNCNAME[0]#__} is complete."
}
#
__cron() {
# crontabs
notify "Replicating crontab of user ${_OWNER}"
crontab -l > "${_BASE}${USER}.crontab.txt"
#
notify "${FUNCNAME[0]#__} is complete."
}
#
#
getUser() {
/usr/bin/stat -f %Su "${BASH_SOURCE[0]}"
}
#
###################################
# SECTION: Auxilary Functions.
###################################
#
###################################
# SECTION: Execute.
###################################
# Menu functions
func_menu_help() {
BOLD=$(tput bold)
NORM=$(tput sgr0)
cat <<EOM
${BOLD}
manifest -- execute, in case of Doomsday.
A Bash program that generates parseable package manifests using the
syntax of respective package managers. All designed with love for the paranoid.${NORM}
-c: show config (RC) file
-f: list functions; takes optional argument
-i: interactively create a .manifestrc file
-q: quiet mode
-v: print version
-h: get help, seriously
EOM
}
#
#
#
init
#
#
while getopts acfinq:tvh FLAG; do
case $FLAG in
a) #set a
shift $((OPTIND-1))
echo win
exit 0
;;
c) #--config
shift $((OPTIND-1))
printf "%b" "This option is not ready. Stay tuned!"
exit 0
;;
f) #set option "f"
shift $((OPTIND-1))
if [[ -z "${1}" ]]; then
arr=(`compgen -A function | grep '__' | sed 's/__//g'`)
printf '%-14s STATUS\n' "COMMAND"
printf '%.0s-' {1..30}; echo
for tttt in "${arr[@]}"
do
if type -p "${tttt}" &>/dev/null
then
printf '%-14s found\n' "${tttt}"
else
printf '%-14s not found\n' "${tttt}"
fi
done
else
if ( compgen -A function "__${1}"); then
eval "__${1}" 'help'
else
printf 'Function %s not found.\n' "${1}"
fi
fi
exit 0
;;
i) #--init
printf '%s\n' "Initialize configuration file?"
exit 0
;;
n) # use osx notifications
shift $((OPTIND-1))
break
;;
q) # set 'q'
__manifest &> /dev/null &
exit 0
;;
t) # set 't'
# shift $((OPTIND-1))
#system=1
#__manifest
;;
v) #set option "v"
printf 'β\n'
exit 0
;;
h) #show help
func_menu_help
exit 0
;;
\?) #unrecognized option - show help
printf 'Unknown option.\n'
printf 'Use -h for helpful info.\n'
exit 2
;;
esac
done
#
for tryFunction in "${@}"
do
if type -t "__$tryFunction" >/dev/null; then
"__$tryFunction" false
shift
else
printf 'Invalid choice.\n'
exit 1
fi
done
#
if [[ $BUILD == 'GO' ]]
then
_CWD="${_CWD}"
install -b -m 755 /dev/null "${_CWD}/dist/manifest"; while read -ra words; do if [[ ${words[0]} == source ]]; then eval cat "${words[1]}"; else printf '%s\n' "${words[*]}"; fi; done < "${0}" >| "${_CWD}/dist/manifest"
fi