-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsetup.sh
196 lines (162 loc) · 5.38 KB
/
setup.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
#!/usr/bin/env bash
# Automated Mac Setup Script - Updated Nov 2023
# This script installs essential command-line tools and applications using Homebrew.
# Enable strict mode for safety
set -euo pipefail
# Log the start of the script execution
LOGFILE="$HOME/mac_setup_$(date +'%Y%m%d_%H%M%S').log"
exec > >(tee -a "$LOGFILE") 2>&1
echo "[$(date)] Starting Mac setup..."
# Function to keep sudo active
keep_sudo_active() {
sudo -v
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
}
# Request and keep the administrator password active.
keep_sudo_active
# Function to check the system processor
detect_architecture() {
ARCH=$(uname -m)
if [[ "$ARCH" == "arm64" ]]; then
echo "[$(date)] M1/M2/M3 Processor detected. Proceeding with compatible installations."
else
echo "[$(date)] Intel Processor detected. Proceeding with installations."
fi
}
detect_architecture
# Function to install Homebrew if not already installed
install_homebrew() {
echo "[$(date)] Checking for Homebrew..."
if ! command -v brew >/dev/null 2>&1; then
echo "[$(date)] Installing Homebrew..."
retry /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add Homebrew to PATH
echo "[$(date)] Adding Homebrew to PATH..."
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
else
echo "[$(date)] Homebrew already installed."
fi
}
# Retry logic for network-related commands
retry() {
local n=0
local try=5
local cmd="$@"
until [ $n -ge $try ]
do
$cmd && break
n=$((n+1))
echo "Retry $n/$try failed for: $cmd"
sleep 5
done
}
install_homebrew
# Update and Upgrade Homebrew: Ensure Homebrew is up-to-date.
update_homebrew() {
echo "[$(date)] Updating and Upgrading Homebrew..."
brew update
brew upgrade
}
update_homebrew
# Function to install XCode Command Line Tools
install_xcode_tools() {
echo "[$(date)] Checking for Xcode Command Line Tools..."
if ! xcode-select -p >/dev/null 2>&1; then
echo "[$(date)] Installing Xcode Command Line Tools..."
xcode-select --install
else
echo "[$(date)] Xcode Command Line Tools already installed."
fi
}
install_xcode_tools
# Finder Configuration: Set up Finder preferences like showing hidden files.
configure_finder() {
echo "[$(date)] Configuring Finder settings..."
chflags nohidden ~/Library
defaults write com.apple.finder AppleShowAllFiles YES
defaults write com.apple.finder ShowPathbar -bool true
defaults write com.apple.finder ShowStatusBar -bool true
osascript -e 'tell application "Finder" to quit'
osascript -e 'tell application "Finder" to launch'
}
configure_finder
# Terminal and Shell Setup: Install iTerm2 and Oh My Zsh.
install_terminal_tools() {
echo "[$(date)] Installing iTerm2..."
brew install --cask --appdir="/Applications" iterm2
echo "[$(date)] Installing oh-my-zsh..."
RUNZSH=no sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# Backup existing .zshrc
backup_file ~/.zshrc
# Configure .zshrc for Oh My Zsh
echo "[$(date)] Configuring .zshrc for Oh My Zsh..."
sed -i '' 's/^ZSH_THEME=".*"$/ZSH_THEME="agnoster"/' ~/.zshrc
sed -i '' 's/^plugins=(.*)$/plugins=(brew macos)/' ~/.zshrc
echo 'ZSH_DISABLE_COMPFIX="true"' >> ~/.zshrc
echo '# Disabling compfix to prevent the "insecure directories" warning when starting zsh' >> ~/.zshrc
cat << 'EOF' >> ~/.zshrc
# Add my custom functions and settings here.
preexec() {
timer=$(gdate +%s.%N)
}
precmd() {
if [ -n "$timer" ]; then
now=$(gdate +%s.%N)
elapsed=$(echo "$now - $timer" | bc)
timer_show=$(printf "%.2f" $elapsed)
echo "Execution time: ${timer_show}s"
unset timer
fi
}
EOF
}
backup_file() {
local file="$1"
if [[ -f "$file" ]]; then
cp "$file" "${file}.bak_$(date +'%Y%m%d_%H%M%S')"
echo "[$(date)] Backed up $file to ${file}.bak_$(date +'%Y%m%d_%H%M%S')"
fi
}
install_terminal_tools
# Powerline Fonts Installation: Clone and install Powerline fonts.
install_powerline_fonts() {
echo "[$(date)] Installing Powerline fonts..."
if [ ! -d "$HOME/fonts" ]; then
retry git clone https://github.com/powerline/fonts.git "$HOME/fonts"
pushd "$HOME/fonts" && ./install.sh && popd
else
echo "[$(date)] Powerline fonts already installed."
fi
}
install_powerline_fonts
# Python and pip Installation: Install Python and pip (pip is included with Python).
install_python() {
echo "[$(date)] Checking for Python..."
if ! command -v python3 >/dev/null 2>&1; then
echo "[$(date)] Installing Python..."
brew install python
else
echo "[$(date)] Python already installed."
fi
}
install_python
# Core Applications Installation: Install essential applications using Homebrew.
install_core_apps() {
echo "[$(date)] Installing core applications..."
brew install --cask --appdir="/Applications" alfred &
brew install --cask --appdir="/Applications" visual-studio-code &
brew install --cask --appdir="/Applications" slack &
brew install --cask --appdir="/Applications" 1password &
wait
}
install_core_apps
# Clean up: Remove outdated versions from the cellar.
cleanup_homebrew() {
echo "[$(date)] Running brew cleanup..."
brew cleanup
}
cleanup_homebrew
# Ensure successful completion
echo "[$(date)] Mac setup script completed successfully."
exit 0