-
Notifications
You must be signed in to change notification settings - Fork 6
/
bootstrap
executable file
·218 lines (189 loc) · 7.34 KB
/
bootstrap
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
#! /bin/bash
# Copyright © 2017 Calum MacRae
# Use of this source code is governed by an ISC
# license that can be found in the LICENSE file.
#######################################
# A bootstrapper to set-up Save macOS #
#######################################
# Usage
usage(){
cat <<EOF
Usage: bootstrap [Options]
Options:
-h, --help: Show this message
--with-brew: Allow installs along side Homebrew
--with-ports: Allow installs along side MacPorts
EOF
}
# Sanity checks
sanity(){
# Ensure the user is running this script on macOS
if [ $(uname -s) != "Darwin" ]; then
echo "This script is for use with macOS!"
exit 1
fi
# Ensure script is not being run with root privileges
if [ $EUID -eq 0 ]; then
echo "Please don't run this script with root priveleges!"
exit 1
fi
# Check for Homebrew install
if [[ -f /usr/local/bin/brew && ! $ALLOW_BREW ]]; then
echo -e ""$RED"It looks like you have Homebrew installed. This could seriously conflict with pkgsrc!"
echo -e "As we don't wish to affect what you may have implemented, we'll leave it up to you to remove it."$ESC""
echo
echo "Alternatively, to install along side Homebrew; use the '--with-brew' option."
exit 1
fi
# Check for MacPorts install
if [[ -f /opt/local/bin/port && ! $ALLOW_PORTS ]]; then
echo -e ""$RED"It looks like you have MacPorts installed. This could seriously conflict with pkgsrc!"
echo -e "As we don't wish to affect what you may have implemented, we'll leave it up to you to remove it."$ESC""
echo
echo "Alternatively, to install along side MacPorts; use the '--with-ports' option."
exit 1
fi
}
# Safe run function to ensure commands are executed successfully
saferun() {
typeset cmnd="$*"
typeset ret_code
eval $cmnd
ret_code=$?
if [ $ret_code != 0 ]; then
echo -e ""$RED_UL"\n\nIt looks like there was an issue running: $*"$ESC"\n\nExiting..."
exit $?
fi
}
install() {
# pkgsrc site/bootstrap properties
local PKGSRC_SITE="https://pkgsrc.joyent.com/packages/Darwin"
local PKGSRC_QUARTER="trunk"
local BOOTSTRAP_TRUNK_RELEASE="20170205"
local BOOTSTRAP_URL="${PKGSRC_SITE}/bootstrap/"
local BOOTSTRAP_TAR="bootstrap-${PKGSRC_QUARTER}-x86_64-${BOOTSTRAP_TRUNK_RELEASE}.tar.gz"
local BOOTSTRAP_PATH="/tmp/${BOOTSTRAP_TAR}"
local BOOTSTRAP_SHA="0b44c8e86e5083a9f151a448b12b718e04d6deed9bd6be61a792023b7a5513b4"
local REPO_GPGKEY="0xDE817B8E"
echo -e "\n"$BLUE_UL"Welcome to Save macOS!"$ESC""
echo
echo "Welcome to the Save macOS project, an effort to make macOS"
echo "a more comfortable system for hackers, developers & power users"
echo "by providing fast, secure, 64bit binary package management using"
echo -e "the excellent "$YELLOW_UL"pkgsrc"$ESC" from the NetBSD project!"
echo
echo "This script will set up your environment for you. It will automatically"
echo "install pkgsrc & its associated binary package manager - pkgin, with signed"
echo "packages, and set up your path environments so binaries and man-pages can"
echo "be found."
echo
echo "Please enter your password for sudo authentication"
# Prompt for initial sudo password & keep alive
sudo -k
saferun sudo echo "sudo authenticaion successful!"
while true ; do sudo -n true ; sleep 60 ; kill -0 "$$" || exit ; done 2>/dev/null &
# Download the bootstrap, if not already there
if [ ! -f $BOOTSTRAP_PATH ]; then
echo -e ""$YELLOW"Fetching pkgsrc bootstrap from Joyent's pkgsrc repo..."$ESC""
saferun curl -o $BOOTSTRAP_PATH "${BOOTSTRAP_URL}/${BOOTSTRAP_TAR}"
fi
# Verify the bootstrap integrity
echo "Verifying bootstrap integrity..."
echo "$BOOTSTRAP_SHA $BOOTSTRAP_PATH" | shasum -a 256 -c - || exit 1
# extract the pkgsrc bootstrap in the root directory
echo -e "pkgsrc retrieved successfully, proceeding to install..."
saferun sudo tar xfP $BOOTSTRAP_PATH -C / &> /dev/null
echo -e ""$GREEN"pkgsrc installed."$ESC""
echo
echo -e ""$YELLOW"Updating pkgin repository information..."$ESC""
saferun sudo /opt/pkg/bin/pkgin -y update # Update the pkgsrc repository information
echo "Package database updated!"
echo -e "\n"$YELLOW"Setting up GPG for signed packages..."$ESC""
if [ ! $(command -v gpg &> /dev/null) ]; then
echo "It doesn't look like GPG is installed"
echo "Installing gnupg from Joyent's repo"
saferun sudo /opt/pkg/bin/pkgin -y in gnupg
export PATH=$PATH:/opt/pkg/sbin:/opt/pkg/bin
fi
echo -e ""$YELLOW"Importing Joyent's repo GPG key..."$ESC""
saferun gpg --keyserver hkp://keys.gnupg.net --recv-keys $REPO_GPGKEY &> /dev/null
echo -e ""$BLUE"\nImported Joyent's repo GPG key successfully!"$ESC""
echo -e "Verifying repo's GPG integrity..."
saferun curl -s -o "${BOOTSTRAP_PATH}.asc" "${BOOTSTRAP_URL}/${BOOTSTRAP_TAR}.asc"
saferun gpg --verify "${BOOTSTRAP_PATH}.asc" &> /dev/null
echo -e ""$BLUE"Looks good!"$ESC""
# Add binary dirs to system-wide path
echo
echo -e ""$YELLOW"Updating \$PATH so new binaries can be found..."$ESC""
if [ ! -f /etc/paths.d/pkgsrc ]; then
echo -e "/opt/pkg/bin\n/opt/pkg/sbin" | sudo tee -a /etc/paths.d/pkgsrc > /dev/null
fi
echo -e "Updating \$MANPATH so new manpages can be found..."
if [ ! -f /etc/manpaths.d/pkgsrc ]; then
echo -e "MANPATH /opt/pkg/man\nMANPATH /opt/pkg/share/man" | sudo tee -a /etc/manpaths.d/pkgsrc > /dev/null
fi
# Set up PATH eval in user's shellrc
SHELLRC=~/.$(echo $SHELL | awk -F/ '{print$NF}')rc
if [ -f $SHELLRC ]; then
echo "It looks like your shell's rc file is $SHELLRC"
else
echo -e "It doesn't look like $SHELLRC exists...\nA new shell rc will be created\n"
fi
# Update user's shell rc file with new $PATH
if [[ ! $(fgrep "path_helper" $SHELLRC) ]]; then
echo "Adding proper PATH evaluation to $SHELLRC"
echo -e "\n# Evaluate system PATH\nif [ -x /usr/libexec/path_helper ]; then\n eval \`/usr/libexec/path_helper -s\`\nfi" >> $SHELLRC
else
echo "It looks like you already have proper PATH evaluation in $SHELLRC"
fi
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
echo -e "Your system's \$PATH has been updated"
# Final screen!
echo -e "\n"$GREEN_UL"Hurrah! You Saved macOS!"$ESC""
echo
echo "All processes are complete! So what now? Well, you should probably read the"
echo "manpage for pkgin at least, as you'll be managing all your non-Apple software"
echo "with this excellent package manager."
echo
echo -e ""$YELLOW"Open a new shell to get started."$ESC""
echo
echo -e ""$GREEN"Thanks for using Save macOS!"$ESC""
exit 0
}
main(){
# ANSI properties/colours
local ESC='\033[0m'
local BLUE='\033[38;34m'
local BLUE_UL='\033[38;4;34m'
local GREEN='\033[38;32m'
local GREEN_UL='\033[38;4;32m'
local RED='\033[38;31m'
local RED_UL='\033[38;4;31m'
local YELLOW='\033[38;33m'
local YELLOW_UL='\033[38;4;33m'
if [ $# -gt 0 ]; then
case "$1" in
-h|--help)
usage
exit 3
;;
--with-brew)
export ALLOW_BREW=true
;;
--with-ports)
export ALLOW_PORTS=true
;;
*)
echo -e "ERROR: Unrecognized parameter: '${1}'\nSee usage (--help) for options..." 1>&2
exit 2
;;
esac
fi
clear
sanity
install
}
# Actual run
main $@