-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·412 lines (349 loc) · 11.4 KB
/
install.sh
File metadata and controls
executable file
·412 lines (349 loc) · 11.4 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/bin/bash
# install.sh - Smart installation script with PREFIX detection
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
# Smart PREFIX detection
# Priority: 1. Command line arg, 2. Environment variable, 3. Default
if [ -n "$1" ]; then
INSTALL_PREFIX="$1"
elif [ -n "$PREFIX" ]; then
INSTALL_PREFIX="$PREFIX"
else
INSTALL_PREFIX="/usr/local"
fi
REPO_URL="https://github.com/mahbubhs/gitignore"
BINARY_NAME="gitignore"
INSTALL_DIR="$INSTALL_PREFIX/bin"
MANDIR="$INSTALL_PREFIX/share/man/man1"
VERSION="2.0.0"
print_banner() {
echo -e "${CYAN}${BOLD}"
echo "╔════════════════════════════════════════╗"
echo "║ ║"
echo "║ GITIGNORE TOOL INSTALLER v2.0 ║"
echo "║ ║"
echo "╚════════════════════════════════════════╝"
echo -e "${NC}"
}
print_success() {
echo -e "${GREEN}✓${NC} $1"
}
print_error() {
echo -e "${RED}✗${NC} $1"
}
print_warning() {
echo -e "${YELLOW}⚠${NC} $1"
}
print_info() {
echo -e "${BLUE}ℹ${NC} $1"
}
show_prefix_info() {
echo -e "${CYAN}${BOLD}Installation Configuration:${NC}"
echo -e " PREFIX: ${BOLD}$INSTALL_PREFIX${NC}"
echo -e " Binary: ${BOLD}$INSTALL_DIR/$BINARY_NAME${NC}"
echo -e " Man page: ${BOLD}$MANDIR/gitignore.1${NC}"
echo ""
if [ "$INSTALL_PREFIX" != "/usr/local" ]; then
print_warning "Using custom PREFIX: $INSTALL_PREFIX"
echo ""
fi
}
check_requirements() {
print_info "Checking requirements..."
local missing_deps=()
if ! command -v gcc &> /dev/null && ! command -v clang &> /dev/null; then
missing_deps+=("gcc or clang")
fi
if ! command -v make &> /dev/null; then
missing_deps+=("make")
fi
if ! command -v curl &> /dev/null; then
missing_deps+=("curl")
fi
if ! ldconfig -p 2>/dev/null | grep -q libcurl; then
missing_deps+=("libcurl-dev")
fi
if [ ${#missing_deps[@]} -gt 0 ]; then
print_error "Missing dependencies: ${missing_deps[*]}"
echo ""
print_info "Install missing dependencies:"
echo ""
if [ -f /etc/debian_version ]; then
echo " ${BOLD}sudo apt-get update${NC}"
echo " ${BOLD}sudo apt-get install -y build-essential libcurl4-openssl-dev${NC}"
elif [ -f /etc/redhat-release ]; then
echo " ${BOLD}sudo dnf install -y gcc make libcurl-devel${NC}"
elif [ "$(uname)" == "Darwin" ]; then
echo " ${BOLD}brew install curl${NC}"
else
echo " Please install: ${missing_deps[*]}"
fi
echo ""
read -p "Would you like to continue anyway? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
else
print_success "All requirements met"
fi
}
detect_os() {
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
VER=$VERSION_ID
elif [ "$(uname)" == "Darwin" ]; then
OS="macos"
VER=$(sw_vers -productVersion)
else
OS=$(uname -s)
VER=$(uname -r)
fi
print_info "Detected OS: $OS $VER"
}
clone_or_download() {
print_info "Downloading source code..."
local temp_dir=$(mktemp -d)
cd "$temp_dir"
if command -v git &> /dev/null; then
print_info "Cloning from GitHub..."
if git clone --depth 1 "$REPO_URL.git" gitignore 2>/dev/null; then
cd gitignore
print_success "Repository cloned"
return 0
else
print_warning "Git clone failed, trying alternative method..."
fi
fi
print_info "Downloading tarball..."
if curl -sSL "$REPO_URL/archive/refs/heads/main.tar.gz" -o gitignore.tar.gz; then
tar -xzf gitignore.tar.gz
cd gitignore-main 2>/dev/null || cd gitignore-master 2>/dev/null || {
print_error "Failed to extract archive"
exit 1
}
print_success "Source downloaded"
else
print_error "Failed to download source code"
print_info "Please visit: $REPO_URL"
exit 1
fi
}
build_from_source() {
print_info "Building from source..."
# Generate templates
if make templates &> build.log; then
print_success "Templates generated"
else
print_error "Failed to generate templates"
cat build.log
exit 1
fi
# Build with PREFIX
if make PREFIX="$INSTALL_PREFIX" >> build.log 2>&1; then
print_success "Build successful"
else
print_error "Build failed"
cat build.log
exit 1
fi
}
install_binary() {
print_info "Installing binary..."
# Check if we need sudo
if [ -w "$INSTALL_DIR" ]; then
SUDO=""
else
SUDO="sudo"
print_warning "Requires sudo privileges for installation to $INSTALL_PREFIX"
fi
# Install with PREFIX
if $SUDO make PREFIX="$INSTALL_PREFIX" install &> /dev/null; then
print_success "Installed to $INSTALL_DIR/$BINARY_NAME"
else
print_error "Installation failed"
# Try alternative installation
print_info "Trying alternative installation method..."
$SUDO install -d "$INSTALL_DIR"
if $SUDO cp "$BINARY_NAME" "$INSTALL_DIR/"; then
$SUDO chmod +x "$INSTALL_DIR/$BINARY_NAME"
print_success "Binary installed manually"
# Try to install man page
if [ -f man/gitignore.1 ]; then
$SUDO install -d "$MANDIR"
$SUDO cp man/gitignore.1 "$MANDIR/"
print_success "Man page installed"
fi
else
print_error "Could not install binary"
exit 1
fi
fi
}
update_path() {
# Check if INSTALL_DIR is in PATH
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
print_warning "Installation directory not in PATH"
echo ""
print_info "Add to your PATH by adding this to ~/.bashrc or ~/.zshrc:"
echo ""
echo " ${BOLD}export PATH=\"$INSTALL_DIR:\$PATH\"${NC}"
echo ""
read -p "Would you like to add it now? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
local shell_rc="$HOME/.bashrc"
if [ -f "$HOME/.zshrc" ]; then
shell_rc="$HOME/.zshrc"
fi
echo "" >> "$shell_rc"
echo "# Added by gitignore installer" >> "$shell_rc"
echo "export PATH=\"$INSTALL_DIR:\$PATH\"" >> "$shell_rc"
print_success "Added to $shell_rc"
print_info "Restart your shell or run: source $shell_rc"
fi
fi
}
setup_config() {
print_info "Setting up configuration..."
local config_dir="$HOME/.config/gitignore"
mkdir -p "$config_dir"/{templates,cache,backups}
if [ ! -f "$config_dir/config.conf" ]; then
cat > "$config_dir/config.conf" << 'EOF'
# gitignore configuration file
auto_backup=true
cache_enabled=true
cache_duration=86400
verbose=false
use_color=true
EOF
print_success "Configuration created"
else
print_info "Configuration already exists, skipping"
fi
if [ ! -f "$config_dir/templates/auto.gitignore" ]; then
cat > "$config_dir/templates/auto.gitignore" << 'EOF'
# Auto template
.DS_Store
*.log
*.tmp
.env
node_modules/
__pycache__/
EOF
print_success "Auto template created"
fi
}
verify_installation() {
print_info "Verifying installation..."
if command -v "$BINARY_NAME" &> /dev/null; then
local installed_version=$($BINARY_NAME --version | head -1 | awk '{print $NF}')
print_success "Installation verified"
print_info "Installed version: $installed_version"
if $BINARY_NAME list &> /dev/null; then
print_success "Basic functionality test passed"
else
print_warning "Basic functionality test failed"
fi
else
print_error "Verification failed - command not found"
print_info "Binary location: $INSTALL_DIR/$BINARY_NAME"
update_path
fi
}
show_completion() {
echo ""
echo -e "${GREEN}${BOLD}════════════════════════════════════════${NC}"
echo -e "${GREEN}${BOLD} Installation Complete! 🎉${NC}"
echo -e "${GREEN}${BOLD}════════════════════════════════════════${NC}"
echo ""
echo "Installation details:"
echo " PREFIX: ${BOLD}$INSTALL_PREFIX${NC}"
echo " Binary: ${BOLD}$INSTALL_DIR/$BINARY_NAME${NC}"
echo " Man page: ${BOLD}$MANDIR/gitignore.1${NC}"
echo ""
echo "Quick start:"
echo " ${BOLD}gitignore --help${NC} # Show help"
echo " ${BOLD}gitignore auto${NC} # Auto-detect project"
echo " ${BOLD}gitignore init python${NC} # Create Python .gitignore"
echo " ${BOLD}gitignore -t${NC} # Interactive mode"
echo ""
echo "Configuration:"
echo " ${CYAN}$HOME/.config/gitignore/${NC}"
echo ""
echo "Documentation:"
echo " ${BLUE}$REPO_URL${NC}"
echo ""
}
uninstall() {
print_info "Uninstalling gitignore from $INSTALL_PREFIX..."
if [ -w "$INSTALL_DIR" ]; then
SUDO=""
else
SUDO="sudo"
fi
if $SUDO rm -f "$INSTALL_DIR/$BINARY_NAME"; then
print_success "Binary removed from $INSTALL_DIR"
fi
if $SUDO rm -f "$MANDIR/gitignore.1"; then
print_success "Manual page removed from $MANDIR"
fi
read -p "Remove configuration files? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
rm -rf "$HOME/.config/gitignore"
print_success "Configuration removed"
fi
print_success "Uninstallation complete"
}
main() {
# Parse arguments
case "${1:-}" in
--uninstall|-u)
print_banner
uninstall
exit 0
;;
--help|-h)
echo "Usage: $0 [OPTIONS] [PREFIX]"
echo ""
echo "Options:"
echo " -h, --help Show this help message"
echo " -u, --uninstall Uninstall gitignore"
echo ""
echo "Examples:"
echo " $0 # Install to /usr/local (default)"
echo " $0 ~/.local # Install to ~/.local"
echo " PREFIX=~/.local $0 # Install using environment variable"
echo ""
echo "Environment Variables:"
echo " PREFIX Installation prefix (default: /usr/local)"
echo ""
exit 0
;;
esac
print_banner
show_prefix_info
detect_os
check_requirements
original_dir=$(pwd)
clone_or_download
build_from_source
install_binary
cd "$original_dir"
setup_config
verify_installation
show_completion
}
# Handle Ctrl+C
trap 'echo -e "\n${RED}Installation cancelled${NC}"; exit 1' INT
# Run main
main "$@"