-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
256 lines (216 loc) · 7.82 KB
/
install.sh
File metadata and controls
256 lines (216 loc) · 7.82 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
#!/bin/bash
# Smart Search Installation Script
# This script installs the smart search tools to make them globally accessible
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Configuration
INSTALL_DIR="$HOME/.local/bin"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Function to print colored output
print_colored() {
local color=$1
local message=$2
echo -e "${color}${message}${NC}"
}
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to add directory to PATH if not already there
add_to_path() {
local dir=$1
local shell_rc=""
# Detect shell and set appropriate RC file
if [[ "$SHELL" == *"zsh"* ]]; then
shell_rc="$HOME/.zshrc"
elif [[ "$SHELL" == *"bash"* ]]; then
if [[ -f "$HOME/.bashrc" ]]; then
shell_rc="$HOME/.bashrc"
else
shell_rc="$HOME/.bash_profile"
fi
elif [[ -f "$HOME/.profile" ]]; then
shell_rc="$HOME/.profile"
fi
if [[ -n "$shell_rc" ]]; then
# Check if PATH already contains the directory
if ! grep -q "export PATH.*$dir" "$shell_rc" 2>/dev/null; then
echo "" >> "$shell_rc"
echo "# Added by Smart Search installer" >> "$shell_rc"
echo "export PATH=\"$dir:\$PATH\"" >> "$shell_rc"
print_colored "$GREEN" "✅ Added $dir to PATH in $shell_rc"
return 0
else
print_colored "$YELLOW" "⚠️ $dir already in PATH in $shell_rc"
return 1
fi
else
print_colored "$YELLOW" "⚠️ Could not detect shell RC file. Please manually add $dir to your PATH"
return 1
fi
}
# Function to create symlinks or copy files
install_script() {
local script_name=$1
local source_path="$SCRIPT_DIR/$script_name"
local target_path="$INSTALL_DIR/$script_name"
if [[ ! -f "$source_path" ]]; then
print_colored "$RED" "❌ Error: $script_name not found in $SCRIPT_DIR"
return 1
fi
# Create symlink or copy file
if command_exists ln; then
ln -sf "$source_path" "$target_path"
print_colored "$GREEN" "✅ Created symlink: $target_path -> $source_path"
else
cp "$source_path" "$target_path"
chmod +x "$target_path"
print_colored "$GREEN" "✅ Copied: $source_path -> $target_path"
fi
}
# Function to create wrapper scripts without extensions
create_wrapper() {
local script_name=$1
local wrapper_name=$2
local target_path="$INSTALL_DIR/$wrapper_name"
cat > "$target_path" << EOF
#!/bin/bash
# Wrapper script for $script_name
exec "$INSTALL_DIR/$script_name" "\$@"
EOF
chmod +x "$target_path"
print_colored "$GREEN" "✅ Created wrapper: $wrapper_name"
}
# Main installation function
main() {
print_colored "$PURPLE" "🚀 Smart Search Installation Script"
print_colored "$BLUE" "=================================="
echo
# Check for required files
print_colored "$CYAN" "📋 Checking required files..."
local required_files=("smart_search.sh" "smart_search.py")
local missing_files=()
for file in "${required_files[@]}"; do
if [[ -f "$SCRIPT_DIR/$file" ]]; then
print_colored "$GREEN" "✅ Found: $file"
else
print_colored "$RED" "❌ Missing: $file"
missing_files+=("$file")
fi
done
if [[ ${#missing_files[@]} -gt 0 ]]; then
print_colored "$RED" "❌ Installation cannot continue. Missing required files."
exit 1
fi
echo
# Check for Python
print_colored "$CYAN" "🐍 Checking Python installation..."
if command_exists python3; then
local python_version=$(python3 --version 2>&1)
print_colored "$GREEN" "✅ Found: $python_version"
else
print_colored "$YELLOW" "⚠️ Python3 not found. Python script may not work."
fi
echo
# Create installation directory
print_colored "$CYAN" "📁 Setting up installation directory..."
if [[ ! -d "$INSTALL_DIR" ]]; then
mkdir -p "$INSTALL_DIR"
print_colored "$GREEN" "✅ Created directory: $INSTALL_DIR"
else
print_colored "$GREEN" "✅ Directory exists: $INSTALL_DIR"
fi
echo
# Install scripts
print_colored "$CYAN" "📦 Installing scripts..."
# Install bash script
install_script "smart_search.sh"
# Install Python script
install_script "smart_search.py"
# Create convenient wrapper commands
create_wrapper "smart_search.sh" "ssearch"
create_wrapper "smart_search.py" "pysearch"
create_wrapper "smart_search.sh" "smart-search"
echo
# Update PATH
print_colored "$CYAN" "🔧 Updating PATH..."
local path_updated=false
# Check if install directory is already in PATH
if [[ ":$PATH:" == *":$INSTALL_DIR:"* ]]; then
print_colored "$GREEN" "✅ $INSTALL_DIR already in PATH"
else
if add_to_path "$INSTALL_DIR"; then
path_updated=true
fi
fi
echo
# Installation complete
print_colored "$GREEN" "🎉 Installation completed successfully!"
echo
print_colored "$YELLOW" "📋 Available commands:"
print_colored "$CYAN" " • smart_search.sh - Bash version"
print_colored "$CYAN" " • smart_search.py - Python version"
print_colored "$CYAN" " • ssearch - Short alias for bash version"
print_colored "$CYAN" " • pysearch - Short alias for python version"
print_colored "$CYAN" " • smart-search - Alternative alias for bash version"
echo
if [[ "$path_updated" == true ]]; then
print_colored "$YELLOW" "⚠️ Please restart your terminal or run:"
print_colored "$CYAN" " source ~/.bashrc # or ~/.zshrc depending on your shell"
echo
fi
print_colored "$YELLOW" "📖 Usage examples:"
print_colored "$CYAN" " ssearch \"config\" /path/to/search"
print_colored "$CYAN" " pysearch -c \"TODO\" ."
print_colored "$CYAN" " smart-search -a \"test\" ./project"
echo
print_colored "$BLUE" "📚 For detailed documentation, see README.md"
print_colored "$GREEN" "✅ Happy searching! 🔍"
}
# Handle command line arguments
case "${1:-}" in
-h|--help)
echo "Smart Search Installation Script"
echo
echo "Usage: $0 [OPTIONS]"
echo
echo "Options:"
echo " -h, --help Show this help message"
echo " --uninstall Remove installed scripts"
echo
echo "This script installs smart search tools to $INSTALL_DIR"
echo "and adds the directory to your PATH for global access."
exit 0
;;
--uninstall)
print_colored "$YELLOW" "🗑️ Uninstalling Smart Search..."
# Remove installed files
local files_to_remove=("smart_search.sh" "smart_search.py" "ssearch" "pysearch" "smart-search")
for file in "${files_to_remove[@]}"; do
if [[ -f "$INSTALL_DIR/$file" ]]; then
rm "$INSTALL_DIR/$file"
print_colored "$GREEN" "✅ Removed: $INSTALL_DIR/$file"
fi
done
print_colored "$GREEN" "✅ Uninstallation completed!"
print_colored "$YELLOW" "⚠️ Note: PATH modifications in shell RC files were not removed automatically."
print_colored "$YELLOW" " You may want to manually remove the PATH entry for $INSTALL_DIR"
exit 0
;;
"")
# No arguments, proceed with installation
main
;;
*)
print_colored "$RED" "❌ Unknown option: $1"
print_colored "$YELLOW" "Use --help for usage information"
exit 1
;;
esac