-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_install.sh
175 lines (146 loc) · 4.58 KB
/
new_install.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
#!/bin/bash
# **************************************************************************** #
# #
# ::: :::::::: #
# new_install.sh :+: :+: :+: #
# +:+ +:+ +:+ #
# By: pibouill <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/12/08 13:41:49 by pibouill #+# #+# #
# Updated: 2024/12/08 13:42:44 by pibouill ### ########.fr #
# #
# **************************************************************************** #
# Detect the operating system and check for specific hostname
OS="$(uname -s)"
HOSTNAME="$(hostname)"
# Color codes for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # end color
# Check if running on 42prague
is_42prague() {
echo "$HOSTNAME" | grep -q "42prague"
return $?
}
# Packages to install
COMMON_PACKAGES=(
"git"
"wget"
"curl"
"vim"
"nvim"
"tmux"
"htop"
"tree"
"lua"
"luarocks"
"python3"
"nodejs"
"go"
"python3"
"ripgrep"
)
LINUX_PACKAGES=(
)
# Function to check if a package is installed
is_package_installed() {
local package="$1"
if command -v brew &> /dev/null; then
brew list "$package" &> /dev/null
elif command -v "$package" &> /dev/null; then
return 0
fi
return 1
}
# Function to install a package
install_package() {
local package="$1"
echo -e "${YELLOW}Installing $package...${NC}"
if ! is_package_installed "$package"; then
if command -v brew &> /dev/null; then
brew install "$package"
elif command -v apt &> /dev/null && [ -w /var/lib/dpkg ]; then
sudo apt-get install -y "$package"
elif command -v apt &> /dev/null; then
echo -e "${RED}Cannot install $package: insufficient permissions${NC}"
return 1
fi
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ Installed $package${NC}"
else
echo -e "${RED}✗ Failed to install $package${NC}"
fi
else
echo -e "${GREEN}✓ $package is already installed${NC}"
fi
}
# Function to install Homebrew
install_homebrew() {
if command -v brew &> /dev/null; then
echo -e "${GREEN}Homebrew is already installed${NC}"
return 0
fi
echo -e "${YELLOW}Installing Homebrew...${NC}"
# Determine Homebrew installation directory
if is_42prague; then
# 42prague specific location
echo -e "\n\n${GREEN}42Prague detected${NC}"
HOMEBREW_DIR="$HOME/sgoinfre/homebrew"
else
# User-space installation
HOMEBREW_DIR="$HOME/.homebrew"
fi
# Create Homebrew directory
mkdir -p "$HOMEBREW_DIR"
# Download and install Homebrew
git clone https://github.com/Homebrew/brew.git "$HOMEBREW_DIR"
# Add Homebrew to PATH
export PATH="$HOMEBREW_DIR/bin:$PATH"
# Persist Homebrew in shell configuration
echo "export PATH=\"$HOMEBREW_DIR/bin:\$PATH\"" >> ~/.bashrc
echo "export HOMEBREW_PREFIX=\"$HOMEBREW_DIR\"" >> ~/.bashrc
# Source the updated configuration
source ~/.zshrc
# Verify installation
if command -v brew &> /dev/null; then
echo -e "${GREEN}✓ Homebrew installed successfully${NC}"
else
echo -e "${RED}✗ Failed to install Homebrew${NC}"
return 1
fi
}
# Main installation function
install_packages() {
# Install common packages
for package in "${COMMON_PACKAGES[@]}"; do
install_package "$package"
done
# Install Linux-specific packages
for package in "${LINUX_PACKAGES[@]}"; do
install_package "$package"
done
}
# Main script execution
main() {
# Error check for root user
if [[ $EUID -eq 0 ]]; then
echo -e "${RED}Error: Do not run this script as root${NC}"
exit 1
fi
echo -e "${GREEN}Detected OS: $OS${NC}"
echo -e "${GREEN}Hostname: $HOSTNAME${NC}"
# Install Homebrew
install_homebrew
# Install packages
install_packages
# Update packages
if command -v brew &> /dev/null; then
echo -e "${YELLOW}Updating packages...${NC}"
brew update
brew upgrade
fi
echo -e "${GREEN}✓ Installation complete!${NC}"
}
# Run the main function
main