-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpx.sh
616 lines (529 loc) · 18.8 KB
/
helpx.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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#!/bin/bash
# -----------------------------
# Helpx: Help and Cron Job Management Tool
# -----------------------------
# -----------------------------
# Exit on any error and treat unset variables as errors
# -----------------------------
set -euo pipefail
# -----------------------------
# Define Known Commands Globally
# -----------------------------
# Added "list" to known_commands
known_commands=("create" "edit" "delete" "list" "menu" "gui")
# -----------------------------
# Function to Check if Script is Sourced
# -----------------------------
is_sourced() {
# Check if the script is being sourced
# BASH_SOURCE[0] != $0 when sourced
[[ "${BASH_SOURCE[0]}" != "${0}" ]]
}
# -----------------------------
# Function to Define Colors for Output
# -----------------------------
define_colors() {
HELPX_RED='\033[0;31m'
HELPX_GREEN='\033[0;32m'
HELPX_BLUE='\033[0;34m'
HELPX_YELLOW='\033[1;33m'
HELPX_CYAN='\033[0;36m'
HELPX_RESET='\033[0m'
}
# Initialize Colors
define_colors
# -----------------------------
# Function to Print Header
# -----------------------------
print_header() {
echo -e "${HELPX_CYAN}"
echo "=============================="
echo " HELPX MENU "
echo "=============================="
echo -e "${HELPX_RESET}"
}
# -----------------------------
# Helper Functions for Output
# -----------------------------
print_success() {
echo -e "${HELPX_GREEN}[+] $1${HELPX_RESET}"
}
print_error() {
echo -e "${HELPX_RED}[-] $1${HELPX_RESET}"
}
print_info() {
echo -e "${HELPX_YELLOW}[i] $1${HELPX_RESET}"
}
print_prompt() {
echo -e "${HELPX_BLUE}[*] $1${HELPX_RESET}"
}
print_color() {
echo -e "${HELPX_GREEN}[-] $1${HELPX_RESET}"
}
# -----------------------------
# Function to Determine Home Directory
# -----------------------------
determine_home() {
if [[ "$EUID" -eq 0 && -n "${SUDO_USER:-}" ]]; then
# Get the home directory of the user who invoked sudo
USER_HOME=$(eval echo "~$SUDO_USER")
else
USER_HOME="$HOME"
fi
echo "$USER_HOME"
}
# -----------------------------
# Function to Define Help Topics Directory
# -----------------------------
define_help_dir() {
USER_HOME=$(determine_home)
HELP_DIR="$USER_HOME/.helptopics"
mkdir -p "$HELP_DIR"
}
# -----------------------------
# Define Log File
# -----------------------------
define_log_file() {
LOG_FILE="$HELP_DIR/.helpx.log"
if [[ ! -f "$LOG_FILE" ]]; then
touch "$LOG_FILE"
chmod 664 "$LOG_FILE"
fi
}
# -----------------------------
# Function to Log Actions
# -----------------------------
log_action() {
local action="$1"
local detail="$2"
echo "$(date '+%Y-%m-%d %H:%M:%S') : $action : $detail" >> "$LOG_FILE"
}
# -----------------------------
# Function to Sanitize Topic Names (Auto-formatting)
# -----------------------------
sanitize_topic_name() {
local raw_name="$1"
# Replace spaces with underscores, remove trailing .txt, and remove invalid characters
local sanitized_name
sanitized_name=$(echo "$raw_name" | sed 's/\.txt$//' | tr ' ' '_' | tr -cd '[:alnum:]_-')
echo "$sanitized_name"
}
# -----------------------------
# Function to Use User's Default Editor
# -----------------------------
get_editor() {
local editor
editor="${EDITOR:-nano}"
echo "$editor"
}
# -----------------------------
# Function to Install Helpx as Command-Line Tool
# -----------------------------
install_helpx() {
# Define the installation path
local install_path="/usr/local/bin/helpx"
# Check if helpx is already installed
if command -v helpx &>/dev/null; then
print_info "Existing helpx installation found at $(command -v helpx). Overwriting..."
sudo rm -f "$install_path"
fi
# Install the new helpx script
sudo cp "$(realpath "${BASH_SOURCE[0]}")" "$install_path"
sudo chmod +x "$install_path"
sudo chown root:root "$install_path"
print_success "Helpx installed successfully at $install_path."
log_action "INSTALL" "Helpx installed at $install_path."
}
# -----------------------------
# Function to Create a New Help Topic
# -----------------------------
create_topic() {
local raw_topic_name="$1"
# Removed category parameter as per user request
if [[ -z "$raw_topic_name" ]]; then
print_error "Please provide a topic name. Usage: helpx create <topic_name>"
exit 1
fi
# Sanitize topic name
local topic_name
topic_name=$(sanitize_topic_name "$raw_topic_name")
# Define topic file path directly in HELP_DIR
local topic_file="$HELP_DIR/$topic_name.txt"
if [[ -f "$topic_file" ]]; then
print_error "Help topic '$topic_name.txt' already exists."
exit 1
fi
# Create the topic with a template
tee "$topic_file" > /dev/null <<'EOF'
# <Topic Name> Help
# ---------------------
# Description:
# Tags:
# Usage:
# Examples:
EOF
print_success "Help topic '$topic_name' created successfully."
log_action "CREATE" "Topic '$topic_name' created."
# Open the topic in the default editor
local editor
editor=$(get_editor)
"$editor" "$topic_file"
}
# -----------------------------
# Function to Edit an Existing Help Topic
# -----------------------------
edit_topic() {
local topic_name="$1"
if [[ -z "$topic_name" ]]; then
print_error "Please provide a topic name. Usage: helpx edit <topic_name>"
exit 1
fi
# Strip .txt if present to prevent double extension
topic_name="${topic_name%.txt}" # # CHANGE
# Find the topic file using exact path
local topic_file
topic_file="$HELP_DIR/$topic_name.txt"
if [[ ! -f "$topic_file" ]]; then
print_error "Help topic '$topic_name.txt' does not exist."
exit 1
fi
print_header
echo -e "${HELPX_GREEN}Editing Help Topic: $topic_name${HELPX_RESET}"
echo "----------------------------------------"
# Backup the current topic before editing
cp "$topic_file" "$topic_file.bak"
print_info "Backup created at '$topic_file.bak'."
log_action "BACKUP" "Backup created for topic '$topic_name'."
# Open the topic in the default editor
local editor
editor=$(get_editor)
"$editor" "$topic_file"
print_success "Help topic '$topic_name' updated successfully."
log_action "EDIT" "Topic '$topic_name' edited."
}
# -----------------------------
# Function to Delete a Help Topic
# -----------------------------
delete_topic() {
local topic_name="$1"
if [[ -z "$topic_name" ]]; then
print_error "Please provide a topic name. Usage: helpx delete <topic_name>"
exit 1
fi
# Strip .txt if present to prevent double extension
topic_name="${topic_name%.txt}" # # CHANGE
# Find the topic file using exact path
local topic_file
topic_file="$HELP_DIR/$topic_name.txt"
if [[ ! -f "$topic_file" ]]; then
print_error "Help topic '$topic_name.txt' does not exist."
exit 1
fi
# Backup before deletion
cp "$topic_file" "$topic_file.bak"
print_info "Backup created at '$topic_file.bak'."
log_action "BACKUP_DELETE" "Backup created before deleting topic '$topic_name'."
rm "$topic_file"
print_success "Help topic '$topic_name' deleted successfully."
log_action "DELETE" "Topic '$topic_name' deleted."
}
# -----------------------------
# Function to List All Help Topics
# -----------------------------
list_topics() {
echo -e "${HELPX_CYAN}"
echo "LIST OF HELP TOPICS"
echo "========================================"
echo -e "${HELPX_RESET}"
# Check if HELP_DIR exists and is not empty
if [[ -d "$HELP_DIR" ]]; then
local topics=("$HELP_DIR"/*.txt)
if [[ -e "${topics[0]}" ]]; then
for topic_file in "${topics[@]}"; do
# Extract the filename without the directory and .txt extension
local filename
filename=$(basename "$topic_file" .txt)
echo "- $filename"
done
log_action "LIST" "Listed all help topics."
else
echo -e "${HELPX_YELLOW}No help topics found.${HELPX_RESET}"
fi
else
echo -e "${HELPX_YELLOW}Help topics directory does not exist.${HELPX_RESET}"
fi
}
# -----------------------------
# Function to Implement Interactive Menu Using select
# -----------------------------
interactive_menu() {
while true; do
print_header
echo -e "${HELPX_YELLOW}Interactive Helpx Menu:${HELPX_RESET}"
echo "1. Create a New Help Topic"
echo "2. Edit an Existing Help Topic"
echo "3. Delete a Help Topic"
echo "4. List All Help Topics"
echo "5. Exit"
echo
read -p "Enter your choice (1-5): " choice
case "$choice" in
1)
read -p "Enter the new topic name: " raw_topic_name
[[ -z "$raw_topic_name" ]] && print_error "Topic name cannot be empty." && continue
create_topic "$raw_topic_name"
;;
2)
read -p "Enter the topic name to edit: " topic_name
[[ -z "$topic_name" ]] && print_error "Topic name cannot be empty." && continue
edit_topic "$topic_name"
;;
3)
read -p "Enter the topic name to delete: " topic_name
[[ -z "$topic_name" ]] && print_error "Topic name cannot be empty." && continue
# Confirmation Prompt
read -p "Are you sure you want to delete '$topic_name'? (y/N): " confirm
case "$confirm" in
[yY][eE][sS]|[yY])
delete_topic "$topic_name"
;;
*)
print_info "Deletion cancelled."
;;
esac
;;
4)
list_topics
;;
5)
print_info "Exiting Interactive Menu."
break
;;
*)
print_error "Invalid choice. Please select between 1-5."
;;
esac
echo
done
}
# -----------------------------
# Function to Implement GUI Menu Using Zenity
# -----------------------------
gui_menu() {
while true; do
# Display the main menu using Zenity and suppress GTK warnings
selection=$(zenity --list --title="Helpx GUI Menu" \
--column="Options" \
"Create a New Help Topic" \
"Edit an Existing Help Topic" \
"Delete a Help Topic" \
"List All Help Topics" \
"Exit" \
--height=500 --width=500 2>/dev/null)
# Check if the user closed the dialog
if [[ $? -ne 0 ]]; then
break
fi
case "$selection" in
"Create a New Help Topic")
# Prompt for topic name
raw_topic_name=$(zenity --entry --title="Create Help Topic" --text="Enter the new topic name:" 2>/dev/null)
# Check if user canceled or entered nothing
if [[ $? -ne 0 || -z "$raw_topic_name" ]]; then
zenity --warning --text="Topic creation canceled or no name entered." 2>/dev/null
continue
fi
# Sanitize topic name
sanitized_topic_name=$(sanitize_topic_name "$raw_topic_name")
# Create the topic
create_topic "$raw_topic_name"
;;
"Edit an Existing Help Topic")
# Get list of topics
topics=$(find "$HELP_DIR" -type f -name "*.txt" | sed "s|$HELP_DIR/||;s|\.txt$||" | sort)
if [[ -z "$topics" ]]; then
zenity --error --text="No help topics available to edit." 2>/dev/null
continue
fi
# Prompt to select a topic
topic_name=$(zenity --list --title="Edit Help Topic" --column="Topics" $topics 2>/dev/null)
if [[ $? -ne 0 || -z "$topic_name" ]]; then
zenity --warning --text="No topic selected for editing." 2>/dev/null
continue
fi
# Edit the selected topic
edit_topic "$topic_name"
;;
"Delete a Help Topic")
# Get list of topics
topics=$(find "$HELP_DIR" -type f -name "*.txt" | sed "s|$HELP_DIR/||;s|\.txt$||" | sort)
if [[ -z "$topics" ]]; then
zenity --error --text="No help topics available to delete." 2>/dev/null
continue
fi
# Prompt to select a topic
topic_name=$(zenity --list --title="Delete Help Topic" --column="Topics" $topics 2>/dev/null)
if [[ $? -ne 0 || -z "$topic_name" ]]; then
zenity --warning --text="No topic selected for deletion." 2>/dev/null
continue
fi
# Confirmation
zenity --question --title="Confirm Deletion" --text="Are you sure you want to delete '$topic_name'?" 2>/dev/null
if [[ $? -eq 0 ]]; then
delete_topic "$topic_name"
else
zenity --info --text="Deletion cancelled." 2>/dev/null
fi
;;
"List All Help Topics")
# Create a temporary file to store the list
tmpfile=$(mktemp)
# Capture the list of topics
list_topics > "$tmpfile"
# Display the list using Zenity
zenity --text-info --title="List of Help Topics" --filename="$tmpfile" --width=400 --height=400 2>/dev/null
# Remove the temporary file
rm -f "$tmpfile"
;;
"Exit")
print_info "Exiting GUI Menu."
break
;;
*)
print_error "Invalid selection."
;;
esac
done
}
# -----------------------------
# Function to Install or Update Helpx (Only when Sourced)
# -----------------------------
install_or_update_helpx() {
install_helpx
}
define_help_dir
define_log_file
view_topic_console() {
local topic_name="$1"
if [[ -z "$topic_name" ]]; then
print_error "Please provide a topic name. Usage: helpx \"topic_name\""
exit 1
fi
# Strip .txt if present to prevent double extension
topic_name="${topic_name%.txt}" # # CHANGE
# Find the topic file using exact path
local topic_file
topic_file="$HELP_DIR/$topic_name.txt"
if [[ ! -f "$topic_file" ]]; then
print_error "Help topic '$topic_name.txt' does not exist."
exit 1
fi
print_header
echo -e "${HELPX_YELLOW}Help Topic: $topic_name${HELPX_RESET}"
echo "----------------------------------------"
cat "$topic_file"
echo "----------------------------------------"
log_action "VIEW_CONSOLE" "Viewed topic '$topic_name' in console."
}
# -----------------------------
# Main Execution (Only when not Sourced)
# -----------------------------
if ! is_sourced; then
# known_commands are already defined globally
# If exactly one argument
if [[ $# -eq 1 ]]; then
# Check if the argument is a known command
if [[ " ${known_commands[@]} " =~ " $1 " ]]; then
# It's a known command, handle accordingly
case "${1}" in
create)
# Missing topic name
print_error "Please provide a topic name. Usage: helpx create <topic_name>"
exit 1
;;
edit)
print_error "Please provide a topic name. Usage: helpx edit <topic_name>"
exit 1
;;
delete)
print_error "Please provide a topic name. Usage: helpx delete <topic_name>"
exit 1
;;
list)
list_topics
;;
menu)
interactive_menu
;;
gui)
gui_menu
;;
*)
# Removed handling for "search", "cron", "help"
print_error "Invalid command."
exit 1
;;
esac
exit 0
else
# Treat as topic name (including category, but categories are not used)
view_topic_console "$1"
exit 0
fi
fi
# Handle multiple arguments or other commands
case "${1:-}" in
create)
if [[ -z "${2:-}" ]]; then
print_error "Please provide a topic name. Usage: helpx create <topic_name>"
exit 1
fi
create_topic "$2"
;;
edit)
if [[ -z "${2:-}" ]]; then
print_error "Please provide a topic name. Usage: helpx edit <topic_name>"
exit 1
fi
edit_topic "$2"
;;
delete)
if [[ -z "${2:-}" ]]; then
print_error "Please provide a topic name. Usage: helpx delete <topic_name>"
exit 1
fi
delete_topic "$2"
;;
list)
list_topics
;;
menu)
interactive_menu
;;
gui)
gui_menu
;;
*)
# Removed handling for "search", "cron", "help"
print_color " 𝙎𝙮𝙣𝙩𝙖𝙭 𝙈𝙞𝙨𝙨𝙞𝙣𝙜 "
print_error " •═════════|══════════• "
print_error " • V • "
print_error " • ╭──────────────╮ • "
print_error " • │ Menu │ • "
print_error " • ╰──────────────╯ • "
print_error " • ╭──────────────╮ • "
print_error " • │ Gui │ • "
print_error " • ╰──────────────╯ • "
print_error " • ╭──────────────╮ • "
print_error " • │ List │ • "
print_error " • ╰──────────────╯ • "
print_error " • ╭──────────────╮ • "
print_error " • │ Delete │ • "
print_error " • ╰──────────────╯ • "
print_error " •════════════════════• "
print_color " 𝙜𝙞𝙩𝙝𝙪𝙗.𝙘𝙤𝙢/𝙯𝙚𝙗𝙗𝙚𝙧𝙣 "
exit 1
;;
esac
fi
exit 0