-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
199 lines (163 loc) Β· 5.04 KB
/
install.sh
File metadata and controls
199 lines (163 loc) Β· 5.04 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
#!/bin/bash
################################################################################
# System Monitor - Installation Script v1.0
#
# Installs System Monitor to $HOME/.local/bin/ and configures shell alias
#
# Usage:
# bash install.sh
#
# Environment Variables:
# INSTALL_DIR: Custom installation directory (default: $HOME/.local/bin)
# SHELL_RC: Custom shell rc file (default: $HOME/.bashrc for bash)
#
# Privacy Note:
# By default, this tool fetches your geolocation using your public IP.
# See PRIVACY.md for details. To disable, use: MONITOR_OFFLINE=1 monitor
#
################################################################################
set -euo pipefail
# Configuration
INSTALL_DIR="${INSTALL_DIR:-$HOME/.local/bin}"
MONITOR_REPO="$(cd "$(dirname "$0")" && pwd)"
MONITOR_SRC="$MONITOR_REPO/src/monitor.py"
MONITOR_BIN="$INSTALL_DIR/monitor"
# Detect shell RC file
if [[ -n "${BASH_VERSION:-}" ]]; then
SHELL_RC="${SHELL_RC:-$HOME/.bashrc}"
elif [[ -n "${ZSH_VERSION:-}" ]]; then
SHELL_RC="${SHELL_RC:-$HOME/.zshrc}"
else
SHELL_RC="${SHELL_RC:-$HOME/.bashrc}"
fi
# Color output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Functions
log_info() {
echo -e "${GREEN}β${NC} $*"
}
log_warn() {
echo -e "${YELLOW}β ${NC} $*"
}
log_error() {
echo -e "${RED}β${NC} $*" >&2
}
check_dependencies() {
local missing=()
for cmd in python3 curl free df uptime ip tput; do
if ! command -v "$cmd" &> /dev/null; then
missing+=("$cmd")
fi
done
if [[ ${#missing[@]} -gt 0 ]]; then
log_error "Missing required dependencies: ${missing[*]}"
log_warn "Install them with your package manager, e.g.:"
log_warn " Ubuntu/Debian: sudo apt install ${missing[*]}"
log_warn " Fedora: sudo dnf install ${missing[*]}"
log_warn " Arch: sudo pacman -S ${missing[*]}"
return 1
fi
return 0
}
check_python_version() {
local py_version
py_version=$(python3 --version 2>&1 | grep -oP '\d+\.\d+' | head -1)
if [[ -z "$py_version" ]]; then
log_error "Could not determine Python version"
return 1
fi
# Compare versions: check if 3.6+
local major minor
major=$(echo "$py_version" | cut -d. -f1)
minor=$(echo "$py_version" | cut -d. -f2)
if [[ $major -lt 3 ]] || ( [[ $major -eq 3 ]] && [[ $minor -lt 6 ]] ); then
log_error "Python 3.6+ required, found $py_version"
return 1
fi
return 0
}
install_monitor() {
log_info "Creating installation directory: $INSTALL_DIR"
mkdir -p "$INSTALL_DIR"
if [[ ! -f "$MONITOR_SRC" ]]; then
log_error "Source file not found: $MONITOR_SRC"
log_warn "Make sure you're running this script from the project root"
return 1
fi
log_info "Installing monitor script..."
cp "$MONITOR_SRC" "$MONITOR_BIN"
chmod +x "$MONITOR_BIN"
# Verify installation
if [[ ! -x "$MONITOR_BIN" ]]; then
log_error "Failed to install monitor binary"
return 1
fi
log_info "Installed: $MONITOR_BIN"
}
configure_alias() {
if [[ ! -f "$SHELL_RC" ]]; then
log_warn "Shell RC file not found: $SHELL_RC"
log_info "You can manually add this alias: alias monitor='python3 \"$MONITOR_BIN\"'"
return 0
fi
log_info "Configuring shell alias in: $SHELL_RC"
# Remove old alias if exists
sed -i '/# SYSTEM MONITOR INSTALLED/,+1d' "$SHELL_RC" 2>/dev/null || true
# Add new alias
cat >> "$SHELL_RC" << EOF
# SYSTEM MONITOR INSTALLED
alias monitor='python3 "$MONITOR_BIN"'
EOF
log_info "Alias configured"
}
test_installation() {
log_info "Testing installation..."
if ! timeout 3 python3 "$MONITOR_BIN" 2>/dev/null || true; then
log_info "Monitor started successfully (test timeout after 3s)"
fi
return 0
}
# Main installation flow
main() {
echo -e "${GREEN}π System Monitor Installer v1.0${NC}\n"
log_info "Checking dependencies..."
if ! check_dependencies; then
return 1
fi
log_info "Checking Python version..."
if ! check_python_version; then
return 1
fi
if ! install_monitor; then
return 1
fi
if ! configure_alias; then
log_warn "Alias configuration incomplete"
fi
if ! test_installation; then
log_warn "Installation test failed"
fi
echo
echo -e "${GREEN}β Installation complete!${NC}"
echo
echo "Next steps:"
echo " 1. Reload your shell: source $SHELL_RC"
echo " 2. Run the monitor: monitor"
echo
echo "Options:"
echo " MONITOR_CYCLES=5 monitor # Run 5 cycles instead of infinite"
echo " MONITOR_INTERVAL=3 monitor # Update every 3 seconds"
echo " MONITOR_OFFLINE=1 monitor # Run without internet/geo-IP"
echo " MONITOR_DEBUG=1 monitor # Enable debug output"
echo
}
# Run main with error handling
if main; then
exit 0
else
log_error "Installation failed"
exit 1
fi