#!/usr/bin/env bash ############################################################################### # Script Name: setup-hybrid-notifications.sh # Description: โš™๏ธ Setup Pro Automation - Configure advanced email + text notifications # Author: DJCastle # Version: 1.0.0 # Created: 2025-01-11 # Updated: 2026-02-06 # # LICENSE: Free to use, modify, and distribute # # DISCLAIMER: This script is provided "AS IS" without warranty of any kind. # Use at your own risk. The author is not responsible for any damage, data loss, # or other issues that may occur from using this script. Always backup your # system before running system modification scripts. ############################################################################### # # PURPOSE: # Configure hybrid notifications (email + text) for the advanced auto-update # system. This script sets up auto-update-brew-hybrid.sh with dual notification methods. # # SETUP FEATURES: # - Configures email address for detailed HTML reports # - Configures phone number for quick text summaries # - Tests both notification methods during setup # - Sets up automatic execution via macOS launchd # - Provides intelligent scheduling recommendations # - Validates both Mail app and iMessage configuration # # HOW TO RUN IN TERMINAL: # 1. Open Terminal application (Applications > Utilities > Terminal) # 2. Navigate to script directory: cd /path/to/homeBrewScripts # 3. Make script executable: chmod +x setup-hybrid-notifications.sh # 4. Run the script: ./setup-hybrid-notifications.sh # Follow the interactive prompts to configure both notification methods # # NOTIFICATION METHODS: # ๐Ÿ“ง EMAIL: Detailed HTML reports using macOS Mail app (no external dependencies) # ๐Ÿ“ฑ TEXT: Quick status summaries via iMessage # # REQUIREMENTS: # - macOS with administrator privileges # - Mail app configured with email account # - iMessage configured and signed in # - auto-update-brew-hybrid.sh script present # # SCHEDULING RECOMMENDATIONS: # - WEEKLY (recommended): Less notifications while staying current # - DAILY: Latest updates if you use your Mac heavily # - MANUAL: Full control over when updates happen # # LOG FILE: # Setup operations are logged to: ~/Library/Logs/HybridNotificationSetup.log ############################################################################### SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" HYBRID_SCRIPT="$SCRIPT_DIR/auto-update-brew-hybrid.sh" LOG="$HOME/Library/Logs/HybridNotificationSetup.log" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Function to print colored output print_status() { echo -e "${BLUE}[INFO]${NC} $1" | tee -a "$LOG" } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" | tee -a "$LOG" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" | tee -a "$LOG" } print_error() { echo -e "${RED}[ERROR]${NC} $1" | tee -a "$LOG" } # Function to validate email format validate_email() { local email="$1" if [[ "$email" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ ]]; then return 0 else return 1 fi } # Function to validate phone number format validate_phone_number() { local phone="$1" if [[ "$phone" =~ ^\+[0-9]{10,15}$ ]]; then return 0 else return 1 fi } # Function to test email functionality test_email() { local email_address="$1" print_status "Testing email functionality..." # Check if mail command is available if ! command -v mail &> /dev/null; then print_error "mail command not available. Please configure Mail app first." return 1 fi # Send test email local test_subject="๐Ÿงช Test Email from Auto Update Brew Setup" local test_body="This is a test email from your Auto Update Brew setup script. If you receive this, email notifications are working correctly." echo "$test_body" | mail -s "$test_subject" "$email_address" >> "$LOG" 2>&1 if [ $? -eq 0 ]; then print_success "Test email sent successfully!" return 0 else print_error "Failed to send test email. Please check Mail app configuration." return 1 fi } # Function to test text messaging test_text_message() { local phone_number="$1" print_status "Testing text message to $phone_number..." # Send test message osascript < "$temp_file" mv "$temp_file" "$HYBRID_SCRIPT" print_success "Configuration updated in hybrid script" } # Function to create launchd plist for automatic execution create_launchd_plist() { local schedule="$1" local plist_name="com.homebrew.hybridautoupdate" local plist_path="$HOME/Library/LaunchAgents/$plist_name.plist" # Create plist content based on schedule local plist_content="" case "$schedule" in "daily") plist_content=" Label $plist_name ProgramArguments $HYBRID_SCRIPT StartCalendarInterval Hour 2 Minute 0 StandardOutPath $HOME/Library/Logs/AutoUpdateBrewHybrid.log StandardErrorPath $HOME/Library/Logs/AutoUpdateBrewHybrid.log " ;; "weekly") plist_content=" Label $plist_name ProgramArguments $HYBRID_SCRIPT StartCalendarInterval Weekday 0 Hour 2 Minute 0 StandardOutPath $HOME/Library/Logs/AutoUpdateBrewHybrid.log StandardErrorPath $HOME/Library/Logs/AutoUpdateBrewHybrid.log " ;; esac # Write plist file echo "$plist_content" > "$plist_path" # Load the plist launchctl load "$plist_path" if [ $? -eq 0 ]; then print_success "Automatic execution scheduled: $schedule" return 0 else print_error "Failed to schedule automatic execution" return 1 fi } # Function to show scheduling recommendations show_scheduling_recommendations() { echo "" echo "โฐ Scheduling Recommendations" echo "============================" echo "" echo "๐Ÿ“… WEEKLY (Recommended):" echo " โœ… Less notification noise" echo " โœ… Still keeps your system current" echo " โœ… Runs every Sunday at 2:00 AM" echo " โœ… Good balance of updates vs. notifications" echo "" echo "๐Ÿ“… DAILY:" echo " โœ… Always has the latest updates" echo " โŒ More notifications (daily emails/texts)" echo " โœ… Runs every day at 2:00 AM" echo " โœ… Good if you use your Mac heavily" echo "" echo "๐Ÿ“… MANUAL:" echo " โœ… No automatic notifications" echo " โœ… You control when updates happen" echo " โŒ You must remember to run updates" echo " โœ… Good if you prefer manual control" echo "" } # Main setup function main() { print_status "Hybrid Notification Setup started at $(date)" # Check if hybrid script exists if [ ! -f "$HYBRID_SCRIPT" ]; then print_error "Hybrid script not found: $HYBRID_SCRIPT" exit 1 fi # Make sure hybrid script is executable chmod +x "$HYBRID_SCRIPT" echo "" echo "๐Ÿ”„ Hybrid Notification Setup" echo "============================" echo "" # Get email address echo "๐Ÿ“ง Email Setup" echo "--------------" echo "Enter your email address for detailed reports:" read -p "Email address: " email_address # Validate email while ! validate_email "$email_address"; do print_error "Invalid email format. Please enter a valid email address." read -p "Email address: " email_address done # Test email echo "" print_status "Testing email functionality..." if test_email "$email_address"; then print_success "Email notifications configured successfully" else print_warning "Email test failed. You can still use the script manually." fi # Get phone number echo "" echo "๐Ÿ“ฑ Phone Number Setup" echo "--------------------" echo "Enter your phone number for quick summaries (format: +1234567890):" read -p "Phone number: " phone_number # Validate phone number while ! validate_phone_number "$phone_number"; do print_error "Invalid phone number format. Please use format: +1234567890" read -p "Phone number: " phone_number done # Test text messaging echo "" print_status "Testing text messaging..." if test_text_message "$phone_number"; then print_success "Text notifications configured successfully" else print_warning "Text messaging test failed. You can still use the script manually." fi # Update script configuration update_script_config "$email_address" "$phone_number" # Show scheduling recommendations show_scheduling_recommendations # Schedule selection echo "Choose when to run automatic updates:" echo "1. Weekly (recommended) - Sunday at 2:00 AM" echo "2. Daily - Every day at 2:00 AM" echo "3. Manual execution only" echo "" read -p "Enter choice (1-3): " schedule_choice case "$schedule_choice" in 1) create_launchd_plist "weekly" ;; 2) create_launchd_plist "daily" ;; 3) print_status "Manual execution only selected" ;; *) print_error "Invalid choice. Manual execution only selected." ;; esac # Test the hybrid script echo "" print_status "Testing hybrid auto-update script..." if "$HYBRID_SCRIPT"; then print_success "Hybrid script test completed successfully" else print_warning "Hybrid script test had issues (this is normal if conditions aren't met)" fi # Final instructions echo "" echo "โœ… Setup Complete!" echo "==================" echo "" echo "๐Ÿ“ง Email notifications: $email_address" echo "๐Ÿ“ฑ Text notifications: $phone_number" echo "๐Ÿ“ Log file: $HOME/Library/Logs/AutoUpdateBrewHybrid.log" echo "๐Ÿ”ง Manual execution: ./auto-update-brew-hybrid.sh" echo "" echo "๐Ÿ“‹ Next steps:" echo "1. Make sure Mail app is configured" echo "2. Make sure you're signed into iMessage" echo "3. Add your phone number to your contacts" echo "4. Test the script manually when on YourWiFiNetwork WiFi and plugged in" echo "5. Check the log file for any issues" echo "" echo "๐Ÿšจ What happens if the script doesn't run:" echo "- If not on YourWiFiNetwork WiFi: Sends notification and skips" echo "- If not plugged into power: Sends notification and skips" echo "- If Homebrew not installed: Sends error notification" echo "- If network issues: Retries up to 3 times with 5-minute delays" echo "" print_success "Hybrid notification setup completed at $(date)" } # Run main function main "$@"