forked from matthewmueller/dots
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmacstrap.sh
executable file
·146 lines (126 loc) · 3.19 KB
/
macstrap.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
#!/bin/sh
set -e
# macstrap main
main() {
# load the version
macstrapInstallFolder="$(dirname "$(realpath "$0")")"
. "$macstrapInstallFolder/version.sh"
. "$macstrapInstallFolder/utils.sh"
# global variables
export macstrapVersion="$version"
export macstrapConfigFolder="$HOME/.macstrap"
export macstrapConfigFile="$macstrapConfigFolder/macstrap.cfg"
if [ $# -eq 0 ]; then
# if no argument is present show the usage
usage
exit
else
# run command
case $1 in
-v | --version )
echo "macstrap $version"
exit
;;
-h | --help )
usage
exit
;;
install )
executeCommand "install"
exit
;;
update )
executeCommand "update"
exit
;;
"update-macstrap" )
updatemacstrap
exit
;;
*)
usage
exit
;;
esac
fi
}
# usage info
usage() {
. "$macstrapInstallFolder/banner.sh"
cat <<EOF
Usage: macstrap [options] [command] [args ...]
Options:
-v, --version Get the version
-h, --help This message
Commands:
install Install configured apps and binaries
update Update configured apps and binaries
update-macstrap Update macstrap to the latest version
EOF
}
# executes a command with pre and post hooks
executeCommand() {
executeCustomScripts "pre-$1-"
executeScript "$macstrapConfigFolder/commands/$1.sh"
executeCustomScripts "post-$1-"
printf "Finished executing command \033[1m%s\033[0m\n" "$1"
echo
}
# executes the custom scripts with the specified prefix
executeCustomScripts() {
for file in "$macstrapConfigFolder/custom-scripts/$1"*.sh
do
if [ -f "$file" ]; then
executeScript "$file"
fi
done
}
# executes a script or does nothing if it does not exist
executeScript() {
if [ -e "$1" ]; then
echo
printf "Executing \033[1m%s\033[0m ...\n" "$1"
echo
. "$1"
echo
printf "Finished executing \033[1m%s\033[0m\n" "$1"
echo
else
printf "Script \033[1m%s\033[0m does not exists. Doing nothing.\n" "$1"
fi
}
# update macstrap via git
updatemacstrap() {
echo
echo "Before upgrading macstrap to the latest version please check that your configured configuration repository is already migrated.";
echo "Please check the update documentation on https://github.com/guylabs/macstrap on how to do that."
echo
printf "Press enter to continue updating macstrap..."
read -r
currentFolder=$(pwd)
mkdir -p /tmp/macstrap \
&& cd /tmp/macstrap \
&& curl -L https://github.com/guylabs/macstrap/archive/master.tar.gz | tar zx --strip 1 \
&& bash ./install.sh \
&& cd "$currentFolder" \
&& rm -rf /tmp/macstrap \
&& echo "Updated macstrap from version $version to $(macstrap --version)"
exit
}
# "readlink -f" shim for Mac OS X
realpath() {
target=$1
cd "$(dirname "$target")"
target="$(basename "$target")"
# iterate down a (possible) chain of symlinks
while [ -L "$target" ]
do
target="$(readlink "$target")"
cd "$(dirname "$target")"
target="$(basename "$target")"
done
dir="$(pwd -P)"
echo "$dir/$target"
}
# call main
main "$@"