-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
Β·183 lines (150 loc) Β· 4.96 KB
/
build.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
#!/bin/bash
##############################################
# Dotfiles Setup Script
# Author: PunGrumpy
# Description: This script is used to setup dotfiles on a new machine. (macOS/Linux)
# Usage: bash build.sh
# GitHub: https://github.com/PunGrumpy/dotfiles
# License: MIT
##############################################
##############################################
# COLORS
##############################################
RED='\033[0;31m'
GREEN='\033[0;32m'
RESET='\033[0m'
##############################################
# VARIABLES
##############################################
OS=$(uname -s)
DOTFILES_PATH="$HOME/.dotfiles"
DOTFILES_URL="https://github.com/PunGrumpy/dotfiles.git"
HOMEBREW_URL="https://raw.githubusercontent.com/Homebrew/install/master/install.sh"
SHELL_CONFIG="fish" # Default shell configuration
##############################################
# FUNCTIONS
##############################################
# Function to print messages in color
print_message() {
local color=$1
local message=$2
echo -e "${color}${message}${RESET}"
}
# Function to welcome user
welcome_user() {
print_message "${GREEN}" "π Welcome ${USER} to dotfiles setup script."
}
# Function to clear screen
clear_screen() {
sleep 1
clear
sleep 1
}
# Function to check if a command is available
check_command() {
command -v "$1" >/dev/null 2>&1
}
# Function to handle errors
handle_error() {
local message="$1"
print_message "${RED}" "Error: $message"
exit 1
}
# Function to validate shell configuration input
validate_shell_config() {
local shell_config="$1"
if [[ "$shell_config" != "bash" && "$shell_config" != "fish" ]]; then
handle_error "Invalid shell configuration. Please enter 'bash' or 'fish'."
fi
}
# Function to install dotfiles
install_dotfiles() {
if [ -z "$DOTFILES_URL" ]; then
handle_error "Dotfiles URL is not provided."
fi
if [ -d "$DOTFILES_PATH" ]; then
print_message "${RED}" "β οΈ Dotfiles path already exists."
read -p "Do you want to remove the existing dotfiles path? (y/n): " input_remove
if [ "${input_remove,,}" == "y" ]; then
print_message "${GREEN}" "π Removing dotfiles path..."
rm -rf "$DOTFILES_PATH" || handle_error "Failed to remove dotfiles."
print_message "${GREEN}" "π Dotfiles path removed successfully."
else
print_message "${RED}" "β οΈ Dotfiles path not removed."
fi
fi
print_message "${GREEN}" "π Cloning dotfiles..."
git clone "$DOTFILES_URL" "$DOTFILES_PATH" || handle_error "Failed to clone dotfiles."
print_message "${GREEN}" "π Dotfiles cloned successfully."
}
# Function to symlink dotfiles
symlink_dotfiles() {
local dotfiles=($(find "$DOTFILES_PATH" -maxdepth 1 -name '.*' -type f))
if [ ${#dotfiles[@]} -eq 0 ]; then
handle_error "No dotfiles found in '$DOTFILES_PATH'."
fi
for file in "${dotfiles[@]}"; do
if [ "$(basename "$file")" == ".git" ]; then
continue
fi
local filename=$(basename "$file")
ln -sf "$file" "$HOME/$filename"
done
ln -sf "$DOTFILES_PATH/.config" "$HOME"
ln -sf "$DOTFILES_PATH/.scripts" "$HOME"
print_message "${GREEN}" "π Creating symlinks... Done."
}
# Function to install Homebrew
install_homebrew() {
print_message "${GREEN}" "πΊ Getting Homebrew..."
/bin/bash -c "$(curl -fsSL $HOMEBREW_URL)" || handle_error "Failed to install Homebrew."
if [ "$OS" == "Darwin" ]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
else
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
fi
print_message "${GREEN}" "πΊ Homebrew installed successfully."
}
# Function to install brew bundle
install_brew_bundle() {
print_message "${GREEN}" "π¦ Installing Brewfile..."
if [ -f "$DOTFILES_PATH/Brewfile" ]; then
brew bundle --file="$DOTFILES_PATH/Brewfile" || handle_error "Failed to install Brewfile."
print_message "${GREEN}" "π¦ Brewfile installed successfully."
else
print_message "${RED}" "β οΈ Brewfile not found."
fi
}
##############################################
# MAIN SCRIPT
##############################################
# Clear screen
clear_screen
# Welcome user
welcome_user
# Clear screen
clear_screen
# Ask for dotfiles URL
read -p "Enter the URL for dotfiles repository (default: $DOTFILES_URL): " input_url
DOTFILES_URL=${input_url:-$DOTFILES_URL}
# Ask for shell configuration
read -p "Enter the shell configuration (bash/fish): " input_shell
SHELL_CONFIG=${input_shell:-$SHELL_CONFIG}
# Validate shell configuration input
validate_shell_config "$SHELL_CONFIG"
# Install dotfiles
install_dotfiles
# Symlink dotfiles
symlink_dotfiles
# Check dependencies
print_message "${GREEN}" "π Checking dependencies..."
check_command git || handle_error "Git is not installed."
check_command curl || handle_error "Curl is not installed."
# Install Homebrew
install_homebrew
# Install Brewfile
install_brew_bundle
# Installation completed
print_message "${GREEN}" "π Installation completed."
print_message "${GREEN}" "π Please install Fisher manually for Fish shell configuration."
print_message "${GREEN}" "π Please restart your terminal to apply changes."