-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·215 lines (169 loc) · 5.14 KB
/
setup.sh
File metadata and controls
executable file
·215 lines (169 loc) · 5.14 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
#!/usr/bin/env bash
RESET='\033[0m'
BOLD='\033[1m'
DIM='\033[2m'
UNDERLINE='\033[4m'
REVERSE='\033[7m'
RED='\033[31m'
YELLOW='\033[33m'
GREEN='\033[32m'
BLUE='\033[34m'
CYAN='\033[36m'
set -euo pipefail
PROJECT_NAME="framesniff"
PROGRAM_EXTENSION=".py"
WRAPPER_NAME=$PROJECT_NAME$PROGRAM_EXTENSION
VENV_DIR=".venv"
VENV_PYTHON="$VENV_DIR/bin/python"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT_PATH="$SCRIPT_DIR/$PROJECT_NAME$PROGRAM_EXTENSION"
VENV_ARGCOMPLETE="$SCRIPT_DIR/$VENV_DIR/bin/register-python-argcomplete"
CURRENT_SHELL=$(basename "$SHELL")
case "$CURRENT_SHELL" in
bash)
RC_FILE="$HOME/.bashrc"
;;
zsh)
RC_FILE="$HOME/.zshrc"
;;
*)
RC_FILE=""
;;
esac
AUTOCOMPLETE_LINE="eval \"\$($VENV_ARGCOMPLETE $WRAPPER_NAME)\""
SYSTEM_DEPENDENCIES=("python3" "iw" "dhcpcd" "wpa_supplicant" "pkill" "ip")
print_step() {
echo
echo "==> $1"
}
print_ok() {
echo "✔ $1"
}
print_error() {
echo "✖ $1"
exit 1
}
print_header() {
echo
echo -e "${REVERSE} $1 ${RESET}"
echo
}
print_section() {
echo -e "${BOLD}$1:${RESET}"
}
print_info() {
echo -e " ${BLUE}$1${RESET}"
}
print_success() {
echo -e " ${GREEN}$1${RESET}"
}
print_warning() {
echo -e " ${YELLOW}$1${RESET}"
}
print_error_msg() {
echo -e " ${RED}$1${RESET}"
}
print_command() {
echo -e " ${GREEN}$1${RESET}"
}
print_comment() {
echo -e " ${DIM}# $1${RESET}"
}
print_separator_smooth() {
local cols
cols=$(tput cols 2>/dev/null || echo 80)
printf "%*s\n" "$cols" "" | tr ' ' '─'
}
print_separator_equals() {
local cols
cols=$(tput cols 2>/dev/null || echo 80)
printf "%*s\n" "$cols" "" | tr ' ' '='
}
print_step "Checking required system dependencies..."
for pkg in "${SYSTEM_DEPENDENCIES[@]}"; do
if ! command -v "$pkg" >/dev/null 2>&1; then
print_error "Missing dependency: $pkg"
fi
done
print_ok "All required system dependencies are available."
print_step "Setting up virtual environment..."
if [ ! -d "$VENV_DIR" ]; then
python3 -m venv "$VENV_DIR"
print_ok "Virtual environment created."
else
print_ok "Virtual environment already exists."
fi
source "$VENV_DIR/bin/activate"
print_step "Upgrading pip..."
python -m pip install --upgrade pip >/dev/null 2>&1
print_step "Installing dependencies..."
if ! pip install -r requirements.txt --no-cache-dir --no-input --upgrade --force-reinstall; then
print_error "Failed to install dependencies"
fi
print_step "Setup completed successfully!"
print_header "$RED IMPORTANT"
print_section "Interpreter"
print_info "$VENV_PYTHON"
echo
print_section "Usage"
print_comment "activate virtual environment"
print_command "source \"$VENV_DIR/bin/activate\""
echo
print_comment "run the program"
print_command "\"$VENV_PYTHON\" \"$SCRIPT_PATH\" --help"
echo
print_header "$CYAN OPTIONAL: CREATE WRAPPER + AUTOCOMPLETE"
read -p "Do you want to make '$WRAPPER_NAME' a system command with autocomplete? (requires sudo) (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
if [ ! -f /usr/share/bash-completion/bash_completion ]; then
print_warning "bash-completion not found (optional)"
fi
if [ ! -f "$VENV_ARGCOMPLETE" ]; then
print_error_msg "argcomplete not found in venv."
print_command "Run: source \"$VENV_DIR/bin/activate\" && pip install argcomplete"
exit 1
fi
print_step "Creating wrapper at /usr/local/bin/$WRAPPER_NAME..."
WRAPPER_CONTENT="#!/bin/bash
exec \"$SCRIPT_DIR/$VENV_PYTHON\" \"$SCRIPT_PATH\" \"\$@\""
if echo "$WRAPPER_CONTENT" | sudo tee /usr/local/bin/$WRAPPER_NAME > /dev/null; then
sudo chmod +x /usr/local/bin/$WRAPPER_NAME
print_ok "Wrapper created at /usr/local/bin/$WRAPPER_NAME"
else
print_error_msg "Failed to create wrapper. Try manually:"
print_command "echo '$WRAPPER_CONTENT' | sudo tee /usr/local/bin/$WRAPPER_NAME && sudo chmod +x /usr/local/bin/$WRAPPER_NAME"
exit 1
fi
print_step "Configuring autocomplete..."
if [ -z "$RC_FILE" ]; then
print_warning "Shell '$CURRENT_SHELL' not supported for automation."
print_info "Manually add the following to your shell configuration file:"
print_command "$AUTOCOMPLETE_LINE"
else
if grep -q "# $WRAPPER_NAME autocomplete" "$RC_FILE"; then
print_warning "Autocomplete already configured in $RC_FILE"
else
{
echo ""
echo "# $WRAPPER_NAME autocomplete"
if [[ "$CURRENT_SHELL" == "zsh" ]]; then
echo "autoload -U bashcompinit && bashcompinit"
fi
echo "$AUTOCOMPLETE_LINE"
} >> "$RC_FILE"
print_ok "Autocomplete added to $RC_FILE"
fi
print_header "$RED IMPORTANT"
print_section "$RED To activate"
print_command "source $RC_FILE"
echo
fi
print_section "Usage"
print_command "$WRAPPER_NAME --help"
echo
else
print_info "Skipped. You can run the script directly:"
print_command "\"$SCRIPT_DIR/$VENV_PYTHON\" \"$SCRIPT_PATH\" --help"
fi
print_ok "Setup complete! Happy networking!"