diff --git a/launcher.sh b/launcher.sh index 4135713..883b73c 100755 --- a/launcher.sh +++ b/launcher.sh @@ -18,11 +18,11 @@ LIB_DIR="$BASE_DIR/lib" CONFIG_DIR="$BASE_DIR/config" # Load core libraries -source "$LIB_DIR/common-functions.sh" || { echo "ERROR: Failed to load common-functions.sh"; exit 1; } -source "$LIB_DIR/system-detect.sh" || { echo "ERROR: Failed to load system-detect.sh"; exit 1; } -source "$LIB_DIR/domain-discovery.sh" || { echo "ERROR: Failed to load domain-discovery.sh"; exit 1; } -source "$LIB_DIR/user-manager.sh" || { echo "ERROR: Failed to load user-manager.sh"; exit 1; } -source "$LIB_DIR/reference-db.sh" || { echo "ERROR: Failed to load reference-db.sh"; exit 1; } +source "$LIB_DIR/common-functions.sh" || { echo "ERROR: Failed to load common-functions.sh"; return 1; } +source "$LIB_DIR/system-detect.sh" || { echo "ERROR: Failed to load system-detect.sh"; return 1; } +source "$LIB_DIR/domain-discovery.sh" || { echo "ERROR: Failed to load domain-discovery.sh"; return 1; } +source "$LIB_DIR/user-manager.sh" || { echo "ERROR: Failed to load user-manager.sh"; return 1; } +source "$LIB_DIR/reference-db.sh" || { echo "ERROR: Failed to load reference-db.sh"; return 1; } # Color codes RED='\033[0;31m' @@ -663,10 +663,9 @@ startup_detection() { echo " Database: $SYS_DB_TYPE $SYS_DB_VERSION" echo "" - local user_count=$(grep -c "^USER|" "$SYSREF_DB" 2>/dev/null || echo 0) - local domain_count=$(grep -c "^DOMAIN|" "$SYSREF_DB" 2>/dev/null || echo 0) - local db_count=$(grep -c "^DB|" "$SYSREF_DB" 2>/dev/null || echo 0) - local wp_count=$(grep -c "^WP|" "$SYSREF_DB" 2>/dev/null || echo 0) + # Count records in database with single awk pass (instead of 4 separate grep -c calls) + local counts=$(awk -F'|' '{a[$1]++} END {printf "%d %d %d %d", a["USER"]+0, a["DOMAIN"]+0, a["DB"]+0, a["WP"]+0}' "$SYSREF_DB" 2>/dev/null || echo "0 0 0 0") + read -r user_count domain_count db_count wp_count <<< "$counts" echo -e "Server Content:" echo " Users: $user_count" @@ -679,7 +678,9 @@ startup_detection() { echo "" # Read from terminal (use /dev/tty directly) - read -p "Press Enter to continue..." /dev/null; then + true # Continue even if read fails + fi fi } @@ -688,7 +689,13 @@ startup_detection() { ############################################################################# main() { - init_directories + # Initialize directories once at startup + init_directories || { + echo "ERROR: Failed to initialize directories" + return 1 + } + + # Detect system configuration (builds database if cache expired) startup_detection while true; do @@ -710,12 +717,12 @@ main() { 7) run_module "maintenance" "cleanup-toolkit-data.sh" ;; 0) echo "" - read -p "Clean history and remove traces? (yes/no): " clean_hist < /dev/tty + if ! read -p "Clean history and remove traces? (yes/no): " clean_hist /dev/null; then + return 0 # Exit if read fails + fi if [ "$clean_hist" = "yes" ]; then - # Use secure temp file creation to prevent symlink attacks - CLEANUP_FILE=$(mktemp -t server-toolkit-cleanup.XXXXXX 2>/dev/null) || CLEANUP_FILE="/tmp/.cleanup_requested" - touch "$CLEANUP_FILE" 2>/dev/null || true + touch /tmp/.cleanup_requested 2>/dev/null || true echo "" echo "Cleanup will happen automatically..." echo "" diff --git a/run.sh b/run.sh index 04769e9..0c3e39c 100755 --- a/run.sh +++ b/run.sh @@ -30,7 +30,10 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then fi # Run the launcher (source in current shell, don't execute in subshell) -source "$SCRIPT_DIR/launcher.sh" +source "$SCRIPT_DIR/launcher.sh" || { + echo "ERROR: Failed to load launcher.sh" + return 1 +} # Check if cleanup is requested if [ -f /tmp/.cleanup_requested ]; then