Tutorial

New updates and improvements to Roundfleet.

Configuring Keyboard Settings on Mac Devices

Managing keyboard settings consistently across a Mac fleet is essential for maintaining productivity and user experience standards. This comprehensive guide provides shell scripts and techniques to configure various keyboard settings on macOS devices, including key repeat rates, delays, keyboard navigation, and auto-correct features.

Understanding Mac Keyboard Settings

macOS provides several keyboard configuration options that can significantly impact user productivity and comfort:

Key Repeat Rate

The speed at which a character is repeatedly registered when a key is held down. A higher rate means faster character repetition.

Delay Until Repeat

The amount of time the keyboard waits before starting to repeat a key when held down. A shorter delay means repetition begins sooner.

Keyboard Navigation

Allows users to navigate interface elements using the keyboard instead of mouse clicks, improving accessibility and workflow efficiency.

Auto-Correct

Automatically corrects misspelled words as you type, which can be helpful or disruptive depending on user preferences and use cases.

Prerequisites

Before configuring keyboard settings, ensure you have:

  • Administrative privileges on the Mac
  • Terminal or SSH access
  • Understanding of user context requirements
  • Backup of current settings (recommended)

Basic Keyboard Configuration Commands

Understanding the Configuration Tools

Mac keyboard settings are managed through several system components:

  • defaults: Command-line interface to user defaults system
  • launchctl: Interface to launchd for running commands in user context
  • NSGlobalDomain: Domain for system-wide settings
  • User context: Settings must be applied in the correct user session

Current User Detection

Most keyboard settings require user context. Here's how to detect the current user:

#!/bin/bash

# Get current console user
get_current_user() {
    local current_user=$(ls -l /dev/console | awk '/ / { print $3 }')
    echo "$current_user"
}

# Get user ID
get_user_id() {
    local username=$1
    local user_id=$(id -u "$username")
    echo "$user_id"
}

# Example usage
current_user=$(get_current_user)
user_id=$(get_user_id "$current_user")

echo "Current user: $current_user"
echo "User ID: $user_id"

Key Repeat Rate Configuration

Basic Key Repeat Rate Script

Configure the speed of key repetition:

#!/bin/bash

# Configure key repeat rate
# Values: 1-15 (1 = fastest, 15 = slowest)
KEY_REPEAT_RATE=2

echo "Configuring key repeat rate to: $KEY_REPEAT_RATE"

# Get current user and user ID
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")

# Apply setting in user context
if launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" defaults write -g KeyRepeat -int $KEY_REPEAT_RATE; then
    echo "✓ Key repeat rate set successfully"
else
    echo "✗ Failed to set key repeat rate"
    exit 1
fi

echo "Changes will take effect after user logout/login"

Advanced Key Repeat Rate Script

More comprehensive script with validation and logging:

#!/bin/bash

# Advanced key repeat rate configuration script
LOG_FILE="/var/log/keyboard_settings.log"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')

# Function to log messages
log_message() {
    echo "[$TIMESTAMP] $1" | tee -a "$LOG_FILE"
}

# Function to validate key repeat rate
validate_key_repeat_rate() {
    local rate=$1
    
    if [[ ! "$rate" =~ ^[0-9]+$ ]]; then
        log_message "ERROR: Key repeat rate must be a number"
        return 1
    fi
    
    if [ "$rate" -lt 1 ] || [ "$rate" -gt 15 ]; then
        log_message "ERROR: Key repeat rate must be between 1-15"
        return 1
    fi
    
    return 0
}

# Function to get current key repeat rate
get_current_key_repeat_rate() {
    local current_user=$1
    local current_uid=$2
    
    local current_rate=$(launchctl asuser $current_uid sudo -iu "$current_user" defaults read -g KeyRepeat 2>/dev/null)
    
    if [ -n "$current_rate" ]; then
        echo "$current_rate"
    else
        echo "Not set"
    fi
}

# Function to set key repeat rate
set_key_repeat_rate() {
    local rate=$1
    local current_user=$2
    local current_uid=$3
    
    log_message "Setting key repeat rate to: $rate for user: $current_user"
    
    # Get current setting
    local current_rate=$(get_current_key_repeat_rate "$current_user" "$current_uid")
    log_message "Current key repeat rate: $current_rate"
    
    # Apply new setting
    if launchctl asuser $current_uid sudo -iu "$current_user" defaults write -g KeyRepeat -int $rate; then
        log_message "SUCCESS: Key repeat rate set to $rate"
        
        # Verify setting
        local new_rate=$(get_current_key_repeat_rate "$current_user" "$current_uid")
        log_message "Verified new key repeat rate: $new_rate"
        
        return 0
    else
        log_message "ERROR: Failed to set key repeat rate"
        return 1
    fi
}

# Main execution
KEY_REPEAT_RATE=${1:-2}  # Default to 2 if not specified

log_message "Starting key repeat rate configuration"

# Validate input
if ! validate_key_repeat_rate "$KEY_REPEAT_RATE"; then
    log_message "Invalid key repeat rate: $KEY_REPEAT_RATE"
    exit 1
fi

# Get current user
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")

if [ -z "$CurrentUser" ] || [ "$CurrentUser" = "root" ]; then
    log_message "ERROR: No valid user session found"
    exit 1
fi

log_message "Current user: $CurrentUser (UID: $CurrentUserUID)"

# Set key repeat rate
if set_key_repeat_rate "$KEY_REPEAT_RATE" "$CurrentUser" "$CurrentUserUID"; then
    log_message "Key repeat rate configuration completed successfully"
else
    log_message "Key repeat rate configuration failed"
    exit 1
fi

log_message "Note: Changes will take effect after user logout/login"

Delay Until Repeat Configuration

Basic Delay Script

Configure the delay before key repetition begins:

#!/bin/bash

# Configure delay until repeat
# Values: 0-3 (0 = shortest delay, 3 = longest delay)
KEY_DELAY=1

echo "Configuring delay until repeat to: $KEY_DELAY"

# Get current user and user ID
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")

# Apply setting in user context
if launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" defaults write -g InitialKeyRepeat -int $KEY_DELAY; then
    echo "✓ Delay until repeat set successfully"
else
    echo "✗ Failed to set delay until repeat"
    exit 1
fi

echo "Changes will take effect after user logout/login"

Advanced Delay Configuration Script

Comprehensive script with validation and multiple user support:

#!/bin/bash

# Advanced delay until repeat configuration script
LOG_FILE="/var/log/keyboard_settings.log"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')

# Function to log messages
log_message() {
    echo "[$TIMESTAMP] $1" | tee -a "$LOG_FILE"
}

# Function to validate delay value
validate_delay() {
    local delay=$1
    
    if [[ ! "$delay" =~ ^[0-9]+$ ]]; then
        log_message "ERROR: Delay must be a number"
        return 1
    fi
    
    if [ "$delay" -lt 0 ] || [ "$delay" -gt 3 ]; then
        log_message "ERROR: Delay must be between 0-3"
        return 1
    fi
    
    return 0
}

# Function to get current delay setting
get_current_delay() {
    local current_user=$1
    local current_uid=$2
    
    local current_delay=$(launchctl asuser $current_uid sudo -iu "$current_user" defaults read -g InitialKeyRepeat 2>/dev/null)
    
    if [ -n "$current_delay" ]; then
        echo "$current_delay"
    else
        echo "Not set"
    fi
}

# Function to set delay until repeat
set_delay_until_repeat() {
    local delay=$1
    local current_user=$2
    local current_uid=$3
    
    log_message "Setting delay until repeat to: $delay for user: $current_user"
    
    # Get current setting
    local current_delay=$(get_current_delay "$current_user" "$current_uid")
    log_message "Current delay until repeat: $current_delay"
    
    # Apply new setting
    if launchctl asuser $current_uid sudo -iu "$current_user" defaults write -g InitialKeyRepeat -int $delay; then
        log_message "SUCCESS: Delay until repeat set to $delay"
        
        # Verify setting
        local new_delay=$(get_current_delay "$current_user" "$current_uid")
        log_message "Verified new delay until repeat: $new_delay"
        
        return 0
    else
        log_message "ERROR: Failed to set delay until repeat"
        return 1
    fi
}

# Main execution
KEY_DELAY=${1:-1}  # Default to 1 if not specified

log_message "Starting delay until repeat configuration"

# Validate input
if ! validate_delay "$KEY_DELAY"; then
    log_message "Invalid delay value: $KEY_DELAY"
    exit 1
fi

# Get current user
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")

if [ -z "$CurrentUser" ] || [ "$CurrentUser" = "root" ]; then
    log_message "ERROR: No valid user session found"
    exit 1
fi

log_message "Current user: $CurrentUser (UID: $CurrentUserUID)"

# Set delay until repeat
if set_delay_until_repeat "$KEY_DELAY" "$CurrentUser" "$CurrentUserUID"; then
    log_message "Delay until repeat configuration completed successfully"
else
    log_message "Delay until repeat configuration failed"
    exit 1
fi

log_message "Note: Changes will take effect after user logout/login"

Keyboard Navigation Configuration

Basic Keyboard Navigation Script

Enable keyboard navigation for better accessibility:

#!/bin/bash

# Enable keyboard navigation
echo "Enabling keyboard navigation..."

# Get current user and user ID
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")

# Enable keyboard navigation (3 = full keyboard access)
if launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" defaults write NSGlobalDomain AppleKeyboardUIMode -int 3; then
    echo "✓ Keyboard navigation enabled successfully"
else
    echo "✗ Failed to enable keyboard navigation"
    exit 1
fi

echo "Keyboard navigation is now enabled"
echo "Note: This feature requires macOS 13.0 or later"

Advanced Keyboard Navigation Script

Comprehensive script with version checking and configuration options:

#!/bin/bash

# Advanced keyboard navigation configuration script
LOG_FILE="/var/log/keyboard_settings.log"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')

# Function to log messages
log_message() {
    echo "[$TIMESTAMP] $1" | tee -a "$LOG_FILE"
}

# Function to check macOS version
check_macos_version() {
    local version=$(sw_vers -productVersion)
    local major_version=$(echo "$version" | cut -d. -f1)
    
    log_message "macOS version: $version"
    
    if [ "$major_version" -ge 13 ]; then
        log_message "macOS version is compatible with keyboard navigation"
        return 0
    else
        log_message "WARNING: macOS version may not fully support keyboard navigation"
        return 1
    fi
}

# Function to get current keyboard navigation setting
get_keyboard_navigation_status() {
    local current_user=$1
    local current_uid=$2
    
    local current_setting=$(launchctl asuser $current_uid sudo -iu "$current_user" defaults read NSGlobalDomain AppleKeyboardUIMode 2>/dev/null)
    
    if [ -n "$current_setting" ]; then
        case "$current_setting" in
            0) echo "Disabled (text boxes and lists only)" ;;
            1) echo "Enabled (text boxes and lists only)" ;;
            2) echo "Enabled (controls when Tab key is pressed)" ;;
            3) echo "Enabled (all controls)" ;;
            *) echo "Unknown ($current_setting)" ;;
        esac
    else
        echo "Not set (default)"
    fi
}

# Function to set keyboard navigation
set_keyboard_navigation() {
    local mode=$1
    local current_user=$2
    local current_uid=$3
    
    log_message "Setting keyboard navigation mode to: $mode for user: $current_user"
    
    # Get current setting
    local current_status=$(get_keyboard_navigation_status "$current_user" "$current_uid")
    log_message "Current keyboard navigation: $current_status"
    
    # Apply new setting
    if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain AppleKeyboardUIMode -int $mode; then
        log_message "SUCCESS: Keyboard navigation set to mode $mode"
        
        # Verify setting
        local new_status=$(get_keyboard_navigation_status "$current_user" "$current_uid")
        log_message "Verified new keyboard navigation: $new_status"
        
        return 0
    else
        log_message "ERROR: Failed to set keyboard navigation"
        return 1
    fi
}

# Main execution
KEYBOARD_MODE=${1:-3}  # Default to 3 (full keyboard access) if not specified

log_message "Starting keyboard navigation configuration"

# Check macOS version
check_macos_version

# Validate mode
if [[ ! "$KEYBOARD_MODE" =~ ^[0-3]$ ]]; then
    log_message "ERROR: Invalid keyboard mode: $KEYBOARD_MODE (must be 0-3)"
    exit 1
fi

# Get current user
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")

if [ -z "$CurrentUser" ] || [ "$CurrentUser" = "root" ]; then
    log_message "ERROR: No valid user session found"
    exit 1
fi

log_message "Current user: $CurrentUser (UID: $CurrentUserUID)"

# Set keyboard navigation
if set_keyboard_navigation "$KEYBOARD_MODE" "$CurrentUser" "$CurrentUserUID"; then
    log_message "Keyboard navigation configuration completed successfully"
else
    log_message "Keyboard navigation configuration failed"
    exit 1
fi

log_message "Keyboard navigation is now configured"

Auto-Correct Configuration

Basic Auto-Correct Disable Script

Disable automatic spelling correction:

#!/bin/bash

# Disable auto-correct
echo "Disabling auto-correct..."

# Get current user and user ID
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")

# Disable auto-correct
if launchctl asuser $CurrentUserUID sudo -iu "$CurrentUser" defaults write NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool false; then
    echo "✓ Auto-correct disabled successfully"
else
    echo "✗ Failed to disable auto-correct"
    exit 1
fi

echo "Auto-correct is now disabled"
echo "Note: Changes will take effect after user logout/login"

Advanced Auto-Correct Management Script

Comprehensive script for managing various auto-correct features:

#!/bin/bash

# Advanced auto-correct configuration script
LOG_FILE="/var/log/keyboard_settings.log"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')

# Function to log messages
log_message() {
    echo "[$TIMESTAMP] $1" | tee -a "$LOG_FILE"
}

# Function to get current auto-correct settings
get_autocorrect_settings() {
    local current_user=$1
    local current_uid=$2
    
    log_message "Getting current auto-correct settings for user: $current_user"
    
    # Spelling correction
    local spelling_correction=$(launchctl asuser $current_uid sudo -iu "$current_user" defaults read NSGlobalDomain NSAutomaticSpellingCorrectionEnabled 2>/dev/null)
    
    # Capitalize words
    local capitalize_words=$(launchctl asuser $current_uid sudo -iu "$current_user" defaults read NSGlobalDomain NSAutomaticCapitalizationEnabled 2>/dev/null)
    
    # Add period with double-space
    local period_substitution=$(launchctl asuser $current_uid sudo -iu "$current_user" defaults read NSGlobalDomain NSAutomaticPeriodSubstitutionEnabled 2>/dev/null)
    
    # Smart quotes
    local smart_quotes=$(launchctl asuser $current_uid sudo -iu "$current_user" defaults read NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled 2>/dev/null)
    
    # Smart dashes
    local smart_dashes=$(launchctl asuser $current_uid sudo -iu "$current_user" defaults read NSGlobalDomain NSAutomaticDashSubstitutionEnabled 2>/dev/null)
    
    echo "Current Auto-Correct Settings:"
    echo "  Spelling Correction: ${spelling_correction:-Not set}"
    echo "  Capitalize Words: ${capitalize_words:-Not set}"
    echo "  Period Substitution: ${period_substitution:-Not set}"
    echo "  Smart Quotes: ${smart_quotes:-Not set}"
    echo "  Smart Dashes: ${smart_dashes:-Not set}"
}

# Function to configure auto-correct features
configure_autocorrect() {
    local current_user=$1
    local current_uid=$2
    local spelling_correction=$3
    local capitalize_words=$4
    local period_substitution=$5
    local smart_quotes=$6
    local smart_dashes=$7
    
    log_message "Configuring auto-correct settings for user: $current_user"
    
    local success=0
    
    # Configure spelling correction
    if [ -n "$spelling_correction" ]; then
        if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool $spelling_correction; then
            log_message "SUCCESS: Spelling correction set to $spelling_correction"
        else
            log_message "ERROR: Failed to set spelling correction"
            success=1
        fi
    fi
    
    # Configure capitalize words
    if [ -n "$capitalize_words" ]; then
        if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticCapitalizationEnabled -bool $capitalize_words; then
            log_message "SUCCESS: Capitalize words set to $capitalize_words"
        else
            log_message "ERROR: Failed to set capitalize words"
            success=1
        fi
    fi
    
    # Configure period substitution
    if [ -n "$period_substitution" ]; then
        if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticPeriodSubstitutionEnabled -bool $period_substitution; then
            log_message "SUCCESS: Period substitution set to $period_substitution"
        else
            log_message "ERROR: Failed to set period substitution"
            success=1
        fi
    fi
    
    # Configure smart quotes
    if [ -n "$smart_quotes" ]; then
        if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled -bool $smart_quotes; then
            log_message "SUCCESS: Smart quotes set to $smart_quotes"
        else
            log_message "ERROR: Failed to set smart quotes"
            success=1
        fi
    fi
    
    # Configure smart dashes
    if [ -n "$smart_dashes" ]; then
        if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticDashSubstitutionEnabled -bool $smart_dashes; then
            log_message "SUCCESS: Smart dashes set to $smart_dashes"
        else
            log_message "ERROR: Failed to set smart dashes"
            success=1
        fi
    fi
    
    return $success
}

# Main execution
log_message "Starting auto-correct configuration"

# Configuration options (change as needed)
SPELLING_CORRECTION=${1:-false}
CAPITALIZE_WORDS=${2:-false}
PERIOD_SUBSTITUTION=${3:-false}
SMART_QUOTES=${4:-false}
SMART_DASHES=${5:-false}

# Get current user
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")

if [ -z "$CurrentUser" ] || [ "$CurrentUser" = "root" ]; then
    log_message "ERROR: No valid user session found"
    exit 1
fi

log_message "Current user: $CurrentUser (UID: $CurrentUserUID)"

# Show current settings
get_autocorrect_settings "$CurrentUser" "$CurrentUserUID"

# Configure auto-correct
if configure_autocorrect "$CurrentUser" "$CurrentUserUID" "$SPELLING_CORRECTION" "$CAPITALIZE_WORDS" "$PERIOD_SUBSTITUTION" "$SMART_QUOTES" "$SMART_DASHES"; then
    log_message "Auto-correct configuration completed successfully"
else
    log_message "Some auto-correct configurations failed"
    exit 1
fi

log_message "Note: Changes will take effect after user logout/login"

Comprehensive Keyboard Configuration

All-in-One Configuration Script

Script to configure all keyboard settings at once:

#!/bin/bash

# Comprehensive keyboard configuration script
LOG_FILE="/var/log/keyboard_settings.log"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')

# Configuration variables
KEY_REPEAT_RATE=2
KEY_DELAY=1
KEYBOARD_NAVIGATION=3
DISABLE_AUTOCORRECT=true
DISABLE_CAPITALIZE=true
DISABLE_PERIOD_SUB=true
DISABLE_SMART_QUOTES=true
DISABLE_SMART_DASHES=true

# Function to log messages
log_message() {
    echo "[$TIMESTAMP] $1" | tee -a "$LOG_FILE"
}

# Function to apply all keyboard settings
apply_keyboard_settings() {
    local current_user=$1
    local current_uid=$2
    
    log_message "Applying comprehensive keyboard settings for user: $current_user"
    
    local success=0
    
    # Key repeat rate
    if launchctl asuser $current_uid sudo -iu "$current_user" defaults write -g KeyRepeat -int $KEY_REPEAT_RATE; then
        log_message "SUCCESS: Key repeat rate set to $KEY_REPEAT_RATE"
    else
        log_message "ERROR: Failed to set key repeat rate"
        success=1
    fi
    
    # Delay until repeat
    if launchctl asuser $current_uid sudo -iu "$current_user" defaults write -g InitialKeyRepeat -int $KEY_DELAY; then
        log_message "SUCCESS: Delay until repeat set to $KEY_DELAY"
    else
        log_message "ERROR: Failed to set delay until repeat"
        success=1
    fi
    
    # Keyboard navigation
    if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain AppleKeyboardUIMode -int $KEYBOARD_NAVIGATION; then
        log_message "SUCCESS: Keyboard navigation set to $KEYBOARD_NAVIGATION"
    else
        log_message "ERROR: Failed to set keyboard navigation"
        success=1
    fi
    
    # Auto-correct features
    if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool $DISABLE_AUTOCORRECT; then
        log_message "SUCCESS: Auto-correct disabled"
    else
        log_message "ERROR: Failed to disable auto-correct"
        success=1
    fi
    
    if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticCapitalizationEnabled -bool $DISABLE_CAPITALIZE; then
        log_message "SUCCESS: Auto-capitalize disabled"
    else
        log_message "ERROR: Failed to disable auto-capitalize"
        success=1
    fi
    
    if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticPeriodSubstitutionEnabled -bool $DISABLE_PERIOD_SUB; then
        log_message "SUCCESS: Period substitution disabled"
    else
        log_message "ERROR: Failed to disable period substitution"
        success=1
    fi
    
    if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticQuoteSubstitutionEnabled -bool $DISABLE_SMART_QUOTES; then
        log_message "SUCCESS: Smart quotes disabled"
    else
        log_message "ERROR: Failed to disable smart quotes"
        success=1
    fi
    
    if launchctl asuser $current_uid sudo -iu "$current_user" defaults write NSGlobalDomain NSAutomaticDashSubstitutionEnabled -bool $DISABLE_SMART_DASHES; then
        log_message "SUCCESS: Smart dashes disabled"
    else
        log_message "ERROR: Failed to disable smart dashes"
        success=1
    fi
    
    return $success
}

# Main execution
log_message "Starting comprehensive keyboard configuration"

# Get current user
CurrentUser=$(ls -l /dev/console | awk '/ / { print $3 }')
CurrentUserUID=$(id -u "$CurrentUser")

if [ -z "$CurrentUser" ] || [ "$CurrentUser" = "root" ]; then
    log_message "ERROR: No valid user session found"
    exit 1
fi

log_message "Current user: $CurrentUser (UID: $CurrentUserUID)"

# Apply all settings
if apply_keyboard_settings "$CurrentUser" "$CurrentUserUID"; then
    log_message "Comprehensive keyboard configuration completed successfully"
    echo "✓ All keyboard settings applied successfully"
else
    log_message "Some keyboard configurations failed"
    echo "⚠ Some settings may not have been applied. Check log: $LOG_FILE"
    exit 1
fi

log_message "Note: Changes will take effect after user logout/login"
echo "Note: Please log out and log back in for changes to take effect"

Enterprise Deployment Scripts

Multi-User Configuration Script

Script for applying settings to multiple users:

#!/bin/bash

# Multi-user keyboard configuration script
LOG_FILE="/var/log/keyboard_settings.log"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')

# Configuration
KEY_REPEAT_RATE=2
KEY_DELAY=1
KEYBOARD_NAVIGATION=3
DISABLE_AUTOCORRECT=false

# Function to log messages
log_message() {
    echo "[$TIMESTAMP] $1" | tee -a "$LOG_FILE"
}

# Function to get all users
get_all_users() {
    # Get users with UID >= 500 (regular users)
    local users=$(dscl . -list /Users UniqueID | awk '$2 >= 500 { print $1 }')
    echo "$users"
}

# Function to configure user settings
configure_user_keyboard() {
    local username=$1
    local user_uid=$(id -u "$username" 2>/dev/null)
    
    if [ -z "$user_uid" ]; then
        log_message "ERROR: Cannot get UID for user: $username"
        return 1
    fi
    
    log_message "Configuring keyboard settings for user: $username (UID: $user_uid)"
    
    # Check if user has a home directory
    local home_dir=$(eval echo ~$username)
    if [ ! -d "$home_dir" ]; then
        log_message "WARNING: Home directory not found for user: $username"
        return 1
    fi
    
    # Apply settings
    launchctl asuser $user_uid sudo -iu "$username" defaults write -g KeyRepeat -int $KEY_REPEAT_RATE
    launchctl asuser $user_uid sudo -iu "$username" defaults write -g InitialKeyRepeat -int $KEY_DELAY
    launchctl asuser $user_uid sudo -iu "$username" defaults write NSGlobalDomain AppleKeyboardUIMode -int $KEYBOARD_NAVIGATION
    launchctl asuser $user_uid sudo -iu "$username" defaults write NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool $DISABLE_AUTOCORRECT
    
    log_message "SUCCESS: Keyboard settings configured for user: $username"
    return 0
}

# Main execution
log_message "Starting multi-user keyboard configuration"

# Get all users
users=$(get_all_users)

if [ -z "$users" ]; then
    log_message "ERROR: No users found"
    exit 1
fi

log_message "Found users: $users"

# Configure each user
successful=0
failed=0

for user in $users; do
    if configure_user_keyboard "$user"; then
        ((successful++))
    else
        ((failed++))
    fi
done

log_message "Multi-user configuration completed"
log_message "Successfully configured: $successful users"
log_message "Failed: $failed users"

echo "Configuration Summary:"
echo "  Successfully configured: $successful users"
echo "  Failed: $failed users"
echo "  Log file: $LOG_FILE"

Remote Deployment Script

Script for deploying configurations remotely:

#!/bin/bash

# Remote keyboard configuration deployment script
REMOTE_HOSTS=(
    "10.0.1.10"
    "10.0.1.11"
    "10.0.1.12"
    # Add more hosts as needed
)

SSH_USER="admin"
SSH_KEY_PATH="/path/to/ssh/key"
LOG_FILE="/var/log/remote_keyboard_deployment.log"

# Function to log messages
log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}

# Function to deploy to remote host
deploy_to_host() {
    local host=$1
    
    log_message "Deploying keyboard configuration to host: $host"
    
    # Copy script to remote host
    if scp -i "$SSH_KEY_PATH" -o StrictHostKeyChecking=no ./keyboard_config.sh "$SSH_USER@$host:/tmp/"; then
        log_message "Script copied to $host"
    else
        log_message "ERROR: Failed to copy script to $host"
        return 1
    fi
    
    # Execute script on remote host
    if ssh -i "$SSH_KEY_PATH" -o StrictHostKeyChecking=no "$SSH_USER@$host" "chmod +x /tmp/keyboard_config.sh && sudo /tmp/keyboard_config.sh"; then
        log_message "SUCCESS: Configuration applied on $host"
        return 0
    else
        log_message "ERROR: Failed to apply configuration on $host"
        return 1
    fi
}

# Main execution
log_message "Starting remote keyboard configuration deployment"

successful=0
failed=0

for host in "${REMOTE_HOSTS[@]}"; do
    if deploy_to_host "$host"; then
        ((successful++))
    else
        ((failed++))
    fi
done

log_message "Remote deployment completed"
log_message "Successfully deployed: $successful hosts"
log_message "Failed: $failed hosts"

echo "Deployment Summary:"
echo "  Successfully deployed: $successful hosts"
echo "  Failed: $failed hosts"
echo "  Log file: $LOG_FILE"

Best Practices and Recommendations

1. User Context Management

  • Always apply settings in the correct user context
  • Use launchctl asuser for user-specific settings
  • Verify current user before applying configurations

2. Setting Validation

  • Validate input values before applying settings
  • Check for existing settings before modification
  • Verify settings after application

3. Error Handling

  • Implement comprehensive error checking
  • Log all operations for troubleshooting
  • Provide clear error messages

4. Testing and Deployment

  • Test scripts on individual devices before mass deployment
  • Use staged rollouts for large environments
  • Monitor for issues after deployment

5. Documentation

  • Document all customizations
  • Maintain configuration version control
  • Keep logs of all changes

Conclusion

Effective keyboard configuration management is essential for maintaining consistent user experiences across Mac fleets. The scripts and techniques provided in this guide offer comprehensive solutions for various keyboard configuration scenarios.

Key takeaways:

  • Use appropriate user context for setting application
  • Implement proper validation and error handling
  • Test configurations before mass deployment
  • Monitor and log all configuration changes
  • Consider user preferences and accessibility needs

Remember that keyboard settings are highly personal, so consider providing options for users to customize their experience while maintaining enterprise standards where necessary.

Manage Kernel Extensions on macOS

Monitor and manage kernel extensions across your Roundfleet devices using command-line tools. This tutorial covers displaying loaded kexts, loading/unloading extensions, finding specific extensions, and enterprise security monitoring.

Understanding macOS Kernel Extensions

Kernel Extensions (kexts) are drivers that enable the macOS kernel to communicate with hardware devices and system components. Understanding kext management is crucial for:

  • Hardware compatibility - Device drivers and hardware communication
  • System stability - Monitoring potentially problematic extensions
  • Security assessment - Identifying unauthorized or malicious kexts
  • Compliance requirements - Enterprise security and audit trails

Key tools for kext management:

  • kextstat - Display loaded kernel extensions
  • kextload - Load kernel extensions
  • kextunload - Unload kernel extensions
  • kextfind - Find kernel extensions by criteria

Display Loaded Kernel Extensions

List All Loaded Extensions

#!/bin/bash

# Display all currently loaded kernel extensions
kextstat

echo "Kernel extensions list retrieved successfully"

Show Specific Extension Details

#!/bin/bash

# Display details of a specific kernel extension by bundle ID
BUNDLE_ID="${1:-com.apple.kpi.bsd}"

echo "Searching for kernel extension: $BUNDLE_ID"
kextstat | grep "$BUNDLE_ID"

if [[ $? -eq 0 ]]; then
    echo "Extension found and loaded"
else
    echo "Extension not found or not loaded"
fi

List Third-Party Extensions Only

#!/bin/bash

# Display only third-party (non-Apple) kernel extensions
echo "=== Third-Party Kernel Extensions ==="
echo "Device: $(hostname)"
echo "Date: $(date)"
echo "====================================="

echo -e "\n🔍 Non-Apple Kernel Extensions:"
third_party_kexts=$(kextstat | grep -v com.apple)

if [[ -n "$third_party_kexts" ]]; then
    echo "$third_party_kexts"
    echo -e "\n📊 Third-party extension count: $(echo "$third_party_kexts" | wc -l)"
else
    echo "No third-party kernel extensions detected"
fi

Comprehensive Kext Analysis

#!/bin/bash

# Detailed kernel extension analysis
echo "=== Roundfleet Kernel Extension Analysis ==="
echo "Device: $(hostname)"
echo "OS Version: $(sw_vers -productVersion)"
echo "Kernel Version: $(uname -r)"
echo "Date: $(date)"
echo "=========================================="

# Total loaded extensions
total_kexts=$(kextstat | tail -n +2 | wc -l)
echo "🔧 Total loaded extensions: $total_kexts"

# Apple vs third-party breakdown
apple_kexts=$(kextstat | grep -c com.apple)
third_party_kexts=$(kextstat | grep -v com.apple | tail -n +2 | wc -l)

echo "🍎 Apple extensions: $apple_kexts"
echo "🔌 Third-party extensions: $third_party_kexts"

# Show third-party extensions if any
if [[ $third_party_kexts -gt 0 ]]; then
    echo -e "\n⚠️  Third-Party Extensions:"
    kextstat | grep -v com.apple | tail -n +2
fi

# Check for potentially suspicious extensions
echo -e "\n🔒 Security Analysis:"
suspicious_patterns=("hack" "crack" "cheat" "bypass" "inject")
for pattern in "${suspicious_patterns[@]}"; do
    suspicious=$(kextstat | grep -i "$pattern")
    if [[ -n "$suspicious" ]]; then
        echo "🚨 Suspicious extension detected: $suspicious"
    fi
done

echo "✅ Security analysis completed"

Load Kernel Extensions

Basic Extension Loading

#!/bin/bash

# Load a kernel extension by path
KEXT_PATH="${1}"

if [[ -z "$KEXT_PATH" ]]; then
    echo "Usage: $0 <path_to_kext>"
    echo "Example: $0 /System/Library/Extensions/FakeSMC.kext"
    exit 1
fi

if [[ ! -d "$KEXT_PATH" ]]; then
    echo "❌ Kernel extension not found: $KEXT_PATH"
    exit 1
fi

echo "Loading kernel extension: $KEXT_PATH"
if sudo kextload "$KEXT_PATH"; then
    echo "✅ Kernel extension loaded successfully"
else
    echo "❌ Failed to load kernel extension"
    exit 1
fi

Safe Extension Loading with Verification

#!/bin/bash

# Safely load kernel extension with verification
load_kext_safely() {
    local kext_path="$1"
    local bundle_id="$2"
    
    if [[ -z "$kext_path" ]]; then
        echo "❌ Kernel extension path required"
        return 1
    fi
    
    # Verify extension exists
    if [[ ! -d "$kext_path" ]]; then
        echo "❌ Kernel extension not found: $kext_path"
        return 1
    fi
    
    # Check if already loaded
    if [[ -n "$bundle_id" ]]; then
        if kextstat | grep -q "$bundle_id"; then
            echo "⚠️  Extension already loaded: $bundle_id"
            return 0
        fi
    fi
    
    # Validate extension
    echo "🔍 Validating kernel extension..."
    if ! kextutil -t "$kext_path" 2>/dev/null; then
        echo "❌ Extension validation failed"
        return 1
    fi
    
    # Load the extension
    echo "📦 Loading kernel extension: $kext_path"
    if sudo kextload "$kext_path"; then
        echo "✅ Kernel extension loaded successfully"
        
        # Verify it's actually loaded
        if [[ -n "$bundle_id" ]]; then
            sleep 1
            if kextstat | grep -q "$bundle_id"; then
                echo "✅ Extension verified as loaded"
            else
                echo "⚠️  Extension load status unclear"
            fi
        fi
        
        return 0
    else
        echo "❌ Failed to load kernel extension"
        return 1
    fi
}

# Example usage
# load_kext_safely "/System/Library/Extensions/FakeSMC.kext" "com.apple.driver.FakeSMC"

Enterprise Extension Loading

#!/bin/bash

# Enterprise kernel extension loading with logging
LOG_FILE="/var/log/roundfleet_kext_operations.log"

# Logging function
log_action() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}

load_enterprise_kext() {
    local kext_path="$1"
    local description="$2"
    
    log_action "=== Kernel Extension Load Operation ==="
    log_action "Device: $(hostname)"
    log_action "User: $(whoami)"
    log_action "Extension: $kext_path"
    log_action "Description: ${description:-No description provided}"
    
    # Pre-load checks
    if [[ ! -d "$kext_path" ]]; then
        log_action "ERROR: Extension not found at path: $kext_path"
        return 1
    fi
    
    # Check current system state
    local pre_count=$(kextstat | tail -n +2 | wc -l)
    log_action "Pre-load extension count: $pre_count"
    
    # Attempt to load
    log_action "Attempting to load extension..."
    if sudo kextload "$kext_path" 2>&1 | tee -a "$LOG_FILE"; then
        local post_count=$(kextstat | tail -n +2 | wc -l)
        log_action "SUCCESS: Extension loaded"
        log_action "Post-load extension count: $post_count"
        
        # Generate load report
        {
            echo "Roundfleet Kernel Extension Load Report"
            echo "Time: $(date)"
            echo "Extension: $kext_path"
            echo "Status: SUCCESS"
            echo "Pre-load count: $pre_count"
            echo "Post-load count: $post_count"
        } >> "$LOG_FILE"
        
        return 0
    else
        log_action "ERROR: Failed to load extension"
        return 1
    fi
}

# Usage: load_enterprise_kext "/path/to/extension.kext" "Description"

Unload Kernel Extensions

Basic Extension Unloading

#!/bin/bash

# Unload kernel extension by path
KEXT_PATH="${1}"

if [[ -z "$KEXT_PATH" ]]; then
    echo "Usage: $0 <path_to_kext>"
    echo "Example: $0 /System/Library/Extensions/FakeSMC.kext"
    exit 1
fi

echo "Unloading kernel extension: $KEXT_PATH"
if sudo kextunload "$KEXT_PATH"; then
    echo "✅ Kernel extension unloaded successfully"
else
    echo "❌ Failed to unload kernel extension"
    exit 1
fi

Unload by Bundle ID

#!/bin/bash

# Unload kernel extension by bundle identifier
BUNDLE_ID="${1}"

if [[ -z "$BUNDLE_ID" ]]; then
    echo "Usage: $0 <bundle_id>"
    echo "Example: $0 com.apple.driver.FakeSMC"
    exit 1
fi

# Check if extension is loaded
if ! kextstat | grep -q "$BUNDLE_ID"; then
    echo "⚠️  Extension not currently loaded: $BUNDLE_ID"
    exit 0
fi

echo "Unloading kernel extension: $BUNDLE_ID"
if sudo kextunload -b "$BUNDLE_ID"; then
    echo "✅ Kernel extension unloaded successfully"
    
    # Verify unload
    sleep 1
    if ! kextstat | grep -q "$BUNDLE_ID"; then
        echo "✅ Extension confirmed unloaded"
    else
        echo "⚠️  Extension may still be loaded"
    fi
else
    echo "❌ Failed to unload kernel extension"
    exit 1
fi

Safe Extension Unloading

#!/bin/bash

# Safely unload kernel extensions with dependency checking
unload_kext_safely() {
    local bundle_id="$1"
    local force_unload="${2:-false}"
    
    if [[ -z "$bundle_id" ]]; then
        echo "❌ Bundle ID required"
        return 1
    fi
    
    # Check if extension is loaded
    if ! kextstat | grep -q "$bundle_id"; then
        echo "ℹ️  Extension not loaded: $bundle_id"
        return 0
    fi
    
    # Check for Apple extensions (warn but allow)
    if [[ "$bundle_id" == com.apple.* ]]; then
        echo "⚠️  WARNING: Attempting to unload Apple system extension"
        echo "This may cause system instability"
        
        if [[ "$force_unload" != "true" ]]; then
            echo "❌ Aborting unload of Apple extension (use force_unload=true to override)"
            return 1
        fi
    fi
    
    # Show extension info before unload
    echo "🔍 Extension information:"
    kextstat | grep "$bundle_id"
    
    # Attempt unload
    echo "📤 Unloading extension: $bundle_id"
    if sudo kextunload -b "$bundle_id"; then
        echo "✅ Extension unloaded successfully"
        
        # Verify unload
        sleep 2
        if ! kextstat | grep -q "$bundle_id"; then
            echo "✅ Extension confirmed unloaded"
            return 0
        else
            echo "⚠️  Extension still appears to be loaded"
            return 1
        fi
    else
        echo "❌ Failed to unload extension"
        return 1
    fi
}

# Example usage
# unload_kext_safely "com.third.party.driver"
# unload_kext_safely "com.apple.driver.something" "true"  # Force Apple extension

Find Kernel Extensions

Find by Bundle ID

#!/bin/bash

# Find kernel extensions by bundle ID
BUNDLE_ID="${1}"

if [[ -z "$BUNDLE_ID" ]]; then
    echo "Usage: $0 <bundle_id>"
    echo "Example: $0 com.apple.driver.AppleHDA"
    exit 1
fi

echo "Searching for kernel extension: $BUNDLE_ID"
kextfind -b "$BUNDLE_ID"

# Also check if it's currently loaded
echo -e "\nLoad status:"
if kextstat | grep -q "$BUNDLE_ID"; then
    echo "✅ Extension is currently loaded"
    kextstat | grep "$BUNDLE_ID"
else
    echo "❌ Extension is not currently loaded"
fi

Comprehensive Extension Search

#!/bin/bash

# Comprehensive kernel extension search and analysis
search_kext_comprehensive() {
    local search_term="$1"
    
    if [[ -z "$search_term" ]]; then
        echo "Usage: search_kext_comprehensive <search_term>"
        return 1
    fi
    
    echo "=== Comprehensive Kernel Extension Search ==="
    echo "Search term: $search_term"
    echo "Device: $(hostname)"
    echo "Date: $(date)"
    echo "============================================="
    
    # Search by bundle ID
    echo -e "\n🔍 Searching by bundle ID:"
    local found_by_bundle=$(kextfind -b "*$search_term*" 2>/dev/null)
    if [[ -n "$found_by_bundle" ]]; then
        echo "$found_by_bundle"
    else
        echo "No extensions found matching bundle ID pattern"
    fi
    
    # Search loaded extensions
    echo -e "\n📋 Checking loaded extensions:"
    local loaded_matches=$(kextstat | grep -i "$search_term")
    if [[ -n "$loaded_matches" ]]; then
        echo "Found in loaded extensions:"
        echo "$loaded_matches"
    else
        echo "No loaded extensions match search term"
    fi
    
    # Search filesystem
    echo -e "\n💾 Searching filesystem:"
    echo "System extensions directory:"
    find /System/Library/Extensions -name "*$search_term*" -type d 2>/dev/null | head -10
    
    echo -e "\nLibrary extensions directory:"
    find /Library/Extensions -name "*$search_term*" -type d 2>/dev/null | head -10
    
    echo -e "\n✅ Search completed"
}

# Example usage
# search_kext_comprehensive "bluetooth"
# search_kext_comprehensive "audio"

Find Extensions by Pattern

#!/bin/bash

# Find kernel extensions matching various patterns
echo "=== Kernel Extension Pattern Search ==="

# Audio-related extensions
echo -e "\n🔊 Audio Extensions:"
kextstat | grep -i -E "(audio|sound|hda)"

# Network-related extensions
echo -e "\n🌐 Network Extensions:"
kextstat | grep -i -E "(network|wifi|ethernet|bluetooth)"

# Graphics-related extensions
echo -e "\n🖥️  Graphics Extensions:"
kextstat | grep -i -E "(graphics|display|amd|nvidia|intel)"

# USB-related extensions
echo -e "\n🔌 USB Extensions:"
kextstat | grep -i -E "(usb|port)"

# Security-related extensions
echo -e "\n🔒 Security Extensions:"
kextstat | grep -i -E "(security|firewall|antivirus)"

# Virtualization extensions
echo -e "\n☁️  Virtualization Extensions:"
kextstat | grep -i -E "(vmware|parallels|virtual|hypervisor)"

Enterprise Kernel Extension Monitoring

Security Monitoring Script

#!/bin/bash

# Enterprise kernel extension security monitoring
LOG_FILE="/var/log/roundfleet_kext_security.log"
ALERT_FILE="/var/log/roundfleet_kext_alerts.log"

# Logging function
log_security() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}

# Alert function
send_alert() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - ALERT: $1" | tee -a "$ALERT_FILE"
}

# Security assessment
assess_kext_security() {
    log_security "=== Kernel Extension Security Assessment ==="
    log_security "Device: $(hostname)"
    log_security "OS Version: $(sw_vers -productVersion)"
    
    # Count extensions
    local total_kexts=$(kextstat | tail -n +2 | wc -l)
    local apple_kexts=$(kextstat | grep -c com.apple)
    local third_party=$(kextstat | grep -v com.apple | tail -n +2 | wc -l)
    
    log_security "Total extensions: $total_kexts"
    log_security "Apple extensions: $apple_kexts"
    log_security "Third-party extensions: $third_party"
    
    # Alert on high third-party count
    if [[ $third_party -gt 5 ]]; then
        send_alert "High number of third-party extensions: $third_party"
    fi
    
    # Check for suspicious extensions
    local suspicious_keywords=("hack" "crack" "cheat" "bypass" "inject" "hook" "patch")
    for keyword in "${suspicious_keywords[@]}"; do
        local matches=$(kextstat | grep -i "$keyword")
        if [[ -n "$matches" ]]; then
            send_alert "Suspicious extension detected ($keyword): $matches"
        fi
    done
    
    # Check for unsigned extensions
    echo -e "\n🔍 Checking extension signatures..."
    kextstat | grep -v com.apple | tail -n +2 | while read line; do
        local bundle_id=$(echo "$line" | awk '{print $6}')
        if [[ -n "$bundle_id" ]]; then
            # This is a simplified check - in reality, you'd want more sophisticated signature verification
            log_security "Third-party extension: $bundle_id"
        fi
    done
    
    log_security "Security assessment completed"
}

# Generate security report
generate_security_report() {
    local report_file="/tmp/kext_security_report_$(date +%Y%m%d_%H%M%S).txt"
    
    {
        echo "Roundfleet Kernel Extension Security Report"
        echo "Generated: $(date)"
        echo "Device: $(hostname)"
        echo "OS Version: $(sw_vers -productVersion)"
        echo "========================================"
        echo ""
        
        echo "📊 Extension Statistics:"
        echo "Total loaded: $(kextstat | tail -n +2 | wc -l)"
        echo "Apple extensions: $(kextstat | grep -c com.apple)"
        echo "Third-party: $(kextstat | grep -v com.apple | tail -n +2 | wc -l)"
        echo ""
        
        echo "🔍 Third-Party Extensions:"
        kextstat | grep -v com.apple | tail -n +2
        echo ""
        
        echo "🔒 Security Recommendations:"
        local third_party_count=$(kextstat | grep -v com.apple | tail -n +2 | wc -l)
        if [[ $third_party_count -eq 0 ]]; then
            echo "✅ No third-party extensions detected"
        else
            echo "⚠️  Review all third-party extensions for necessity"
            echo "⚠️  Verify digital signatures and sources"
            echo "⚠️  Consider removing unused extensions"
        fi
        
    } > "$report_file"
    
    echo "📋 Security report generated: $report_file"
}

# Main security monitoring
main() {
    assess_kext_security
    generate_security_report
}

# Execute monitoring
main "$@"

Extension Change Monitoring

#!/bin/bash

# Monitor kernel extension changes over time
BASELINE_FILE="/var/lib/roundfleet/kext_baseline.txt"
CHANGES_LOG="/var/log/roundfleet_kext_changes.log"

# Create directories if needed
sudo mkdir -p "$(dirname "$BASELINE_FILE")"
sudo mkdir -p "$(dirname "$CHANGES_LOG")"

# Logging function
log_change() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | sudo tee -a "$CHANGES_LOG"
}

# Create or update baseline
create_baseline() {
    echo "📋 Creating kernel extension baseline..."
    kextstat > "$BASELINE_FILE"
    log_change "Baseline created with $(kextstat | tail -n +2 | wc -l) extensions"
}

# Check for changes
check_changes() {
    if [[ ! -f "$BASELINE_FILE" ]]; then
        echo "⚠️  No baseline found, creating one..."
        create_baseline
        return
    fi
    
    local current_file="/tmp/kext_current_$(date +%Y%m%d_%H%M%S).txt"
    kextstat > "$current_file"
    
    # Compare with baseline
    local changes=$(diff "$BASELINE_FILE" "$current_file")
    
    if [[ -n "$changes" ]]; then
        log_change "Kernel extension changes detected:"
        echo "$changes" | sudo tee -a "$CHANGES_LOG"
        
        # Analyze changes
        local added=$(echo "$changes" | grep "^>" | wc -l)
        local removed=$(echo "$changes" | grep "^<" | wc -l)
        
        log_change "Extensions added: $added"
        log_change "Extensions removed: $removed"
        
        echo "🔄 Changes detected - check $CHANGES_LOG for details"
    else
        echo "✅ No changes detected since baseline"
    fi
    
    # Clean up
    rm -f "$current_file"
}

# Usage
case "${1:-check}" in
    "baseline")
        create_baseline
        ;;
    "check")
        check_changes
        ;;
    *)
        echo "Usage: $0 [baseline|check]"
        ;;
esac

Kernel Extension Reference

Common Extension Categories

CategoryBundle ID PatternPurpose
Audiocom.apple.driver.*HDA*Audio hardware drivers
Graphicscom.apple.driver.*Graphics*Display and GPU drivers
Networkcom.apple.driver.*Ethernet*Network interface drivers
USBcom.apple.driver.*USB*USB device support
Storagecom.apple.driver.*Storage*Disk and storage drivers
Bluetoothcom.apple.driver.*Bluetooth*Bluetooth connectivity
Securitycom.apple.security.*Security frameworks

Command Reference

# List all loaded extensions
kextstat

# List with specific columns
kextstat -l

# Find extension by bundle ID
kextfind -b com.apple.driver.AppleHDA

# Load extension
sudo kextload /path/to/extension.kext

# Unload by path
sudo kextunload /path/to/extension.kext

# Unload by bundle ID
sudo kextunload -b com.example.driver

# Test extension without loading
kextutil -t /path/to/extension.kext

# Show extension dependencies
kextutil -d /path/to/extension.kext

Important Security Notes

  • Apple Extensions - Generally safe but unloading may cause instability
  • Third-party Extensions - Require careful security assessment
  • Unsigned Extensions - Potential security risk, audit carefully
  • Administrative Privileges - Loading/unloading requires sudo access
  • System Impact - Extension changes can affect system stability
  • Recovery Mode - May be needed if system becomes unstable

Troubleshooting

Common Issues

Extension Won't Load:

# Check extension validity
kextutil -t /path/to/extension.kext

# Check system logs
log show --predicate 'subsystem == "com.apple.kernel"' --last 1h

Extension Won't Unload:

# Check dependencies
kextutil -d /path/to/extension.kext

# Force unload (dangerous)
sudo kextunload -b bundle.id -f

Permission Denied:

# Ensure admin privileges
sudo -v

# Check System Integrity Protection
csrutil status

Remember to test kernel extension operations on individual devices before deploying across your Roundfleet environment, as improper kext management can cause system instability.

Install Unsigned Packages on macOS

Learn how to install unsigned packages on Mac devices using scripts. Unsigned packages are software packages that haven't been digitally signed by the developer, often required for specialized development tools and open-source software.

Basic Package Installation Script

Install an unsigned package from a URL:

#!/bin/bash

# Download and install unsigned package
PACKAGE_NAME="your_package"
DOWNLOAD_URL="https://example.com/package.pkg"

# Download package to temporary directory
curl -o /tmp/${PACKAGE_NAME}.pkg "${DOWNLOAD_URL}"

# Install package with admin privileges
sudo installer -verboseR -pkg /tmp/${PACKAGE_NAME}.pkg -target /

# Clean up
rm /tmp/${PACKAGE_NAME}.pkg

echo "Package installation completed"

Enhanced Installation with Validation

Script with error handling and validation:

#!/bin/bash

PACKAGE_NAME="your_package"
DOWNLOAD_URL="https://example.com/package.pkg"
TEMP_PATH="/tmp/${PACKAGE_NAME}.pkg"

# Function to log messages
log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S'): $1"
}

# Download package
log_message "Downloading package from ${DOWNLOAD_URL}"
if curl -f -o "${TEMP_PATH}" "${DOWNLOAD_URL}"; then
    log_message "Download successful"
else
    log_message "Download failed"
    exit 1
fi

# Verify file exists and has content
if [[ -f "${TEMP_PATH}" && -s "${TEMP_PATH}" ]]; then
    log_message "Package file validated"
else
    log_message "Package file validation failed"
    exit 1
fi

# Install package
log_message "Installing package"
if sudo installer -verboseR -pkg "${TEMP_PATH}" -target /; then
    log_message "Installation completed successfully"
else
    log_message "Installation failed"
    exit 1
fi

# Clean up
rm "${TEMP_PATH}"
log_message "Cleanup completed"

Usage with Roundfleet

  1. Replace PACKAGE_NAME with your desired package name
  2. Replace DOWNLOAD_URL with the actual package URL
  3. Deploy the script through Roundfleet's remote script execution
  4. Monitor installation progress through the action history

Security Considerations

Warning: Installing unsigned packages may compromise security as their authenticity cannot be verified. Only install packages from trusted sources.

  • Verify package sources before deployment
  • Test on a limited set of devices first
  • Monitor for any security alerts post-installation

Troubleshooting

Installation fails: Check network connectivity and URL accessibility Permission denied: Ensure script runs with administrative privileges Package corrupted: Verify download completed successfully


Note: Always validate script execution on test systems before bulk deployment. Roundfleet is not responsible for system damage from unsigned package installations.

Install Latest Firefox Version on macOS Devices

This comprehensive guide demonstrates how to install and manage the latest Firefox version on macOS devices for enterprise deployment, automatic updates, and configuration management.

Overview

Firefox prioritizes user privacy, security, and provides extensive customization options for browsing according to organizational preferences, making it ideal for enterprise use. This guide provides scripts to ensure macOS devices are equipped with the latest Firefox version through automated installation and management.

Key Benefits

  • Privacy-focused: Enhanced user privacy and data protection
  • Security: Regular security updates and enterprise-grade protection
  • Customization: Extensive configuration options for enterprise policies
  • Cross-platform: Consistent experience across different operating systems
  • Open source: Transparent development and security auditing

Basic Firefox Installation

Simple Firefox Installation Script

Create a basic script to install the latest Firefox version:

#!/bin/bash

# Basic Firefox installation script
# Usage: ./install_firefox.sh

install_firefox() {
    echo "Starting Firefox installation process..."
    
    # Check if Firefox is already installed
    if [ -d "/Applications/Firefox.app" ]; then
        echo "Firefox is already installed."
        exit 0
    fi
    
    # Firefox download URL
    firefox_url="https://download.mozilla.org/?product=firefox-latest&os=osx&lang=en-US"
    download_dir="$HOME/Applications"
    
    # Create download directory
    mkdir -p "$download_dir"
    
    echo "Downloading Firefox..."
    curl -L -o "$download_dir/Firefox.dmg" "$firefox_url"
    
    echo "Mounting the Firefox disk image..."
    hdiutil attach "$download_dir/Firefox.dmg"
    
    echo "Installing Firefox..."
    cp -r "/Volumes/Firefox/Firefox.app" "/Applications/"
    
    echo "Unmounting the Firefox disk image..."
    hdiutil detach "/Volumes/Firefox"
    
    echo "Cleaning up..."
    rm "$download_dir/Firefox.dmg"
    
    echo "Firefox installation complete."
}

# Execute installation
install_firefox

Enhanced Firefox Installation with Validation

#!/bin/bash

# Enhanced Firefox installation with validation and logging
# Usage: ./enhanced_firefox_install.sh

enhanced_firefox_install() {
    local log_file="/var/log/roundfleet_firefox_install.log"
    local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    local download_dir="/tmp/roundfleet_firefox"
    
    # Create log directory
    mkdir -p /var/log
    mkdir -p "$download_dir"
    
    echo "[$timestamp] Enhanced Firefox installation started" >> "$log_file"
    
    # Check if running as root (recommended for enterprise deployment)
    if [ "$EUID" -ne 0 ]; then
        echo "Warning: Running as user. Some features may be limited."
        echo "[$timestamp] Running as user, not root" >> "$log_file"
    fi
    
    # Check existing Firefox installation
    check_existing_firefox
    
    # Download and install Firefox
    download_firefox
    install_firefox_app
    verify_installation
    cleanup_installation
    
    echo "[$timestamp] Enhanced Firefox installation completed" >> "$log_file"
}

check_existing_firefox() {
    echo "Checking for existing Firefox installation..."
    
    if [ -d "/Applications/Firefox.app" ]; then
        local current_version=$(defaults read /Applications/Firefox.app/Contents/Info.plist CFBundleShortVersionString 2>/dev/null || echo "Unknown")
        
        echo "Firefox is already installed (Version: $current_version)"
        echo "[$timestamp] Firefox already installed - Version: $current_version" >> "$log_file"
        
        read -p "Do you want to update to the latest version? (y/N): " update_choice
        if [ "$update_choice" != "y" ] && [ "$update_choice" != "Y" ]; then
            echo "Installation cancelled."
            exit 0
        fi
        
        echo "Proceeding with Firefox update..."
    else
        echo "No existing Firefox installation found."
        echo "[$timestamp] No existing Firefox installation found" >> "$log_file"
    fi
}

download_firefox() {
    echo "Downloading latest Firefox..."
    
    local firefox_url="https://download.mozilla.org/?product=firefox-latest&os=osx&lang=en-US"
    local firefox_dmg="$download_dir/Firefox.dmg"
    
    # Check network connectivity
    if ! ping -c 1 download.mozilla.org >/dev/null 2>&1; then
        echo "Error: No network connectivity to Mozilla servers"
        echo "[$timestamp] Network connectivity check failed" >> "$log_file"
        exit 1
    fi
    
    # Download Firefox with progress and error handling
    if curl -L --progress-bar -o "$firefox_dmg" "$firefox_url"; then
        echo "Firefox download completed successfully"
        echo "[$timestamp] Firefox download completed" >> "$log_file"
    else
        echo "Error: Failed to download Firefox"
        echo "[$timestamp] Firefox download failed" >> "$log_file"
        exit 1
    fi
    
    # Verify download
    if [ -f "$firefox_dmg" ]; then
        local file_size=$(stat -f%z "$firefox_dmg" 2>/dev/null || echo "0")
        echo "Downloaded file size: $file_size bytes"
        echo "[$timestamp] Downloaded file size: $file_size bytes" >> "$log_file"
        
        if [ "$file_size" -lt 50000000 ]; then  # Less than 50MB indicates potential issue
            echo "Warning: Downloaded file seems too small. Possible download issue."
            echo "[$timestamp] Downloaded file size warning" >> "$log_file"
        fi
    else
        echo "Error: Download file not found"
        echo "[$timestamp] Download file not found" >> "$log_file"
        exit 1
    fi
}

install_firefox_app() {
    echo "Installing Firefox application..."
    
    local firefox_dmg="$download_dir/Firefox.dmg"
    local mount_point="/tmp/firefox_mount"
    
    # Create mount point
    mkdir -p "$mount_point"
    
    # Mount the DMG file
    if hdiutil attach "$firefox_dmg" -mountpoint "$mount_point" -quiet; then
        echo "Firefox disk image mounted successfully"
        echo "[$timestamp] Firefox disk image mounted" >> "$log_file"
    else
        echo "Error: Failed to mount Firefox disk image"
        echo "[$timestamp] Failed to mount Firefox disk image" >> "$log_file"
        exit 1
    fi
    
    # Copy Firefox application
    if [ -d "$mount_point/Firefox.app" ]; then
        echo "Copying Firefox application to /Applications..."
        
        # Remove existing installation if present
        if [ -d "/Applications/Firefox.app" ]; then
            rm -rf "/Applications/Firefox.app"
        fi
        
        # Copy new Firefox application
        if cp -R "$mount_point/Firefox.app" "/Applications/"; then
            echo "Firefox application copied successfully"
            echo "[$timestamp] Firefox application copied successfully" >> "$log_file"
        else
            echo "Error: Failed to copy Firefox application"
            echo "[$timestamp] Failed to copy Firefox application" >> "$log_file"
            hdiutil detach "$mount_point" -quiet
            exit 1
        fi
    else
        echo "Error: Firefox.app not found in disk image"
        echo "[$timestamp] Firefox.app not found in disk image" >> "$log_file"
        hdiutil detach "$mount_point" -quiet
        exit 1
    fi
    
    # Unmount the disk image
    if hdiutil detach "$mount_point" -quiet; then
        echo "Firefox disk image unmounted successfully"
        echo "[$timestamp] Firefox disk image unmounted" >> "$log_file"
    else
        echo "Warning: Failed to unmount Firefox disk image"
        echo "[$timestamp] Failed to unmount Firefox disk image" >> "$log_file"
    fi
}

verify_installation() {
    echo "Verifying Firefox installation..."
    
    if [ -d "/Applications/Firefox.app" ]; then
        local installed_version=$(defaults read /Applications/Firefox.app/Contents/Info.plist CFBundleShortVersionString 2>/dev/null || echo "Unknown")
        local bundle_id=$(defaults read /Applications/Firefox.app/Contents/Info.plist CFBundleIdentifier 2>/dev/null || echo "Unknown")
        
        echo "Firefox installation verified:"
        echo "  Version: $installed_version"
        echo "  Bundle ID: $bundle_id"
        echo "  Location: /Applications/Firefox.app"
        
        echo "[$timestamp] Firefox installation verified - Version: $installed_version" >> "$log_file"
        
        # Test Firefox launch (optional)
        echo "Testing Firefox launch..."
        if timeout 10 /Applications/Firefox.app/Contents/MacOS/firefox --version >/dev/null 2>&1; then
            echo "Firefox launch test successful"
            echo "[$timestamp] Firefox launch test successful" >> "$log_file"
        else
            echo "Warning: Firefox launch test failed or timed out"
            echo "[$timestamp] Firefox launch test failed" >> "$log_file"
        fi
    else
        echo "Error: Firefox installation verification failed"
        echo "[$timestamp] Firefox installation verification failed" >> "$log_file"
        exit 1
    fi
}

cleanup_installation() {
    echo "Cleaning up installation files..."
    
    if [ -d "$download_dir" ]; then
        rm -rf "$download_dir"
        echo "Installation files cleaned up"
        echo "[$timestamp] Installation files cleaned up" >> "$log_file"
    fi
}

# Execute enhanced installation
enhanced_firefox_install

Advanced Firefox Management

Enterprise Firefox Deployment Manager

#!/bin/bash

# Enterprise Firefox deployment manager
# Usage: ./enterprise_firefox_manager.sh

enterprise_firefox_manager() {
    local config_file="/etc/roundfleet/firefox_config.conf"
    local log_file="/var/log/roundfleet_firefox_enterprise.log"
    local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    
    # Check if running as root
    if [ "$EUID" -ne 0 ]; then
        echo "Error: This script must be run as root for enterprise deployment"
        exit 1
    fi
    
    # Create configuration directory
    mkdir -p /etc/roundfleet
    
    # Load or create configuration
    if [ -f "$config_file" ]; then
        source "$config_file"
    else
        create_enterprise_config
        source "$config_file"
    fi
    
    echo "[$timestamp] Enterprise Firefox manager started" >> "$log_file"
    
    # Display enterprise menu
    display_enterprise_menu
}

create_enterprise_config() {
    cat > "/etc/roundfleet/firefox_config.conf" << 'EOF'
# Roundfleet Enterprise Firefox Configuration

# Organization settings
ORGANIZATION_NAME="Roundfleet Organization"
IT_CONTACT="[email protected]"

# Firefox installation settings
FIREFOX_LANGUAGE="en-US"
FIREFOX_CHANNEL="release"  # release, beta, nightly
AUTO_UPDATE_ENABLED="true"
SILENT_INSTALL="true"

# Enterprise policies
ENABLE_ENTERPRISE_POLICIES="true"
DISABLE_TELEMETRY="true"
DISABLE_STUDIES="true"
BLOCK_ABOUT_CONFIG="false"
HOMEPAGE_URL="https://www.roundfleet.com"

# Security settings
ENABLE_SECURITY_POLICIES="true"
BLOCK_DANGEROUS_DOWNLOADS="true"
ENABLE_SAFE_BROWSING="true"
DISABLE_DEVELOPER_TOOLS="false"

# Network settings
PROXY_ENABLED="false"
PROXY_TYPE="system"
PROXY_HOST=""
PROXY_PORT=""

# Logging and monitoring
ENABLE_DETAILED_LOGGING="true"
LOG_RETENTION_DAYS="30"
SEND_DEPLOYMENT_REPORTS="true"
EOF
    
    echo "configuration created at /etc/roundfleet/firefox_config.conf"
}

display_enterprise_menu() {
    while true; do
        clear
        echo "======================================="
        echo "Roundfleet Enterprise Firefox Manager"
        echo "======================================="
        echo ""
        echo "Organization: $ORGANIZATION_NAME"
        echo "Contact: $IT_CONTACT"
        echo ""
        echo "Available Actions:"
        echo "1. Install/Update Firefox"
        echo "2. Configure Enterprise Policies"
        echo "3. Deploy to Multiple Devices"
        echo "4. Check Firefox Status"
        echo "5. Generate Deployment Report"
        echo "6. Manage Firefox Profiles"
        echo "7. View Configuration"
        echo "8. Exit"
        echo ""
        read -p "Select an option (1-8): " choice
        
        case $choice in
            1)
                install_update_firefox
                ;;
            2)
                configure_enterprise_policies
                ;;
            3)
                deploy_multiple_devices
                ;;
            4)
                check_firefox_status
                ;;
            5)
                generate_deployment_report
                ;;
            6)
                manage_firefox_profiles
                ;;
            7)
                view_configuration
                ;;
            8)
                echo "Exiting Enterprise Firefox Manager."
                exit 0
                ;;
            *)
                echo "Invalid option. Please try again."
                ;;
        esac
        
        echo ""
        read -p "Press Enter to continue..."
    done
}

install_update_firefox() {
    echo "======================================="
    echo "Install/Update Firefox"
    echo "======================================="
    
    local current_version=""
    if [ -d "/Applications/Firefox.app" ]; then
        current_version=$(defaults read /Applications/Firefox.app/Contents/Info.plist CFBundleShortVersionString 2>/dev/null || echo "Unknown")
        echo "Current Firefox version: $current_version"
    else
        echo "Firefox not currently installed"
    fi
    
    echo ""
    echo "Installation options:"
    echo "1. Install/Update to latest stable version"
    echo "2. Install/Update to latest beta version"
    echo "3. Install/Update to latest nightly version"
    echo "4. Cancel"
    echo ""
    read -p "Select installation option (1-4): " install_choice
    
    case $install_choice in
        1)
            install_firefox_version "release"
            ;;
        2)
            install_firefox_version "beta"
            ;;
        3)
            install_firefox_version "nightly"
            ;;
        4)
            echo "Installation cancelled."
            ;;
        *)
            echo "Invalid option."
            ;;
    esac
}

install_firefox_version() {
    local channel="$1"
    local download_dir="/tmp/roundfleet_firefox_enterprise"
    
    echo "Installing Firefox $channel version..."
    
    # Create download directory
    mkdir -p "$download_dir"
    
    # Determine download URL based on channel
    local firefox_url
    case $channel in
        "release")
            firefox_url="https://download.mozilla.org/?product=firefox-latest&os=osx&lang=$FIREFOX_LANGUAGE"
            ;;
        "beta")
            firefox_url="https://download.mozilla.org/?product=firefox-beta-latest&os=osx&lang=$FIREFOX_LANGUAGE"
            ;;
        "nightly")
            firefox_url="https://download.mozilla.org/?product=firefox-nightly-latest&os=osx&lang=$FIREFOX_LANGUAGE"
            ;;
        *)
            echo "Error: Invalid Firefox channel"
            return 1
            ;;
    esac
    
    echo "Downloading Firefox $channel..."
    local firefox_dmg="$download_dir/Firefox-$channel.dmg"
    
    if curl -L --progress-bar -o "$firefox_dmg" "$firefox_url"; then
        echo "Download completed successfully"
        echo "[$timestamp] Firefox $channel download completed" >> "$log_file"
    else
        echo "Error: Download failed"
        echo "[$timestamp] Firefox $channel download failed" >> "$log_file"
        return 1
    fi
    
    # Install Firefox
    echo "Installing Firefox..."
    local mount_point="/tmp/firefox_${channel}_mount"
    mkdir -p "$mount_point"
    
    if hdiutil attach "$firefox_dmg" -mountpoint "$mount_point" -quiet; then
        # Remove existing installation
        if [ -d "/Applications/Firefox.app" ]; then
            rm -rf "/Applications/Firefox.app"
        fi
        
        # Copy new installation
        if cp -R "$mount_point/Firefox.app" "/Applications/"; then
            echo "Firefox $channel installed successfully"
            echo "[$timestamp] Firefox $channel installed successfully" >> "$log_file"
            
            # Apply enterprise policies if enabled
            if [ "$ENABLE_ENTERPRISE_POLICIES" = "true" ]; then
                apply_enterprise_policies
            fi
        else
            echo "Error: Installation failed"
            echo "[$timestamp] Firefox $channel installation failed" >> "$log_file"
        fi
        
        hdiutil detach "$mount_point" -quiet
    else
        echo "Error: Failed to mount disk image"
        echo "[$timestamp] Failed to mount Firefox $channel disk image" >> "$log_file"
    fi
    
    # Cleanup
    rm -rf "$download_dir"
}

configure_enterprise_policies() {
    echo "======================================="
    echo "Configure Enterprise Policies"
    echo "======================================="
    
    local policies_dir="/Applications/Firefox.app/Contents/Resources/distribution"
    local policies_file="$policies_dir/policies.json"
    
    echo "Creating enterprise policies configuration..."
    
    # Create distribution directory
    mkdir -p "$policies_dir"
    
    # Generate policies.json
    cat > "$policies_file" << EOF
{
  "policies": {
    "DisableTelemetry": $DISABLE_TELEMETRY,
    "DisableFirefoxStudies": $DISABLE_STUDIES,
    "BlockAboutConfig": $BLOCK_ABOUT_CONFIG,
    "Homepage": {
      "URL": "$HOMEPAGE_URL",
      "Locked": true
    },
    "DisableSystemAddonUpdate": true,
    "DisableAppUpdate": $([ "$AUTO_UPDATE_ENABLED" = "true" ] && echo "false" || echo "true"),
    "NoDefaultBookmarks": true,
    "OfferToSaveLogins": false,
    "PasswordManagerEnabled": false,
    "DisableDeveloperTools": $DISABLE_DEVELOPER_TOOLS,
    "DisablePrivateBrowsing": false,
    "DisableProfileImport": true,
    "DisableFeedbackCommands": true,
    "DisableFirefoxAccounts": false,
    "DisableForgetButton": true,
    "DisablePocket": true,
    "DisableSetDesktopBackground": true,
    "DisplayBookmarksToolbar": false,
    "DontCheckDefaultBrowser": true,
    "EnableTrackingProtection": {
      "Value": true,
      "Locked": true
    }
  }
}
EOF
    
    echo "policies configuration created"
    echo "[$timestamp] Enterprise policies configured" >> "$log_file"
    
    # Set appropriate permissions
    chmod 644 "$policies_file"
    
    echo "policies applied to Firefox installation"
}

apply_enterprise_policies() {
    echo "Applying enterprise policies..."
    configure_enterprise_policies
}

check_firefox_status() {
    echo "======================================="
    echo "Firefox Status Report"
    echo "======================================="
    
    local device_name=$(scutil --get ComputerName)
    local os_version=$(sw_vers -productVersion)
    
    echo "Device: $device_name"
    echo "macOS Version: $os_version"
    echo "Timestamp: $(date)"
    echo ""
    
    if [ -d "/Applications/Firefox.app" ]; then
        local firefox_version=$(defaults read /Applications/Firefox.app/Contents/Info.plist CFBundleShortVersionString 2>/dev/null || echo "Unknown")
        local bundle_id=$(defaults read /Applications/Firefox.app/Contents/Info.plist CFBundleIdentifier 2>/dev/null || echo "Unknown")
        
        echo "Firefox Status: INSTALLED"
        echo "Version: $firefox_version"
        echo "Bundle ID: $bundle_id"
        echo "Installation Path: /Applications/Firefox.app"
        
        # Check if Firefox is running
        if pgrep -x "firefox" >/dev/null; then
            echo "Process Status: RUNNING"
        else
            echo "Process Status: NOT RUNNING"
        fi
        
        # Check enterprise policies
        local policies_file="/Applications/Firefox.app/Contents/Resources/distribution/policies.json"
        if [ -f "$policies_file" ]; then
            echo "Policies: CONFIGURED"
        else
            echo "Policies: NOT CONFIGURED"
        fi
        
        # Check application permissions
        echo "Application Permissions:"
        ls -la /Applications/Firefox.app | head -1
        
    else
        echo "Firefox Status: NOT INSTALLED"
    fi
}

generate_deployment_report() {
    echo "======================================="
    echo "Generate Deployment Report"
    echo "======================================="
    
    local report_file="/tmp/firefox_deployment_report_$(date +%Y%m%d_%H%M%S).txt"
    
    cat > "$report_file" << EOF
Roundfleet Firefox Deployment Report
Generated: $(date)
Organization: $ORGANIZATION_NAME

Device Information:
  Computer Name: $(scutil --get ComputerName)
  macOS Version: $(sw_vers -productVersion)
  Hardware Model: $(system_profiler SPHardwareDataType | grep "Model Name" | cut -d: -f2 | xargs)

Firefox Installation:
EOF
    
    if [ -d "/Applications/Firefox.app" ]; then
        local firefox_version=$(defaults read /Applications/Firefox.app/Contents/Info.plist CFBundleShortVersionString 2>/dev/null || echo "Unknown")
        
        echo "  Status: Installed" >> "$report_file"
        echo "  Version: $firefox_version" >> "$report_file"
        echo "  Installation Date: $(stat -f "%Sm" /Applications/Firefox.app)" >> "$report_file"
        
        # Check enterprise policies
        local policies_file="/Applications/Firefox.app/Contents/Resources/distribution/policies.json"
        if [ -f "$policies_file" ]; then
            echo "  Enterprise Policies: Configured" >> "$report_file"
        else
            echo "  Enterprise Policies: Not Configured" >> "$report_file"
        fi
    else
        echo "  Status: Not Installed" >> "$report_file"
    fi
    
    cat >> "$report_file" << EOF

Configuration:
  Language: $FIREFOX_LANGUAGE
  Channel: $FIREFOX_CHANNEL
  Auto Update: $AUTO_UPDATE_ENABLED
  Enterprise Policies: $ENABLE_ENTERPRISE_POLICIES

Contact: $IT_CONTACT
EOF
    
    echo "Deployment report generated: $report_file"
}

manage_firefox_profiles() {
    echo "======================================="
    echo "Manage Firefox Profiles"
    echo "======================================="
    
    local profiles_dir="$HOME/Library/Application Support/Firefox/Profiles"
    
    echo "Firefox profiles directory: $profiles_dir"
    
    if [ -d "$profiles_dir" ]; then
        echo "Found Firefox profiles:"
        ls -la "$profiles_dir"
        
        echo ""
        echo "Profile management options:"
        echo "1. List all profiles"
        echo "2. Create new profile"
        echo "3. Remove profile"
        echo "4. Export profile"
        echo "5. Import profile"
        echo "6. Back to main menu"
        
        read -p "Select option (1-6): " profile_choice
        
        case $profile_choice in
            1)
                list_firefox_profiles
                ;;
            2)
                create_firefox_profile
                ;;
            3)
                remove_firefox_profile
                ;;
            4)
                export_firefox_profile
                ;;
            5)
                import_firefox_profile
                ;;
            6)
                return
                ;;
            *)
                echo "Invalid option."
                ;;
        esac
    else
        echo "No Firefox profiles directory found."
        echo "Firefox may not have been launched yet."
    fi
}

list_firefox_profiles() {
    echo "Firefox Profiles:"
    /Applications/Firefox.app/Contents/MacOS/firefox -ProfileManager -no-remote &
    sleep 2
    pkill -f "firefox.*ProfileManager"
}

create_firefox_profile() {
    read -p "Enter profile name: " profile_name
    if [ -n "$profile_name" ]; then
        /Applications/Firefox.app/Contents/MacOS/firefox -CreateProfile "$profile_name"
        echo "Profile '$profile_name' created successfully"
    else
        echo "Profile name cannot be empty"
    fi
}

remove_firefox_profile() {
    echo "Warning: This will permanently delete the profile and all its data."
    read -p "Enter profile name to remove: " profile_name
    if [ -n "$profile_name" ]; then
        read -p "Are you sure you want to remove profile '$profile_name'? (y/N): " confirm
        if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
            # Profile removal logic would go here
            echo "Profile removal functionality would be implemented here"
        else
            echo "Profile removal cancelled"
        fi
    fi
}

export_firefox_profile() {
    echo "Profile export functionality would be implemented here"
}

import_firefox_profile() {
    echo "Profile import functionality would be implemented here"
}

view_configuration() {
    echo "======================================="
    echo "Current Configuration"
    echo "======================================="
    
    if [ -f "/etc/roundfleet/firefox_config.conf" ]; then
        cat "/etc/roundfleet/firefox_config.conf"
    else
        echo "No configuration file found."
    fi
}

# Execute enterprise manager
enterprise_firefox_manager

Mass Deployment and Automation

Automated Firefox Update System

#!/bin/bash

# Automated Firefox update system
# Usage: ./automated_firefox_updater.sh

automated_firefox_updater() {
    local config_file="/etc/roundfleet/firefox_auto_update.conf"
    local log_file="/var/log/roundfleet_firefox_updates.log"
    local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    
    # Check if running as root
    if [ "$EUID" -ne 0 ]; then
        echo "Error: This script must be run as root"
        exit 1
    fi
    
    # Create configuration if it doesn't exist
    if [ ! -f "$config_file" ]; then
        create_update_config
    fi
    
    source "$config_file"
    
    echo "[$timestamp] Automated Firefox updater started" >> "$log_file"
    
    # Check if updates are enabled
    if [ "$ENABLE_AUTO_UPDATES" = "true" ]; then
        check_and_update_firefox
    else
        echo "Automatic updates are disabled"
        echo "[$timestamp] Automatic updates are disabled" >> "$log_file"
    fi
}

create_update_config() {
    mkdir -p /etc/roundfleet
    
    cat > "/etc/roundfleet/firefox_auto_update.conf" << 'EOF'
# Roundfleet Firefox Auto-Update Configuration

# Update settings
ENABLE_AUTO_UPDATES="true"
UPDATE_CHANNEL="release"
CHECK_INTERVAL="daily"
UPDATE_TIME="02:00"

# Safety settings
BACKUP_PROFILES="true"
BACKUP_RETENTION_DAYS="7"
REQUIRE_USER_CONSENT="false"
SKIP_IF_RUNNING="true"

# Notification settings
SEND_NOTIFICATIONS="true"
NOTIFICATION_EMAIL="[email protected]"
NOTIFY_ON_SUCCESS="true"
NOTIFY_ON_FAILURE="true"

# Enterprise settings
MAINTAIN_POLICIES="true"
PRESERVE_CUSTOMIZATIONS="true"
VERIFY_INSTALLATION="true"
EOF
    
    echo "Auto-update configuration created at /etc/roundfleet/firefox_auto_update.conf"
}

check_and_update_firefox() {
    echo "[$timestamp] Checking for Firefox updates" >> "$log_file"
    
    # Check if Firefox is installed
    if [ ! -d "/Applications/Firefox.app" ]; then
        echo "Firefox is not installed. Installing latest version..."
        install_firefox_latest
        return
    fi
    
    # Get current version
    local current_version=$(defaults read /Applications/Firefox.app/Contents/Info.plist CFBundleShortVersionString 2>/dev/null || echo "Unknown")
    
    # Check if Firefox is running and skip if configured
    if [ "$SKIP_IF_RUNNING" = "true" ] && pgrep -x "firefox" >/dev/null; then
        echo "Firefox is currently running. Skipping update."
        echo "[$timestamp] Firefox is running - update skipped" >> "$log_file"
        return
    fi
    
    # Create backup if enabled
    if [ "$BACKUP_PROFILES" = "true" ]; then
        backup_firefox_profiles
    fi
    
    # Download and check latest version
    local download_dir="/tmp/roundfleet_firefox_update"
    mkdir -p "$download_dir"
    
    local firefox_url="https://download.mozilla.org/?product=firefox-latest&os=osx&lang=en-US"
    local firefox_dmg="$download_dir/Firefox-latest.dmg"
    
    echo "Downloading latest Firefox version for comparison..."
    if curl -L -s -o "$firefox_dmg" "$firefox_url"; then
        # Mount and check version
        local mount_point="/tmp/firefox_update_mount"
        mkdir -p "$mount_point"
        
        if hdiutil attach "$firefox_dmg" -mountpoint "$mount_point" -quiet; then
            local latest_version=$(defaults read "$mount_point/Firefox.app/Contents/Info.plist" CFBundleShortVersionString 2>/dev/null || echo "Unknown")
            
            echo "Current version: $current_version"
            echo "Latest version: $latest_version"
            
            if [ "$current_version" != "$latest_version" ]; then
                echo "Update available. Updating Firefox..."
                echo "[$timestamp] Update available - Current: $current_version, Latest: $latest_version" >> "$log_file"
                
                # Perform update
                update_firefox_installation "$mount_point"
                
                # Verify update
                if verify_firefox_update "$latest_version"; then
                    echo "Firefox updated successfully to version $latest_version"
                    echo "[$timestamp] Firefox updated successfully to version $latest_version" >> "$log_file"
                    
                    if [ "$SEND_NOTIFICATIONS" = "true" ] && [ "$NOTIFY_ON_SUCCESS" = "true" ]; then
                        send_update_notification "success" "$current_version" "$latest_version"
                    fi
                else
                    echo "Firefox update verification failed"
                    echo "[$timestamp] Firefox update verification failed" >> "$log_file"
                    
                    if [ "$SEND_NOTIFICATIONS" = "true" ] && [ "$NOTIFY_ON_FAILURE" = "true" ]; then
                        send_update_notification "failure" "$current_version" "$latest_version"
                    fi
                fi
            else
                echo "Firefox is already up to date (version $current_version)"
                echo "[$timestamp] Firefox is already up to date (version $current_version)" >> "$log_file"
            fi
            
            hdiutil detach "$mount_point" -quiet
        else
            echo "Failed to mount Firefox disk image"
            echo "[$timestamp] Failed to mount Firefox disk image for update check" >> "$log_file"
        fi
    else
        echo "Failed to download Firefox for version check"
        echo "[$timestamp] Failed to download Firefox for version check" >> "$log_file"
    fi
    
    # Cleanup
    rm -rf "$download_dir"
}

install_firefox_latest() {
    echo "Installing latest Firefox version..."
    
    local download_dir="/tmp/roundfleet_firefox_install"
    mkdir -p "$download_dir"
    
    local firefox_url="https://download.mozilla.org/?product=firefox-latest&os=osx&lang=en-US"
    local firefox_dmg="$download_dir/Firefox.dmg"
    
    if curl -L --progress-bar -o "$firefox_dmg" "$firefox_url"; then
        local mount_point="/tmp/firefox_install_mount"
        mkdir -p "$mount_point"
        
        if hdiutil attach "$firefox_dmg" -mountpoint "$mount_point" -quiet; then
            if cp -R "$mount_point/Firefox.app" "/Applications/"; then
                local installed_version=$(defaults read /Applications/Firefox.app/Contents/Info.plist CFBundleShortVersionString 2>/dev/null || echo "Unknown")
                echo "Firefox installed successfully (version $installed_version)"
                echo "[$timestamp] Firefox installed successfully (version $installed_version)" >> "$log_file"
                
                # Apply enterprise policies if needed
                if [ "$MAINTAIN_POLICIES" = "true" ]; then
                    apply_enterprise_policies
                fi
            else
                echo "Failed to install Firefox"
                echo "[$timestamp] Failed to install Firefox" >> "$log_file"
            fi
            
            hdiutil detach "$mount_point" -quiet
        else
            echo "Failed to mount Firefox disk image"
            echo "[$timestamp] Failed to mount Firefox disk image for installation" >> "$log_file"
        fi
    else
        echo "Failed to download Firefox"
        echo "[$timestamp] Failed to download Firefox for installation" >> "$log_file"
    fi
    
    rm -rf "$download_dir"
}

update_firefox_installation() {
    local mount_point="$1"
    
    echo "Updating Firefox installation..."
    
    # Remove old installation
    if [ -d "/Applications/Firefox.app" ]; then
        rm -rf "/Applications/Firefox.app"
    fi
    
    # Install new version
    if cp -R "$mount_point/Firefox.app" "/Applications/"; then
        echo "Firefox application updated successfully"
        
        # Restore enterprise policies if enabled
        if [ "$MAINTAIN_POLICIES" = "true" ]; then
            apply_enterprise_policies
        fi
        
        # Preserve customizations if enabled
        if [ "$PRESERVE_CUSTOMIZATIONS" = "true" ]; then
            restore_customizations
        fi
        
        return 0
    else
        echo "Failed to update Firefox application"
        return 1
    fi
}

verify_firefox_update() {
    local expected_version="$1"
    
    if [ -d "/Applications/Firefox.app" ]; then
        local actual_version=$(defaults read /Applications/Firefox.app/Contents/Info.plist CFBundleShortVersionString 2>/dev/null || echo "Unknown")
        
        if [ "$actual_version" = "$expected_version" ]; then
            return 0
        else
            echo "Version mismatch - Expected: $expected_version, Actual: $actual_version"
            return 1
        fi
    else
        echo "Firefox application not found after update"
        return 1
    fi
}

backup_firefox_profiles() {
    echo "Creating backup of Firefox profiles..."
    
    local profiles_dir="$HOME/Library/Application Support/Firefox"
    local backup_dir="/var/backups/roundfleet/firefox_profiles"
    local backup_timestamp=$(date +%Y%m%d_%H%M%S)
    
    mkdir -p "$backup_dir"
    
    if [ -d "$profiles_dir" ]; then
        if tar -czf "$backup_dir/firefox_profiles_backup_$backup_timestamp.tar.gz" -C "$HOME/Library/Application Support" "Firefox"; then
            echo "Firefox profiles backed up successfully"
            echo "[$timestamp] Firefox profiles backed up" >> "$log_file"
            
            # Clean up old backups
            find "$backup_dir" -name "firefox_profiles_backup_*.tar.gz" -mtime +$BACKUP_RETENTION_DAYS -delete
        else
            echo "Failed to backup Firefox profiles"
            echo "[$timestamp] Failed to backup Firefox profiles" >> "$log_file"
        fi
    else
        echo "Firefox profiles directory not found"
    fi
}

send_update_notification() {
    local status="$1"
    local old_version="$2"
    local new_version="$3"
    
    local notification_title="Roundfleet Firefox Update"
    local notification_message
    
    case $status in
        "success")
            notification_message="Firefox updated successfully from $old_version to $new_version"
            ;;
        "failure")
            notification_message="Firefox update failed - attempted to update from $old_version to $new_version"
            ;;
        *)
            notification_message="Firefox update status: $status"
            ;;
    esac
    
    # Use osascript to display notification
    osascript -e "display notification \"$notification_message\" with title \"$notification_title\""
    
    echo "[$timestamp] Update notification sent - Status: $status" >> "$log_file"
}

apply_enterprise_policies() {
    echo "Applying enterprise policies..."
    # This would call the enterprise policy configuration function
    # Implementation depends on the specific policy requirements
}

restore_customizations() {
    echo "Restoring customizations..."
    # This would restore any custom configurations
    # Implementation depends on the specific customization requirements
}

# Execute automated updater
automated_firefox_updater

Troubleshooting and Best Practices

Common Issues and Solutions

  1. Network Connectivity: Ensure stable internet connection for downloads
  2. Permissions: Run installation scripts with appropriate privileges
  3. Disk Space: Verify sufficient disk space before installation
  4. Existing Processes: Check for running Firefox processes before updates
  5. Enterprise Policies: Validate policy configurations after deployment

Best Practices

  • Test Deployments: Always test in controlled environments first
  • Backup Profiles: Create backups before major updates
  • Monitor Installations: Use logging and monitoring for enterprise deployments
  • Version Control: Track Firefox versions across your fleet
  • Security Updates: Prioritize security updates and patches

Conclusion

Firefox deployment and management on macOS requires careful planning and execution. These scripts provide comprehensive solutions for installing, updating, and managing Firefox across Mac fleets. From basic installations to enterprise-grade automation, these tools ensure consistent and secure Firefox deployments while maintaining organizational policies and user preferences.

Hide and Unhide Files and Folders on macOS

Learn how to control the visibility of files and folders on Mac devices. Essential for protecting sensitive data, organizing workspaces, and managing system file access in enterprise environments.

Hide Specific Files or Folders

Hide individual files or folders from Finder view:

#!/bin/bash

# Configuration
TARGET_PATH="/Users/$(stat -f "%Su" /dev/console)/Desktop/sensitive-file.txt"

echo "Hiding file/folder: $TARGET_PATH"

# Hide the specified file or folder
if [[ -e "$TARGET_PATH" ]]; then
    chflags hidden "$TARGET_PATH"
    echo "✅ Successfully hidden: $TARGET_PATH"
else
    echo "❌ File/folder not found: $TARGET_PATH"
    exit 1
fi

Unhide Specific Files or Folders

Reveal previously hidden files or folders:

#!/bin/bash

# Configuration
TARGET_PATH="/Users/$(stat -f "%Su" /dev/console)/Desktop/sensitive-file.txt"

echo "Unhiding file/folder: $TARGET_PATH"

# Unhide the specified file or folder
if [[ -e "$TARGET_PATH" ]]; then
    chflags nohidden "$TARGET_PATH"
    echo "✅ Successfully unhidden: $TARGET_PATH"
else
    echo "❌ File/folder not found: $TARGET_PATH"
    exit 1
fi

Reveal All Hidden Files

Show all hidden files and folders in Finder (including system files):

#!/bin/bash

# Get current user context
CURRENT_USER=$(stat -f "%Su" /dev/console)
CURRENT_USER_UID=$(id -u "$CURRENT_USER")

echo "Revealing all hidden files for user: $CURRENT_USER"

# Enable showing all hidden files in Finder
launchctl asuser $CURRENT_USER_UID sudo -iu "$CURRENT_USER" \
    defaults write com.apple.finder AppleShowAllFiles -boolean true

# Restart Finder to apply changes
launchctl asuser $CURRENT_USER_UID sudo -iu "$CURRENT_USER" \
    killall Finder

echo "✅ All hidden files are now visible"
echo "⚠️  Hidden files appear faded in Finder"

Hide All Revealed Files

Hide all previously revealed hidden files and folders:

#!/bin/bash

# Get current user context
CURRENT_USER=$(stat -f "%Su" /dev/console)
CURRENT_USER_UID=$(id -u "$CURRENT_USER")

echo "Hiding all revealed hidden files for user: $CURRENT_USER"

# Disable showing hidden files in Finder
launchctl asuser $CURRENT_USER_UID sudo -iu "$CURRENT_USER" \
    defaults write com.apple.finder AppleShowAllFiles -boolean false

# Restart Finder to apply changes
launchctl asuser $CURRENT_USER_UID sudo -iu "$CURRENT_USER" \
    killall Finder

echo "✅ Hidden files are now concealed"
echo "🔒 System files are protected from view"

Bulk File Hide/Unhide Management

Script to manage multiple files and folders:

#!/bin/bash

# Function to hide multiple items
hide_multiple_items() {
    local items=("$@")
    local hidden_count=0
    
    echo "🔒 Hiding multiple files and folders..."
    
    for item in "${items[@]}"; do
        if [[ -e "$item" ]]; then
            chflags hidden "$item"
            echo "  ✅ Hidden: $item"
            ((hidden_count++))
        else
            echo "  ❌ Not found: $item"
        fi
    done
    
    echo "📊 Successfully hidden $hidden_count items"
}

# Function to unhide multiple items
unhide_multiple_items() {
    local items=("$@")
    local unhidden_count=0
    
    echo "👁️  Unhiding multiple files and folders..."
    
    for item in "${items[@]}"; do
        if [[ -e "$item" ]]; then
            chflags nohidden "$item"
            echo "  ✅ Unhidden: $item"
            ((unhidden_count++))
        else
            echo "  ❌ Not found: $item"
        fi
    done
    
    echo "📊 Successfully unhidden $unhidden_count items"
}

# Configuration - Add your file/folder paths here
CURRENT_USER=$(stat -f "%Su" /dev/console)
ITEMS_TO_MANAGE=(
    "/Users/$CURRENT_USER/Desktop/confidential"
    "/Users/$CURRENT_USER/Documents/private-notes.txt"
    "/Users/$CURRENT_USER/Desktop/temp-folder"
)

# Choose operation: hide or unhide
OPERATION="hide"  # Change to "unhide" to reveal items

case "$OPERATION" in
    "hide")
        hide_multiple_items "${ITEMS_TO_MANAGE[@]}"
        ;;
    "unhide")
        unhide_multiple_items "${ITEMS_TO_MANAGE[@]}"
        ;;
    *)
        echo "❌ Invalid operation. Use 'hide' or 'unhide'"
        exit 1
        ;;
esac

Enterprise File Visibility Management

Comprehensive script for enterprise file management:

#!/bin/bash

# Enterprise file visibility configuration
COMPANY_NAME="Roundfleet"
POLICY_TYPE="security"  # Options: security, organization, compliance

# Define policy-specific file patterns
case "$POLICY_TYPE" in
    "security")
        HIDE_PATTERNS=(
            "*/confidential*"
            "*/private*"
            "*/.ssh*"
            "*/credentials*"
        )
        ;;
    "organization")
        HIDE_PATTERNS=(
            "*/temp*"
            "*/cache*"
            "*/.DS_Store"
            "*/thumbs.db"
        )
        ;;
    "compliance")
        HIDE_PATTERNS=(
            "*/audit*"
            "*/logs*"
            "*/backup*"
            "*/archive*"
        )
        ;;
esac

# Function to apply enterprise visibility policy
apply_enterprise_visibility_policy() {
    local current_user=$(stat -f "%Su" /dev/console)
    local current_user_uid=$(id -u "$current_user")
    
    echo "🏢 Applying Enterprise File Visibility Policy"
    echo "============================================="
    echo "Policy: $POLICY_TYPE"
    echo "Device: $(hostname)"
    echo "User: $current_user"
    echo "Timestamp: $(date)"
    
    local hidden_count=0
    
    # Process each pattern
    for pattern in "${HIDE_PATTERNS[@]}"; do
        echo "🔍 Processing pattern: $pattern"
        
        # Find files matching pattern in user directories
        while IFS= read -r -d '' file; do
            if [[ -e "$file" ]]; then
                chflags hidden "$file"
                echo "  🔒 Hidden: $file"
                ((hidden_count++))
            fi
        done < <(find "/Users/$current_user" -name "${pattern#*/}" -print0 2>/dev/null)
    done
    
    echo "✅ Enterprise visibility policy applied"
    echo "📊 Hidden $hidden_count items on $(hostname)"
}

# Execute enterprise policy
apply_enterprise_visibility_policy

Check File Visibility Status

Script to check if files or folders are hidden:

#!/bin/bash

# Function to check visibility status
check_visibility_status() {
    local target_path="$1"
    
    if [[ ! -e "$target_path" ]]; then
        echo "❌ File/folder does not exist: $target_path"
        return 1
    fi
    
    # Check hidden flag
    local flags=$(ls -lO "$target_path" 2>/dev/null | awk '{print $5}')
    
    if [[ "$flags" == *"hidden"* ]]; then
        echo "🔒 HIDDEN: $target_path"
    else
        echo "👁️  VISIBLE: $target_path"
    fi
}

# Function to scan directory for hidden items
scan_directory_visibility() {
    local scan_dir="$1"
    local hidden_count=0
    local visible_count=0
    
    echo "📊 Scanning directory: $scan_dir"
    echo "=================================="
    
    if [[ ! -d "$scan_dir" ]]; then
        echo "❌ Directory does not exist: $scan_dir"
        return 1
    fi
    
    # Scan all items in directory
    while IFS= read -r -d '' item; do
        local flags=$(ls -lO "$item" 2>/dev/null | awk '{print $5}')
        local basename_item=$(basename "$item")
        
        if [[ "$flags" == *"hidden"* ]]; then
            echo "  🔒 $basename_item (hidden)"
            ((hidden_count++))
        else
            echo "  👁️  $basename_item (visible)"
            ((visible_count++))
        fi
    done < <(find "$scan_dir" -maxdepth 1 -print0 2>/dev/null)
    
    echo ""
    echo "Summary:"
    echo "  Hidden items: $hidden_count"
    echo "  Visible items: $visible_count"
    echo "  Total items: $((hidden_count + visible_count))"
}

# Configuration
CURRENT_USER=$(stat -f "%Su" /dev/console)
CHECK_PATH="/Users/$CURRENT_USER/Desktop"

# Check specific file or scan directory
if [[ -f "$CHECK_PATH" ]]; then
    check_visibility_status "$CHECK_PATH"
elif [[ -d "$CHECK_PATH" ]]; then
    scan_directory_visibility "$CHECK_PATH"
else
    echo "❌ Path does not exist: $CHECK_PATH"
fi

Usage with Roundfleet

  1. Configure target files/folders or patterns in script variables
  2. Choose between individual or bulk operations
  3. Deploy through Roundfleet's remote script execution
  4. Verify visibility changes in Finder

Common Use Cases

ScenarioCommandPurpose
Hide sensitive filechflags hidden file.txtData protection
Hide temp folderchflags hidden temp/Workspace cleanup
Show system filesAppleShowAllFiles trueTroubleshooting
Hide system filesAppleShowAllFiles falseUser protection

File Visibility Levels

TypeVisibilityDescription
Normal filesAlways visibleStandard user files
Hidden filesInvisibleFiles hidden by chflags
System filesHidden by defaultmacOS system files
Dot filesHidden in FinderUnix-style hidden files

Important Notes

System files: Cannot unhide system files hidden by macOS default Persistence: Hidden flags persist across reboots and file moves Security: Hidden ≠ secure - files are still accessible via Terminal Finder restart: Required when changing AppleShowAllFiles setting

Troubleshooting

Permission denied: Ensure proper file ownership and permissions Finder not updating: Try killall Finder to force refresh Files still visible: Check if system-level hiding is required

Guest User Management on macOS

Manage guest user accounts and secure temporary access across your Roundfleet devices using advanced guest user management systems. This tutorial covers guest account configuration, security policies, access monitoring, and comprehensive guest lifecycle management.

Understanding macOS Guest User Management

macOS provides guest user functionality for temporary, secure access:

  • defaults - System preferences and configuration management
  • Guest User Account - Temporary access without authentication
  • Secure Sandbox - Isolated environment for guest sessions
  • Automatic Cleanup - Session data removal on logout
  • Access Controls - Restrictions and permissions management

Basic Guest User Operations

Enable Guest User Account

#!/bin/bash

# Basic guest user enablement
defaults write /Library/Preferences/com.apple.loginwindow GuestEnabled -bool TRUE

echo "Guest user account enabled successfully"

Enhanced Guest User Configuration

#!/bin/bash

# Comprehensive guest user configuration with security settings
configure_guest_user() {
    echo "=== Comprehensive Guest User Configuration ==="
    
    # Enable guest user
    echo "Enabling guest user account..."
    sudo defaults write /Library/Preferences/com.apple.loginwindow GuestEnabled -bool TRUE
    
    # Configure guest user restrictions
    echo "Configuring guest user security settings..."
    
    # Disable guest user from making system changes
    sudo defaults write /Library/Preferences/com.apple.loginwindow GuestAllowedToChangeNetwork -bool FALSE
    
    # Set guest user home folder restrictions
    sudo defaults write /Library/Preferences/com.apple.loginwindow GuestHomeIsSeparateDisk -bool TRUE
    
    # Configure automatic logout
    sudo defaults write /Library/Preferences/com.apple.loginwindow GuestAutoLogout -int 3600
    
    # Disable guest user from accessing encrypted volumes
    sudo defaults write /Library/Preferences/com.apple.loginwindow GuestCanAccessEncryptedVolumes -bool FALSE
    
    echo "Guest user configuration completed"
}

# Execute comprehensive configuration
configure_guest_user

Guest User Categories

Guest Access Classifications

#!/bin/bash

# Guest user categories for different organizational needs
declare -A GUEST_USER_CATEGORIES=(
    ["lobby_kiosk"]="Public lobby access for visitors and information browsing"
    ["conference_room"]="Meeting room access for presentations and collaboration"
    ["library_station"]="Educational institution public access terminals"
    ["demo_showcase"]="Product demonstration and sales presentation stations"
    ["visitor_workstation"]="Temporary workstations for business visitors"
    ["event_registration"]="Event check-in and registration terminals"
    ["customer_service"]="Customer service and support terminals"
    ["training_lab"]="Training and workshop temporary access"
    ["public_wifi_portal"]="Public WiFi access and terms acceptance"
    ["emergency_access"]="Emergency access stations for staff use"
)

# Security levels for guest access
declare -A SECURITY_LEVELS=(
    ["lobby_kiosk"]="high_restriction"
    ["conference_room"]="medium_restriction"
    ["library_station"]="medium_restriction"
    ["demo_showcase"]="low_restriction"
    ["visitor_workstation"]="high_restriction"
    ["event_registration"]="medium_restriction"
    ["customer_service"]="high_restriction"
    ["training_lab"]="low_restriction"
    ["public_wifi_portal"]="maximum_restriction"
    ["emergency_access"]="minimal_restriction"
)

# Session duration limits
declare -A SESSION_LIMITS=(
    ["lobby_kiosk"]="1800"      # 30 minutes
    ["conference_room"]="10800"  # 3 hours
    ["library_station"]="7200"   # 2 hours
    ["demo_showcase"]="3600"     # 1 hour
    ["visitor_workstation"]="14400" # 4 hours
    ["event_registration"]="900"  # 15 minutes
    ["customer_service"]="1800"   # 30 minutes
    ["training_lab"]="21600"     # 6 hours
    ["public_wifi_portal"]="300"  # 5 minutes
    ["emergency_access"]="unlimited"
)

print_guest_categories() {
    echo "=== Guest User Categories ==="
    for category in "${!GUEST_USER_CATEGORIES[@]}"; do
        echo "Category: $category"
        echo "  Description: ${GUEST_USER_CATEGORIES[$category]}"
        echo "  Security Level: ${SECURITY_LEVELS[$category]}"
        echo "  Session Limit: ${SESSION_LIMITS[$category]} seconds"
        echo ""
    done
}

# Display available categories
print_guest_categories

Guest User Policies

Access Policy Engine

#!/bin/bash

# Guest user management policies for different security requirements
declare -A GUEST_POLICIES=(
    ["public_access_secure"]="Secure public access with maximum restrictions"
    ["business_visitor_standard"]="Standard business visitor access with monitoring"
    ["educational_open"]="Educational environment with learning-focused access"
    ["demo_presentation"]="Demonstration and presentation optimized access"
    ["emergency_minimal"]="Emergency access with minimal restrictions"
    ["compliance_strict"]="Strict compliance with full audit and monitoring"
)

# Policy configurations
get_guest_policy() {
    local policy_type="$1"
    
    case "$policy_type" in
        "public_access_secure")
            cat << EOF
{
    "guest_enabled": true,
    "session_timeout": 1800,
    "network_access": false,
    "file_downloads": false,
    "usb_access": false,
    "printing_allowed": false,
    "applications_allowed": ["Safari", "TextEdit", "Preview"],
    "system_preferences_access": false,
    "auto_logout_enabled": true,
    "session_monitoring": "comprehensive",
    "data_retention": "none",
    "audit_logging": "detailed",
    "password_protection": false,
    "screen_sharing_disabled": true,
    "remote_management_disabled": true
}
EOF
            ;;
        "business_visitor_standard")
            cat << EOF
{
    "guest_enabled": true,
    "session_timeout": 14400,
    "network_access": true,
    "file_downloads": true,
    "usb_access": false,
    "printing_allowed": true,
    "applications_allowed": ["Safari", "TextEdit", "Preview", "Mail", "Calendar"],
    "system_preferences_access": false,
    "auto_logout_enabled": true,
    "session_monitoring": "standard",
    "data_retention": "session_only",
    "audit_logging": "standard",
    "password_protection": false,
    "screen_sharing_disabled": true,
    "remote_management_disabled": true,
    "file_sharing_restrictions": true
}
EOF
            ;;
        "compliance_strict")
            cat << EOF
{
    "guest_enabled": true,
    "session_timeout": 3600,
    "network_access": true,
    "file_downloads": false,
    "usb_access": false,
    "printing_allowed": false,
    "applications_allowed": ["Safari"],
    "system_preferences_access": false,
    "auto_logout_enabled": true,
    "session_monitoring": "comprehensive",
    "data_retention": "none",
    "audit_logging": "comprehensive",
    "password_protection": false,
    "screen_sharing_disabled": true,
    "remote_management_disabled": true,
    "compliance_frameworks": ["hipaa", "gdpr", "sox"],
    "data_encryption": "required",
    "access_logging": "detailed",
    "session_recording": "enabled"
}
EOF
            ;;
        *)
            echo "Unknown guest policy: $policy_type"
            return 1
            ;;
    esac
}

# Apply guest user policy
apply_guest_policy() {
    local policy="$1"
    local config_file="/tmp/guest_policy.json"
    
    echo "Applying guest user policy: $policy"
    
    get_guest_policy "$policy" > "$config_file"
    
    if [[ ! -f "$config_file" ]]; then
        echo "❌ Failed to generate policy configuration"
        return 1
    fi
    
    echo "✅ Guest user policy applied successfully"
    echo "Configuration: $config_file"
    
    # Display key policy settings
    echo "=== Policy Summary ==="
    echo "Guest Enabled: $(jq -r '.guest_enabled' "$config_file")"
    echo "Session Timeout: $(jq -r '.session_timeout' "$config_file") seconds"
    echo "Network Access: $(jq -r '.network_access' "$config_file")"
    echo "Session Monitoring: $(jq -r '.session_monitoring' "$config_file")"
    echo "Audit Logging: $(jq -r '.audit_logging' "$config_file")"
    
    # Apply actual guest settings
    apply_guest_settings "$config_file"
    
    return 0
}

# Apply guest settings
apply_guest_settings() {
    local config_file="$1"
    
    echo "Applying guest user settings..."
    
    # Extract settings from JSON
    local guest_enabled
    guest_enabled=$(jq -r '.guest_enabled' "$config_file")
    local session_timeout
    session_timeout=$(jq -r '.session_timeout' "$config_file")
    local auto_logout
    auto_logout=$(jq -r '.auto_logout_enabled' "$config_file")
    
    # Apply guest user settings
    sudo defaults write /Library/Preferences/com.apple.loginwindow GuestEnabled -bool "$guest_enabled"
    
    if [[ "$auto_logout" == "true" ]]; then
        sudo defaults write /Library/Preferences/com.apple.loginwindow GuestAutoLogout -int "$session_timeout"
    fi
    
    echo "✅ Guest settings applied successfully"
}

Advanced Guest User Monitoring

Guest Session Analytics

#!/bin/bash

# Comprehensive guest session monitoring and analytics
monitor_guest_sessions() {
    local monitoring_profile="$1"
    local session_report="/tmp/guest_session_$(date +%Y%m%d_%H%M%S).json"
    
    echo "=== Guest Session Monitoring ==="
    echo "Monitoring Profile: $monitoring_profile"
    
    # Initialize session report
    cat > "$session_report" << EOF
{
    "monitoring_profile": "$monitoring_profile",
    "scan_timestamp": "$(date -Iseconds)",
    "hostname": "$(hostname)",
    "guest_status": {},
    "active_sessions": [],
    "session_analytics": {}
}
EOF
    
    # Check guest user status
    echo "Checking guest user status..."
    local guest_enabled
    guest_enabled=$(defaults read /Library/Preferences/com.apple.loginwindow GuestEnabled 2>/dev/null || echo "false")
    
    local guest_auto_logout
    guest_auto_logout=$(defaults read /Library/Preferences/com.apple.loginwindow GuestAutoLogout 2>/dev/null || echo "0")
    
    # Check for active guest sessions
    echo "Analyzing active sessions..."
    local active_users
    active_users=$(who | grep -v "^$USER" | wc -l | tr -d ' ')
    
    local guest_sessions
    guest_sessions=$(who | grep "Guest" | wc -l | tr -d ' ')
    
    # Monitor session activity
    echo "Monitoring session activity..."
    local login_history
    login_history=$(last | grep "Guest" | head -10)
    
    # Update session report
    jq --arg guest_enabled "$guest_enabled" \
       --argjson guest_auto_logout "$guest_auto_logout" \
       --argjson active_users "$active_users" \
       --argjson guest_sessions "$guest_sessions" \
       '.guest_status = {
          "enabled": ($guest_enabled == "1"),
          "auto_logout_seconds": $guest_auto_logout,
          "active_users": $active_users,
          "active_guest_sessions": $guest_sessions
        }' "$session_report" > "${session_report}.tmp" && mv "${session_report}.tmp" "$session_report"
    
    # Session analytics
    local total_guest_logins
    total_guest_logins=$(last | grep "Guest" | wc -l | tr -d ' ')
    
    local avg_session_duration="unknown"
    if [[ $total_guest_logins -gt 0 ]]; then
        # Calculate average session duration (simplified)
        avg_session_duration="estimated"
    fi
    
    # Display results
    echo ""
    echo "Guest Session Analysis Results:"
    echo "  Guest User Enabled: $([ "$guest_enabled" = "1" ] && echo "✅ YES" || echo "❌ NO")"
    echo "  Auto Logout: ${guest_auto_logout} seconds"
    echo "  Active Users: $active_users"
    echo "  Active Guest Sessions: $guest_sessions"
    echo "  Total Guest Logins (recent): $total_guest_logins"
    echo "  Average Session Duration: $avg_session_duration"
    echo "  Session Report: $session_report"
    
    # Log monitoring activity
    audit_log "Guest session monitoring completed: $monitoring_profile"
    
    return 0
}

Guest User Management System

#!/bin/bash

# Roundfleet Guest User Management System
# Comprehensive guest access control, monitoring, and security

# Configuration
CONFIG_DIR="/etc/roundfleet/guest"
LOG_FILE="/var/log/roundfleet_guest_management.log"
DATA_DIR="/var/data/roundfleet/guest"
REPORTS_DIR="/var/reports/roundfleet/guest"
AUDIT_LOG="/var/log/roundfleet_guest_audit.log"

# Create required directories
create_directories() {
    local directories=("$CONFIG_DIR" "$DATA_DIR" "$REPORTS_DIR")
    
    for dir in "${directories[@]}"; do
        if [[ ! -d "$dir" ]]; then
            sudo mkdir -p "$dir"
            sudo chmod 755 "$dir"
        fi
    done
}

# Logging functions
log_action() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') [INFO] $1" | tee -a "$LOG_FILE"
}

log_error() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') [ERROR] $1" | tee -a "$LOG_FILE" >&2
}

audit_log() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') [AUDIT] $1" | tee -a "$AUDIT_LOG"
}

# Guest user security enforcement
enforce_guest_security() {
    local security_level="$1"
    
    log_action "Enforcing guest security level: $security_level"
    
    echo "=== Guest User Security Enforcement ==="
    echo "Security Level: $security_level"
    
    case "$security_level" in
        "maximum_restriction")
            echo "Applying maximum security restrictions..."
            
            # Disable network access for guest
            sudo defaults write /Library/Preferences/com.apple.loginwindow GuestNetworkAccess -bool FALSE
            
            # Disable removable media access
            sudo defaults write /Library/Preferences/com.apple.loginwindow GuestRemovableMediaAccess -bool FALSE
            
            # Disable printing
            sudo defaults write /Library/Preferences/com.apple.loginwindow GuestPrintingAccess -bool FALSE
            
            # Set strict application restrictions
            sudo defaults write /Library/Preferences/com.apple.loginwindow GuestAllowedApplications -array "Safari"
            
            echo "  ✅ Maximum security restrictions applied"
            ;;
            
        "standard_restriction")
            echo "Applying standard security restrictions..."
            
            # Allow limited network access
            sudo defaults write /Library/Preferences/com.apple.loginwindow GuestNetworkAccess -bool TRUE
            
            # Disable removable media access
            sudo defaults write /Library/Preferences/com.apple.loginwindow GuestRemovableMediaAccess -bool FALSE
            
            # Allow printing with restrictions
            sudo defaults write /Library/Preferences/com.apple.loginwindow GuestPrintingAccess -bool TRUE
            
            echo "  ✅ Standard security restrictions applied"
            ;;
            
        "minimal_restriction")
            echo "Applying minimal security restrictions..."
            
            # Allow network access
            sudo defaults write /Library/Preferences/com.apple.loginwindow GuestNetworkAccess -bool TRUE
            
            # Allow removable media with monitoring
            sudo defaults write /Library/Preferences/com.apple.loginwindow GuestRemovableMediaAccess -bool TRUE
            
            # Allow printing
            sudo defaults write /Library/Preferences/com.apple.loginwindow GuestPrintingAccess -bool TRUE
            
            echo "  ✅ Minimal security restrictions applied"
            ;;
            
        *)
            echo "❌ Unknown security level: $security_level"
            return 1
            ;;
    esac
    
    audit_log "Guest security enforcement completed: $security_level"
    
    return 0
}

# Guest session lifecycle management
manage_guest_lifecycle() {
    local action="$1"
    local parameters="$2"
    
    log_action "Managing guest lifecycle: $action"
    
    case "$action" in
        "cleanup_expired_sessions")
            echo "Cleaning up expired guest sessions..."
            
            # Force logout inactive guest sessions
            pkill -u Guest 2>/dev/null
            
            # Clean guest home directory
            sudo rm -rf /Users/Guest/* 2>/dev/null
            
            echo "  ✅ Expired sessions cleaned up"
            ;;
            
        "reset_guest_environment")
            echo "Resetting guest environment..."
            
            # Reset guest user preferences
            sudo rm -rf /Users/Guest/Library/Preferences/* 2>/dev/null
            
            # Clear guest application data
            sudo rm -rf /Users/Guest/Library/Application\ Support/* 2>/dev/null
            
            # Clear downloads and documents
            sudo rm -rf /Users/Guest/Downloads/* 2>/dev/null
            sudo rm -rf /Users/Guest/Documents/* 2>/dev/null
            
            echo "  ✅ Guest environment reset"
            ;;
            
        "generate_session_report")
            echo "Generating guest session report..."
            
            local report_file="$REPORTS_DIR/guest_session_report_$(date +%Y%m%d_%H%M%S).json"
            
            # Collect session data
            local session_data
            session_data=$(last | grep "Guest" | head -20)
            
            # Generate report
            cat > "$report_file" << EOF
{
    "report_type": "guest_session_analysis",
    "generated": "$(date -Iseconds)",
    "hostname": "$(hostname)",
    "recent_sessions": "$session_data",
    "guest_status": {
        "enabled": $(defaults read /Library/Preferences/com.apple.loginwindow GuestEnabled 2>/dev/null || echo "false"),
        "auto_logout": $(defaults read /Library/Preferences/com.apple.loginwindow GuestAutoLogout 2>/dev/null || echo "0")
    }
}
EOF
            
            echo "  ✅ Session report generated: $report_file"
            ;;
            
        *)
            echo "❌ Unknown lifecycle action: $action"
            return 1
            ;;
    esac
    
    return 0
}

# Guest compliance monitoring
monitor_guest_compliance() {
    local compliance_framework="$1"
    
    log_action "Monitoring guest user compliance: $compliance_framework"
    
    echo "=== Guest User Compliance Monitoring ==="
    echo "Framework: $compliance_framework"
    
    local violations=()
    local compliance_score=100
    
    case "$compliance_framework" in
        "security_standard")
            # Check if guest user is properly configured
            local guest_enabled
            guest_enabled=$(defaults read /Library/Preferences/com.apple.loginwindow GuestEnabled 2>/dev/null || echo "false")
            
            if [[ "$guest_enabled" != "1" && "$guest_enabled" != "true" ]]; then
                violations+=("guest_user_not_properly_configured")
                ((compliance_score -= 20))
            fi
            
            # Check for auto-logout configuration
            local auto_logout
            auto_logout=$(defaults read /Library/Preferences/com.apple.loginwindow GuestAutoLogout 2>/dev/null || echo "0")
            
            if [[ "$auto_logout" == "0" ]]; then
                violations+=("auto_logout_not_configured")
                ((compliance_score -= 15))
            fi
            ;;
            
        "privacy_protection")
            # Check for data retention policies
            if [[ -d "/Users/Guest" && -n "$(ls -A /Users/Guest 2>/dev/null)" ]]; then
                violations+=("guest_data_not_cleaned")
                ((compliance_score -= 25))
            fi
            
            # Check session monitoring
            if [[ ! -f "$AUDIT_LOG" ]]; then
                violations+=("session_monitoring_not_configured")
                ((compliance_score -= 20))
            fi
            ;;
            
        *)
            echo "❌ Unknown compliance framework: $compliance_framework"
            return 1
            ;;
    esac
    
    echo ""
    echo "Compliance Results:"
    echo "  Framework: $compliance_framework"
    echo "  Compliance Score: $compliance_score/100"
    echo "  Violations Found: ${#violations[@]}"
    
    if [[ ${#violations[@]} -gt 0 ]]; then
        echo "  Violations:"
        for violation in "${violations[@]}"; do
            echo "    - $violation"
        done
    else
        echo "  ✅ No violations found"
    fi
    
    audit_log "Guest compliance monitoring completed: $compliance_framework (Score: $compliance_score/100)"
    
    return 0
}

# Main function with command routing
main() {
    local command="$1"
    shift
    
    # Initialize
    create_directories
    
    case "$command" in
        "enable")
            # Enable guest user with basic configuration
            configure_guest_user
            ;;
        "disable")
            # Disable guest user
            sudo defaults write /Library/Preferences/com.apple.loginwindow GuestEnabled -bool FALSE
            echo "Guest user disabled"
            ;;
        "status")
            # Check guest user status
            local status
            status=$(defaults read /Library/Preferences/com.apple.loginwindow GuestEnabled 2>/dev/null || echo "false")
            echo "Guest User Status: $([ "$status" = "1" ] && echo "Enabled" || echo "Disabled")"
            ;;
        "monitor_sessions")
            monitor_guest_sessions "$@"
            ;;
        "apply_policy")
            apply_guest_policy "$@"
            ;;
        "enforce_security")
            enforce_guest_security "$@"
            ;;
        "manage_lifecycle")
            manage_guest_lifecycle "$@"
            ;;
        "compliance_check")
            monitor_guest_compliance "$@"
            ;;
        "show_categories")
            print_guest_categories
            ;;
        "show_policies")
            for policy in public_access_secure business_visitor_standard educational_open demo_presentation emergency_minimal compliance_strict; do
                echo "Policy: $policy"
                get_guest_policy "$policy" | jq .
                echo ""
            done
            ;;
        *)
            echo "Roundfleet Guest User Management System"
            echo "Usage: $0 <command> [options]"
            echo ""
            echo "Commands:"
            echo "  enable                                    - Enable guest user with configuration"
            echo "  disable                                   - Disable guest user"
            echo "  status                                    - Check guest user status"
            echo "  monitor_sessions <profile>                - Monitor guest sessions"
            echo "  apply_policy <policy>                     - Apply guest user policy"
            echo "  enforce_security <level>                  - Enforce security restrictions"
            echo "  manage_lifecycle <action>                 - Manage guest lifecycle"
            echo "  compliance_check <framework>              - Check compliance"
            echo "  show_categories                           - Show guest user categories"
            echo "  show_policies                             - Show guest user policies"
            echo ""
            echo "Examples:"
            echo "  $0 enable"
            echo "  $0 apply_policy public_access_secure"
            echo "  $0 enforce_security maximum_restriction"
            echo "  $0 monitor_sessions comprehensive"
            echo "  $0 manage_lifecycle cleanup_expired_sessions"
            echo "  $0 compliance_check security_standard"
            ;;
    esac
}

# Execute main function with all arguments
main "$@"

Security Considerations

Guest User Security

  • Session Isolation - Complete isolation of guest sessions from system and user data
  • Data Prevention - Prevent data persistence and information leakage
  • Network Restrictions - Control network access and external communications
  • Application Controls - Restrict available applications and system access
  • Monitoring & Auditing - Comprehensive logging of guest activities

Compliance Framework

  • Privacy Protection - Ensure guest sessions don't compromise user privacy
  • Data Security - Prevent unauthorized access to sensitive information
  • Access Controls - Implement proper access restrictions and limitations
  • Session Management - Proper session lifecycle and cleanup procedures
  • Audit Requirements - Maintain compliance with audit and monitoring standards

Troubleshooting Guide

Common Issues

Guest User Not Appearing

  • Verify guest user is enabled: defaults read /Library/Preferences/com.apple.loginwindow GuestEnabled
  • Check login window settings in System Preferences
  • Restart login window process: sudo killall loginwindow

Guest Session Not Auto-Logging Out

  • Verify auto-logout configuration: defaults read /Library/Preferences/com.apple.loginwindow GuestAutoLogout
  • Check for running guest processes preventing logout
  • Manually force guest logout if needed

Guest User Has Too Much Access

  • Review and apply appropriate security policies
  • Check application restrictions and system access controls
  • Verify network and file access limitations

Diagnostic Commands

# Check guest user status
defaults read /Library/Preferences/com.apple.loginwindow GuestEnabled

# Check auto-logout setting
defaults read /Library/Preferences/com.apple.loginwindow GuestAutoLogout

# List active guest sessions
who | grep Guest

# View guest login history
last | grep Guest

Important Notes

  • Data Security - Guest sessions should never have access to sensitive data
  • Session Cleanup - Ensure proper cleanup of guest data on logout
  • Access Restrictions - Implement appropriate restrictions based on use case
  • Monitoring - Maintain logs and monitoring for security and compliance
  • Regular Maintenance - Regularly clean up and reset guest environments
  • Security Testing - Test guest restrictions to ensure proper isolation

Get Versions of Installed Apps on macOS

Managing a Mac fleet requires keeping track of application versions across all devices. This tutorial shows you how to create and deploy scripts to retrieve application versions on macOS devices remotely.

Why Application Version Tracking Matters

Application version tracking is crucial for:

  • Security compliance: Ensuring all devices run supported software versions
  • License management: Tracking software deployments and compliance
  • Update planning: Identifying devices that need application updates
  • Support efficiency: Quickly identifying software versions during troubleshooting
  • Inventory management: Maintaining accurate software asset records

Basic Application Version Retrieval

Using mdls Command

The mdls command reads metadata attributes from files, including application version information stored in the kMDItemVersion attribute.

#!/bin/bash

# Basic script to get application versions
applications=("Safari" "Mail" "Calendar" "Notes" "Contacts")
heading_printed=false

for app in "${applications[@]}"; do
    version=$(mdls -name kMDItemVersion "/Applications/${app}.app" 2>/dev/null | grep -o '".*"' | sed 's/"//g')
    
    if [ -z "$version" ]; then
        echo "Unable to retrieve version for ${app}"
    else
        if [ "$heading_printed" = false ]; then
            echo "Application Versions:"
            heading_printed=true
        fi
        echo "${app}: ${version}"
    fi
done

Enhanced Version Script with User Context

For applications installed in user-specific locations, you may need to run the script with user context:

#!/bin/bash

# Enhanced script with user context support
applications=("Safari" "Mail" "Calendar" "Notes" "Contacts" "Xcode" "Final Cut Pro")
current_user=$(whoami)
heading_printed=false

echo "Checking application versions for user: $current_user"
echo "Date: $(date)"
echo "----------------------------------------"

for app in "${applications[@]}"; do
    # Try system Applications folder first
    version=$(mdls -name kMDItemVersion "/Applications/${app}.app" 2>/dev/null | grep -o '".*"' | sed 's/"//g')
    
    # If not found, try user Applications folder
    if [ -z "$version" ]; then
        version=$(mdls -name kMDItemVersion "/Users/$current_user/Applications/${app}.app" 2>/dev/null | grep -o '".*"' | sed 's/"//g')
    fi
    
    if [ -z "$version" ]; then
        echo "❌ Unable to retrieve version for ${app}"
    else
        if [ "$heading_printed" = false ]; then
            echo "Application Versions:"
            heading_printed=true
        fi
        echo "✅ ${app}: ${version}"
    fi
done

Advanced Application Inventory Scripts

Comprehensive Application Scanner

#!/bin/bash

# Comprehensive application version scanner
OUTPUT_FILE="/tmp/app_versions_$(date +%Y%m%d_%H%M%S).txt"
DEVICE_NAME=$(scutil --get ComputerName)
DEVICE_USER=$(whoami)

echo "Roundfleet Application Version Report" > "$OUTPUT_FILE"
echo "=====================================" >> "$OUTPUT_FILE"
echo "Device: $DEVICE_NAME" >> "$OUTPUT_FILE"
echo "User: $DEVICE_USER" >> "$OUTPUT_FILE"
echo "Date: $(date)" >> "$OUTPUT_FILE"
echo "macOS Version: $(sw_vers -productVersion)" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"

# Function to get app version and additional info
get_app_info() {
    local app_path="$1"
    local app_name=$(basename "$app_path" .app)
    
    if [ -d "$app_path" ]; then
        local version=$(mdls -name kMDItemVersion "$app_path" 2>/dev/null | grep -o '".*"' | sed 's/"//g')
        local bundle_id=$(mdls -name kMDItemCFBundleIdentifier "$app_path" 2>/dev/null | grep -o '".*"' | sed 's/"//g')
        local size=$(du -sh "$app_path" 2>/dev/null | cut -f1)
        local modified=$(stat -f "%Sm" -t "%Y-%m-%d %H:%M:%S" "$app_path" 2>/dev/null)
        
        if [ -n "$version" ]; then
            echo "Application: $app_name" >> "$OUTPUT_FILE"
            echo "  Version: $version" >> "$OUTPUT_FILE"
            echo "  Bundle ID: $bundle_id" >> "$OUTPUT_FILE"
            echo "  Size: $size" >> "$OUTPUT_FILE"
            echo "  Modified: $modified" >> "$OUTPUT_FILE"
            echo "  Path: $app_path" >> "$OUTPUT_FILE"
            echo "" >> "$OUTPUT_FILE"
        fi
    fi
}

# Scan system Applications folder
echo "System Applications:" >> "$OUTPUT_FILE"
echo "-------------------" >> "$OUTPUT_FILE"
if [ -d "/Applications" ]; then
    for app in /Applications/*.app; do
        get_app_info "$app"
    done
fi

# Scan user Applications folder
echo "User Applications:" >> "$OUTPUT_FILE"
echo "-----------------" >> "$OUTPUT_FILE"
if [ -d "/Users/$DEVICE_USER/Applications" ]; then
    for app in "/Users/$DEVICE_USER/Applications"/*.app; do
        get_app_info "$app"
    done
fi

# Scan Utilities folder
echo "Utilities:" >> "$OUTPUT_FILE"
echo "---------" >> "$OUTPUT_FILE"
if [ -d "/Applications/Utilities" ]; then
    for app in "/Applications/Utilities"/*.app; do
        get_app_info "$app"
    done
fi

echo "Report generated: $OUTPUT_FILE"
echo "Total applications found: $(grep -c "Application:" "$OUTPUT_FILE")"

# Optional: Display the report
cat "$OUTPUT_FILE"

Specific Application Version Checker

#!/bin/bash

# Check specific applications commonly used in enterprise environments
declare -A enterprise_apps=(
    ["Microsoft Word"]="/Applications/Microsoft Word.app"
    ["Microsoft Excel"]="/Applications/Microsoft Excel.app"
    ["Microsoft PowerPoint"]="/Applications/Microsoft PowerPoint.app"
    ["Microsoft Outlook"]="/Applications/Microsoft Outlook.app"
    ["Adobe Photoshop 2024"]="/Applications/Adobe Photoshop 2024/Adobe Photoshop 2024.app"
    ["Adobe Illustrator 2024"]="/Applications/Adobe Illustrator 2024/Adobe Illustrator 2024.app"
    ["Slack"]="/Applications/Slack.app"
    ["Zoom"]="/Applications/zoom.us.app"
    ["Google Chrome"]="/Applications/Google Chrome.app"
    ["Firefox"]="/Applications/Firefox.app"
    ["Visual Studio Code"]="/Applications/Visual Studio Code.app"
    ["Docker Desktop"]="/Applications/Docker.app"
    ["Xcode"]="/Applications/Xcode.app"
)

echo "Application Version Check"
echo "==================================="
echo "Device: $(scutil --get ComputerName)"
echo "Date: $(date)"
echo ""

for app_name in "${!enterprise_apps[@]}"; do
    app_path="${enterprise_apps[$app_name]}"
    
    if [ -d "$app_path" ]; then
        version=$(mdls -name kMDItemVersion "$app_path" 2>/dev/null | grep -o '".*"' | sed 's/"//g')
        bundle_id=$(mdls -name kMDItemCFBundleIdentifier "$app_path" 2>/dev/null | grep -o '".*"' | sed 's/"//g')
        
        if [ -n "$version" ]; then
            echo "✅ $app_name: $version"
            echo "   Bundle ID: $bundle_id"
        else
            echo "⚠️  $app_name: Installed but version unavailable"
        fi
    else
        echo "❌ $app_name: Not installed"
    fi
done

# Check for critical security applications
echo ""
echo "Security Applications:"
echo "--------------------"
security_apps=("Antivirus One" "Malwarebytes" "CleanMyMac" "Little Snitch")

for app in "${security_apps[@]}"; do
    if [ -d "/Applications/${app}.app" ]; then
        version=$(mdls -name kMDItemVersion "/Applications/${app}.app" 2>/dev/null | grep -o '".*"' | sed 's/"//g')
        echo "✅ $app: ${version:-Version unavailable}"
    else
        echo "❌ $app: Not installed"
    fi
done

Remote Deployment Scripts

Multi-Device Version Collection

#!/bin/bash

# Script for collecting application versions from multiple Mac devices
# This script should be deployed to each device and results collected centrally

CENTRAL_SERVER="your-roundfleet-server.com"
DEVICE_ID=$(system_profiler SPHardwareDataType | grep "Serial Number" | awk '{print $4}')
REPORT_FILE="/tmp/device_app_report_${DEVICE_ID}.json"

# Create JSON report
create_json_report() {
    local device_name=$(scutil --get ComputerName)
    local device_user=$(whoami)
    local os_version=$(sw_vers -productVersion)
    local timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
    
    cat > "$REPORT_FILE" << EOF
{
  "device_info": {
    "device_id": "$DEVICE_ID",
    "device_name": "$device_name",
    "user": "$device_user",
    "os_version": "$os_version",
    "timestamp": "$timestamp"
  },
  "applications": [
EOF

    local first_app=true
    for app in /Applications/*.app; do
        if [ -d "$app" ]; then
            local app_name=$(basename "$app" .app)
            local version=$(mdls -name kMDItemVersion "$app" 2>/dev/null | grep -o '".*"' | sed 's/"//g')
            local bundle_id=$(mdls -name kMDItemCFBundleIdentifier "$app" 2>/dev/null | grep -o '".*"' | sed 's/"//g')
            
            if [ -n "$version" ]; then
                if [ "$first_app" = false ]; then
                    echo "," >> "$REPORT_FILE"
                fi
                
                cat >> "$REPORT_FILE" << EOF
    {
      "name": "$app_name",
      "version": "$version",
      "bundle_id": "$bundle_id",
      "path": "$app"
    }EOF
                first_app=false
            fi
        fi
    done
    
    echo "" >> "$REPORT_FILE"
    echo "  ]" >> "$REPORT_FILE"
    echo "}" >> "$REPORT_FILE"
}

# Create the report
create_json_report

# Display local results
echo "Application version report created: $REPORT_FILE"
echo "Device ID: $DEVICE_ID"
echo "Total applications: $(grep -c '"name":' "$REPORT_FILE")"

# Optional: Send to central server (uncomment if needed)
# curl -X POST -H "Content-Type: application/json" -d @"$REPORT_FILE" "https://$CENTRAL_SERVER/api/device-reports"

Automated Version Monitoring

#!/bin/bash

# Automated application version monitoring with alerting
ALERT_THRESHOLD_DAYS=30
LOG_FILE="/var/log/roundfleet_app_monitor.log"
ALERT_FILE="/tmp/app_version_alerts.txt"

# Function to log messages
log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}

# Function to check if app needs update
check_app_age() {
    local app_path="$1"
    local app_name=$(basename "$app_path" .app)
    local modified_date=$(stat -f "%Sm" -t "%Y-%m-%d" "$app_path" 2>/dev/null)
    
    if [ -n "$modified_date" ]; then
        local days_old=$(( ($(date +%s) - $(date -j -f "%Y-%m-%d" "$modified_date" +%s)) / 86400 ))
        
        if [ $days_old -gt $ALERT_THRESHOLD_DAYS ]; then
            echo "⚠️  $app_name: Last modified $days_old days ago ($modified_date)" >> "$ALERT_FILE"
            log_message "ALERT: $app_name is $days_old days old"
        fi
    fi
}

# Initialize alert file
echo "Roundfleet Application Age Alerts" > "$ALERT_FILE"
echo "================================" >> "$ALERT_FILE"
echo "Generated: $(date)" >> "$ALERT_FILE"
echo "Threshold: $ALERT_THRESHOLD_DAYS days" >> "$ALERT_FILE"
echo "" >> "$ALERT_FILE"

log_message "Starting application version monitoring"

# Check critical applications
critical_apps=("Safari" "Mail" "Calendar" "Microsoft Word" "Microsoft Excel" "Google Chrome" "Slack" "Zoom")

for app in "${critical_apps[@]}"; do
    if [ -d "/Applications/${app}.app" ]; then
        check_app_age "/Applications/${app}.app"
        
        version=$(mdls -name kMDItemVersion "/Applications/${app}.app" 2>/dev/null | grep -o '".*"' | sed 's/"//g')
        log_message "Checked $app: Version $version"
    else
        log_message "WARNING: $app not found"
    fi
done

# Display alerts if any
if [ -s "$ALERT_FILE" ] && [ $(wc -l < "$ALERT_FILE") -gt 4 ]; then
    echo "⚠️  Application age alerts found:"
    cat "$ALERT_FILE"
    
    # Optional: Send alerts via email or notification service
    # mail -s "Roundfleet App Version Alerts - $(hostname)" [email protected] < "$ALERT_FILE"
fi

log_message "Application version monitoring completed"

Best Practices for Mac Fleet Management

1. Regular Monitoring

  • Schedule version checks weekly or bi-weekly
  • Monitor critical business applications more frequently
  • Track both system and user-installed applications

2. Version Standardization

  • Maintain approved application version lists
  • Create policies for automatic updates
  • Document version compatibility requirements

3. Security Considerations

  • Prioritize security updates for all applications
  • Monitor for applications with known vulnerabilities
  • Implement approval workflows for major version updates

4. Reporting and Analytics

  • Generate regular compliance reports
  • Track update deployment success rates
  • Monitor application usage patterns

5. Automation Integration

  • Integrate with configuration management tools
  • Automate routine version checks
  • Set up alerts for version discrepancies

Troubleshooting Common Issues

Issue: mdls Command Not Found

# Verify command availability
which mdls
# If not found, check system integrity
sudo /usr/bin/mdutil -i on /

Issue: Permission Denied

# Run with appropriate permissions
sudo -u [username] mdls -name kMDItemVersion "/Applications/AppName.app"

Issue: Version Information Unavailable

# Try alternative methods
defaults read "/Applications/AppName.app/Contents/Info.plist" CFBundleShortVersionString
plutil -p "/Applications/AppName.app/Contents/Info.plist" | grep -i version

Issue: Applications in Non-Standard Locations

# Search for applications system-wide
find /Applications /Users/*/Applications -name "*.app" -type d 2>/dev/null

Conclusion

Application version tracking is essential for maintaining a secure and compliant Mac fleet. These scripts provide a foundation for:

  • Automated version discovery and reporting
  • Compliance monitoring and alerting
  • Security vulnerability assessment
  • Software asset management

Regular implementation of these monitoring practices will help ensure your Mac fleet remains secure, compliant, and properly managed.

For more advanced Mac fleet management capabilities, consider integrating these scripts with your existing configuration management and monitoring tools.

Get Application Path on macOS

Learn how to find and retrieve application installation paths on Mac devices using scripts. This is essential for application management, script deployment, and allowlist/blocklist configuration.

Basic App Path Finder

Find the path of an application using its bundle identifier:

#!/bin/bash

# Specify the bundle ID
BUNDLE_ID="com.apple.Safari"

# Find app path using mdfind
APP_PATH=$(mdfind "kMDItemCFBundleIdentifier=='${BUNDLE_ID}'")

if [[ -n "$APP_PATH" ]]; then
    echo "Application path: $APP_PATH"
else
    echo "Application not found: $BUNDLE_ID"
fi

Enhanced Path Discovery

Script with validation and multiple search methods:

#!/bin/bash

# Function to find app path
find_app_path() {
    local bundle_id="$1"
    
    # Method 1: Use mdfind with bundle identifier
    local app_path=$(mdfind "kMDItemCFBundleIdentifier=='${bundle_id}'" | head -1)
    
    if [[ -n "$app_path" && -d "$app_path" ]]; then
        echo "$app_path"
        return 0
    fi
    
    # Method 2: Search common directories
    local common_paths=(
        "/Applications"
        "/System/Applications"
        "/Applications/Utilities"
        "/System/Library/CoreServices"
    )
    
    for path in "${common_paths[@]}"; do
        if [[ -d "$path" ]]; then
            local found=$(find "$path" -name "*.app" -maxdepth 2 -exec defaults read {}/Contents/Info.plist CFBundleIdentifier \; -print 2>/dev/null | grep -B1 "^${bundle_id}$" | grep "\.app$")
            if [[ -n "$found" ]]; then
                echo "$found"
                return 0
            fi
        fi
    done
    
    return 1
}

# Example usage
BUNDLE_ID="${1:-com.apple.Safari}"

echo "Searching for application: $BUNDLE_ID"

if APP_PATH=$(find_app_path "$BUNDLE_ID"); then
    echo "Found application at: $APP_PATH"
    
    # Get additional info
    APP_NAME=$(defaults read "$APP_PATH/Contents/Info.plist" CFBundleName 2>/dev/null)
    APP_VERSION=$(defaults read "$APP_PATH/Contents/Info.plist" CFBundleShortVersionString 2>/dev/null)
    
    echo "Application Name: ${APP_NAME:-Unknown}"
    echo "Version: ${APP_VERSION:-Unknown}"
else
    echo "Application not found: $BUNDLE_ID"
    exit 1
fi

Multiple App Path Finder

Find paths for multiple applications at once:

#!/bin/bash

# Array of bundle IDs to search for
BUNDLE_IDS=(
    "com.apple.Safari"
    "com.apple.finder"
    "com.apple.systempreferences"
    "com.microsoft.Excel"
    "com.adobe.Photoshop"
)

# Function to find app path
find_app_path() {
    local bundle_id="$1"
    mdfind "kMDItemCFBundleIdentifier=='${bundle_id}'" | head -1
}

echo "=== Application Path Report ==="
echo "Generated: $(date)"
echo "Device: $(hostname)"
echo "================================"

for bundle_id in "${BUNDLE_IDS[@]}"; do
    echo "Searching: $bundle_id"
    
    app_path=$(find_app_path "$bundle_id")
    
    if [[ -n "$app_path" && -d "$app_path" ]]; then
        echo "  ✓ Found: $app_path"
        
        # Get app version if available
        version=$(defaults read "$app_path/Contents/Info.plist" CFBundleShortVersionString 2>/dev/null)
        if [[ -n "$version" ]]; then
            echo "  📦 Version: $version"
        fi
    else
        echo "  ✗ Not found"
    fi
    echo
done

Bundle ID Discovery

Find bundle IDs of installed applications:

#!/bin/bash

echo "=== Installed Applications Bundle IDs ==="

# Search in common application directories
SEARCH_PATHS=(
    "/Applications"
    "/System/Applications"
    "/Applications/Utilities"
)

for search_path in "${SEARCH_PATHS[@]}"; do
    if [[ -d "$search_path" ]]; then
        echo "Scanning: $search_path"
        
        find "$search_path" -name "*.app" -maxdepth 2 | while read app_path; do
            app_name=$(basename "$app_path" .app)
            bundle_id=$(defaults read "$app_path/Contents/Info.plist" CFBundleIdentifier 2>/dev/null)
            
            if [[ -n "$bundle_id" ]]; then
                echo "  $app_name -> $bundle_id"
            fi
        done
        echo
    fi
done

Usage with Roundfleet

  1. Modify the BUNDLE_ID variable with your target application
  2. For multiple apps, update the BUNDLE_IDS array
  3. Deploy through Roundfleet's remote script execution
  4. View results in the action history

Common Bundle IDs

ApplicationBundle ID
Safaricom.apple.Safari
Findercom.apple.finder
System Preferencescom.apple.systempreferences
Microsoft Wordcom.microsoft.Word
Google Chromecom.google.Chrome
Firefoxorg.mozilla.firefox
Slackcom.tinyspeck.slackmacgap
Zoomus.zoom.xos

Troubleshooting

No results found: Verify the bundle ID is correct and application is installed Multiple paths returned: Use head -1 to get the first result or filter by specific location Permission denied: Ensure script has necessary file system access permissions


Note: Bundle IDs are case-sensitive. Use the exact format as provided by the application developer.

Foreground Application Monitoring on macOS

Monitor and manage foreground applications across your Roundfleet devices using advanced AppleScript and system monitoring tools. This tutorial covers application tracking, productivity analysis, security monitoring, and enterprise application lifecycle management.

Understanding macOS Application Monitoring

macOS provides several methods for application monitoring and management:

  • AppleScript - Automation language for application control and monitoring
  • System Events - System-level process and application information
  • Activity Monitor - Real-time application and resource monitoring
  • Finder Integration - Application visibility and state management

Basic Foreground Application Detection

Simple Application List

#!/bin/bash

# Basic script to fetch foreground applications
osascript <<EOF
tell application "Finder"
    set visibleProcesses to name of every process whose visible is true
    return visibleProcesses
end tell
EOF

echo "Foreground applications retrieved successfully"

Enhanced Application Information

#!/bin/bash

# Enhanced foreground application monitoring with detailed information
get_foreground_applications() {
    osascript <<EOF
tell application "System Events"
    set foregroundApps to {}
    set visibleProcesses to every process whose visible is true
    
    repeat with aProcess in visibleProcesses
        set appName to name of aProcess
        set appPID to unix id of aProcess
        try
            set appPath to POSIX path of (file of aProcess as alias)
        on error
            set appPath to "N/A"
        end try
        
        set appInfo to appName & "|" & appPID & "|" & appPath
        set foregroundApps to foregroundApps & {appInfo}
    end repeat
    
    return foregroundApps
end tell
EOF
}

# Execute and format output
echo "=== Foreground Applications ==="
get_foreground_applications | tr ',' '\n' | while read -r app_info; do
    if [[ -n "$app_info" ]]; then
        IFS='|' read -r name pid path <<< "$app_info"
        echo "Application: $name"
        echo "  PID: $pid"
        echo "  Path: $path"
        echo ""
    fi
done

Application Monitoring Categories

Application Type Classifications

#!/bin/bash

# Enterprise application categories for monitoring and policy enforcement
declare -A APPLICATION_CATEGORIES=(
    ["productivity_apps"]="Office suites, document editors, project management tools"
    ["development_tools"]="IDEs, code editors, development environments, version control"
    ["communication_apps"]="Email clients, messaging, video conferencing, collaboration"
    ["creative_software"]="Design tools, media editing, creative applications"
    ["browser_applications"]="Web browsers, internet-based applications"
    ["system_utilities"]="System tools, maintenance applications, security software"
    ["entertainment_media"]="Games, streaming, social media, entertainment platforms"
    ["financial_software"]="Accounting, finance, banking, payment applications"
    ["security_tools"]="Antivirus, VPN, security monitoring, encryption tools"
    ["unauthorized_apps"]="Non-approved, potentially harmful, or restricted applications"
)

# Application risk levels
declare -A RISK_LEVELS=(
    ["productivity_apps"]="low"
    ["development_tools"]="medium"
    ["communication_apps"]="low"
    ["creative_software"]="medium"
    ["browser_applications"]="medium"
    ["system_utilities"]="high"
    ["entertainment_media"]="high"
    ["financial_software"]="medium"
    ["security_tools"]="low"
    ["unauthorized_apps"]="critical"
)

# Business impact classifications
declare -A BUSINESS_IMPACT=(
    ["productivity_apps"]="high_positive"
    ["development_tools"]="high_positive"
    ["communication_apps"]="high_positive"
    ["creative_software"]="medium_positive"
    ["browser_applications"]="medium_neutral"
    ["system_utilities"]="medium_positive"
    ["entertainment_media"]="high_negative"
    ["financial_software"]="high_positive"
    ["security_tools"]="high_positive"
    ["unauthorized_apps"]="high_negative"
)

print_application_categories() {
    echo "=== Application Monitoring Categories ==="
    for category in "${!APPLICATION_CATEGORIES[@]}"; do
        echo "Category: $category"
        echo "  Description: ${APPLICATION_CATEGORIES[$category]}"
        echo "  Risk Level: ${RISK_LEVELS[$category]}"
        echo "  Business Impact: ${BUSINESS_IMPACT[$category]}"
        echo ""
    done
}

# Display available categories
print_application_categories

Application Monitoring Policies

Monitoring Policy Engine

#!/bin/bash

# Monitoring policies for different organizational requirements
declare -A MONITORING_POLICIES=(
    ["strict_compliance"]="Comprehensive monitoring with real-time alerts and detailed logging"
    ["productivity_focused"]="Focus on productivity metrics and time tracking"
    ["security_priority"]="Emphasis on security threats and unauthorized application detection"
    ["development_optimized"]="Optimized for software development teams and technical workflows"
    ["creative_workflow"]="Tailored for creative teams and design-focused environments"
    ["executive_monitoring"]="High-level monitoring for executive and leadership teams"
    ["guest_restricted"]="Limited monitoring for guest and temporary access accounts"
)

# Policy configurations
get_monitoring_policy() {
    local policy_type="$1"
    
    case "$policy_type" in
        "strict_compliance")
            cat << EOF
{
    "monitoring_frequency": "continuous",
    "data_retention": "365_days",
    "alert_threshold": "immediate",
    "logging_level": "comprehensive",
    "screenshot_capture": true,
    "productivity_tracking": true,
    "security_scanning": true,
    "compliance_reporting": ["sox", "hipaa", "gdpr", "pci_dss"],
    "real_time_alerts": true,
    "application_blocking": true,
    "time_restrictions": true,
    "approval_workflow": true
}
EOF
            ;;
        "productivity_focused")
            cat << EOF
{
    "monitoring_frequency": "5_minutes",
    "data_retention": "90_days",
    "alert_threshold": "moderate",
    "logging_level": "productivity_metrics",
    "screenshot_capture": false,
    "productivity_tracking": true,
    "security_scanning": false,
    "compliance_reporting": ["time_tracking"],
    "real_time_alerts": false,
    "application_blocking": false,
    "time_restrictions": false,
    "approval_workflow": false,
    "focus_metrics": ["active_time", "app_usage", "productivity_score"]
}
EOF
            ;;
        "security_priority")
            cat << EOF
{
    "monitoring_frequency": "continuous",
    "data_retention": "180_days",
    "alert_threshold": "high",
    "logging_level": "security_focused",
    "screenshot_capture": true,
    "productivity_tracking": false,
    "security_scanning": true,
    "compliance_reporting": ["security_incidents"],
    "real_time_alerts": true,
    "application_blocking": true,
    "time_restrictions": true,
    "approval_workflow": true,
    "threat_detection": ["malware", "unauthorized_apps", "data_exfiltration"],
    "behavioral_analysis": true
}
EOF
            ;;
        *)
            echo "Unknown monitoring policy: $policy_type"
            return 1
            ;;
    esac
}

# Apply monitoring policy
apply_monitoring_policy() {
    local policy="$1"
    local config_file="/tmp/monitoring_policy.json"
    
    echo "Applying monitoring policy: $policy"
    
    get_monitoring_policy "$policy" > "$config_file"
    
    if [[ ! -f "$config_file" ]]; then
        echo "❌ Failed to generate policy configuration"
        return 1
    fi
    
    echo "✅ Monitoring policy applied successfully"
    echo "Configuration: $config_file"
    
    # Display key policy settings
    echo "=== Policy Summary ==="
    echo "Monitoring Frequency: $(jq -r '.monitoring_frequency' "$config_file")"
    echo "Data Retention: $(jq -r '.data_retention' "$config_file")"
    echo "Alert Threshold: $(jq -r '.alert_threshold' "$config_file")"
    echo "Real-time Alerts: $(jq -r '.real_time_alerts' "$config_file")"
    echo "Application Blocking: $(jq -r '.application_blocking' "$config_file")"
    
    return 0
}

Enterprise Deployment Profiles

Deployment Configuration Templates

#!/bin/bash

# Enterprise deployment profiles for different organizational scenarios
declare -A DEPLOYMENT_PROFILES=(
    ["corporate_standard"]="Standard corporate deployment with balanced monitoring and productivity"
    ["high_security_finance"]="High-security monitoring for financial and sensitive departments"
    ["creative_agency"]="Optimized for creative teams with design and media applications"
    ["software_development"]="Development-focused monitoring for engineering teams"
    ["call_center_support"]="Customer service and support team monitoring"
    ["executive_leadership"]="Executive-level monitoring with privacy considerations"
    ["remote_workforce"]="Remote worker monitoring with productivity focus"
    ["guest_contractor"]="Limited monitoring for external contractors and guests"
)

# Profile-specific configurations
get_deployment_config() {
    local profile="$1"
    
    case "$profile" in
        "corporate_standard")
            cat << EOF
{
    "monitoring_policy": "productivity_focused",
    "application_categories": ["productivity_apps", "communication_apps", "browser_applications"],
    "restricted_categories": ["entertainment_media"],
    "monitoring_schedule": "business_hours",
    "productivity_metrics": {
        "enabled": true,
        "daily_reports": true,
        "weekly_summaries": true,
        "goal_tracking": true
    },
    "security_settings": {
        "threat_detection": "standard",
        "unauthorized_app_blocking": true,
        "malware_scanning": true
    },
    "privacy_settings": {
        "screenshot_frequency": "hourly",
        "keystroke_logging": false,
        "personal_app_privacy": true
    },
    "compliance": {
        "data_retention": "90_days",
        "audit_logging": true,
        "reporting_frequency": "weekly"
    }
}
EOF
            ;;
        "high_security_finance")
            cat << EOF
{
    "monitoring_policy": "strict_compliance",
    "application_categories": ["productivity_apps", "financial_software", "communication_apps"],
    "restricted_categories": ["entertainment_media", "unauthorized_apps"],
    "monitoring_schedule": "24_7",
    "productivity_metrics": {
        "enabled": true,
        "real_time_tracking": true,
        "detailed_analytics": true,
        "compliance_scoring": true
    },
    "security_settings": {
        "threat_detection": "advanced",
        "unauthorized_app_blocking": true,
        "malware_scanning": true,
        "data_loss_prevention": true,
        "encryption_verification": true
    },
    "privacy_settings": {
        "screenshot_frequency": "continuous",
        "keystroke_logging": true,
        "personal_app_privacy": false,
        "session_recording": true
    },
    "compliance": {
        "data_retention": "2555_days",
        "audit_logging": true,
        "reporting_frequency": "daily",
        "frameworks": ["sox", "pci_dss", "nist"]
    }
}
EOF
            ;;
        "creative_agency")
            cat << EOF
{
    "monitoring_policy": "productivity_focused",
    "application_categories": ["creative_software", "productivity_apps", "communication_apps", "browser_applications"],
    "restricted_categories": ["unauthorized_apps"],
    "monitoring_schedule": "flexible_hours",
    "productivity_metrics": {
        "enabled": true,
        "creative_workflow_tracking": true,
        "project_time_allocation": true,
        "inspiration_time_allowance": true
    },
    "security_settings": {
        "threat_detection": "standard",
        "unauthorized_app_blocking": false,
        "malware_scanning": true,
        "creative_software_licensing": true
    },
    "privacy_settings": {
        "screenshot_frequency": "never",
        "keystroke_logging": false,
        "personal_app_privacy": true,
        "creative_process_privacy": true
    },
    "compliance": {
        "data_retention": "60_days",
        "audit_logging": false,
        "reporting_frequency": "monthly",
        "client_confidentiality": true
    }
}
EOF
            ;;
        *)
            echo "Unknown deployment profile: $profile"
            return 1
            ;;
    esac
}

# Deploy profile configuration
deploy_monitoring_profile() {
    local profile="$1"
    local config_file="/tmp/deployment_config_${profile}.json"
    
    echo "Deploying monitoring profile: $profile"
    
    get_deployment_config "$profile" > "$config_file"
    
    if [[ ! -f "$config_file" ]]; then
        echo "❌ Failed to generate deployment configuration"
        return 1
    fi
    
    echo "✅ Deployment profile ready"
    echo "Configuration saved to: $config_file"
    
    # Display profile summary
    echo "=== Profile Summary ==="
    echo "Monitoring Policy: $(jq -r '.monitoring_policy' "$config_file")"
    echo "Monitoring Schedule: $(jq -r '.monitoring_schedule' "$config_file")"
    echo "Security Level: $(jq -r '.security_settings.threat_detection' "$config_file")"
    echo "Privacy Mode: $(jq -r '.privacy_settings.personal_app_privacy' "$config_file")"
    echo "Data Retention: $(jq -r '.compliance.data_retention' "$config_file")"
    
    return 0
}

Enterprise Application Management System

#!/bin/bash

# Roundfleet Enterprise Application Monitoring System
# Comprehensive foreground application monitoring and management

# Configuration
CONFIG_DIR="/etc/roundfleet/application_monitoring"
LOG_FILE="/var/log/roundfleet_app_monitoring.log"
DATA_DIR="/var/data/roundfleet/applications"
REPORTS_DIR="/var/reports/roundfleet/applications"
AUDIT_LOG="/var/log/roundfleet_app_audit.log"
ALERTS_DIR="/var/alerts/roundfleet/applications"

# Create required directories
create_directories() {
    local directories=("$CONFIG_DIR" "$DATA_DIR" "$REPORTS_DIR" "$ALERTS_DIR")
    
    for dir in "${directories[@]}"; do
        if [[ ! -d "$dir" ]]; then
            sudo mkdir -p "$dir"
            sudo chmod 755 "$dir"
        fi
    done
}

# Logging functions
log_action() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') [INFO] $1" | tee -a "$LOG_FILE"
}

log_error() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') [ERROR] $1" | tee -a "$LOG_FILE" >&2
}

audit_log() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') [AUDIT] $1" | tee -a "$AUDIT_LOG"
}

# Application database management
initialize_app_database() {
    local db_file="$DATA_DIR/application_database.json"
    
    if [[ ! -f "$db_file" ]]; then
        cat > "$db_file" << EOF
{
    "version": "1.0",
    "created": "$(date -Iseconds)",
    "applications": {},
    "monitoring_sessions": {},
    "productivity_metrics": {},
    "security_events": {}
}
EOF
        log_action "Application database initialized: $db_file"
    fi
}

# Advanced application detection
get_comprehensive_app_info() {
    local output_file="$1"
    local timestamp="$(date -Iseconds)"
    
    # Get foreground applications with detailed information
    osascript <<EOF > "$output_file"
tell application "System Events"
    set appList to {}
    set visibleProcesses to every process whose visible is true
    
    repeat with aProcess in visibleProcesses
        try
            set appName to name of aProcess
            set appPID to unix id of aProcess
            set appMemory to ""
            set appCPU to ""
            set appPath to ""
            set appBundle to ""
            set appVersion to ""
            
            try
                set appPath to POSIX path of (file of aProcess as alias)
            end try
            
            try
                tell application "Finder"
                    set appFile to POSIX file appPath as alias
                    set appBundle to id of (info for appFile)
                    set appVersion to version of (info for appFile)
                end tell
            end try
            
            set appInfo to "{"
            set appInfo to appInfo & "\\"name\\": \\"" & appName & "\\", "
            set appInfo to appInfo & "\\"pid\\": " & appPID & ", "
            set appInfo to appInfo & "\\"path\\": \\"" & appPath & "\\", "
            set appInfo to appInfo & "\\"bundle_id\\": \\"" & appBundle & "\\", "
            set appInfo to appInfo & "\\"version\\": \\"" & appVersion & "\\", "
            set appInfo to appInfo & "\\"timestamp\\": \\"$timestamp\\""
            set appInfo to appInfo & "}"
            
            set appList to appList & {appInfo}
        end try
    end repeat
    
    set AppleScript's text item delimiters to ","
    set result to "[" & (appList as string) & "]"
    set AppleScript's text item delimiters to ""
    
    return result
end tell
EOF
}

# Application categorization and risk assessment
categorize_application() {
    local app_name="$1"
    local app_path="$2"
    local app_bundle="$3"
    
    # Application categorization logic
    case "$app_name" in
        *"Safari"*|*"Chrome"*|*"Firefox"*|*"Edge"*)
            echo "browser_applications"
            ;;
        *"Office"*|*"Word"*|*"Excel"*|*"PowerPoint"*|*"Pages"*|*"Numbers"*|*"Keynote"*)
            echo "productivity_apps"
            ;;
        *"Xcode"*|*"Visual Studio"*|*"IntelliJ"*|*"Eclipse"*|*"Sublime"*|*"Atom"*)
            echo "development_tools"
            ;;
        *"Photoshop"*|*"Illustrator"*|*"Final Cut"*|*"Logic"*|*"Sketch"*|*"Figma"*)
            echo "creative_software"
            ;;
        *"Slack"*|*"Teams"*|*"Zoom"*|*"Skype"*|*"Mail"*|*"Messages"*)
            echo "communication_apps"
            ;;
        *"iTunes"*|*"Spotify"*|*"Netflix"*|*"YouTube"*|*"Game"*|*"Steam"*)
            echo "entertainment_media"
            ;;
        *"Finder"*|*"Terminal"*|*"Activity Monitor"*|*"System Preferences"*)
            echo "system_utilities"
            ;;
        *)
            # Check bundle ID for better categorization
            case "$app_bundle" in
                com.apple.*)
                    echo "system_utilities"
                    ;;
                com.microsoft.*)
                    echo "productivity_apps"
                    ;;
                com.adobe.*)
                    echo "creative_software"
                    ;;
                *)
                    echo "unknown_application"
                    ;;
            esac
            ;;
    esac
}

# Security threat detection
detect_security_threats() {
    local app_data="$1"
    local threats_file="$ALERTS_DIR/security_threats_$(date +%Y%m%d_%H%M%S).json"
    
    echo "=== Security Threat Detection ==="
    echo "Scanning applications for security threats..."
    
    # Known malicious application patterns
    local malicious_patterns=(
        "keylogger"
        "spyware"
        "adware"
        "trojan"
        "backdoor"
        "cryptominer"
        "ransomware"
    )
    
    # Unauthorized application detection
    local unauthorized_apps=()
    local suspicious_apps=()
    
    # Parse application data and check for threats
    while IFS= read -r app_line; do
        if [[ -n "$app_line" ]]; then
            local app_name=$(echo "$app_line" | jq -r '.name' 2>/dev/null)
            local app_path=$(echo "$app_line" | jq -r '.path' 2>/dev/null)
            
            # Check against malicious patterns
            for pattern in "${malicious_patterns[@]}"; do
                if [[ "$app_name" =~ $pattern ]] || [[ "$app_path" =~ $pattern ]]; then
                    suspicious_apps+=("$app_name:$pattern")
                    audit_log "Suspicious application detected: $app_name (pattern: $pattern)"
                fi
            done
            
            # Check for applications in unauthorized locations
            if [[ "$app_path" =~ /tmp/ ]] || [[ "$app_path" =~ /var/tmp/ ]] || [[ "$app_path" =~ /Downloads/ ]]; then
                unauthorized_apps+=("$app_name:$app_path")
                audit_log "Unauthorized location application: $app_name (path: $app_path)"
            fi
        fi
    done <<< "$(echo "$app_data" | jq -c '.[]' 2>/dev/null)"
    
    # Generate threat report
    local threat_report=$(cat << EOF
{
    "scan_timestamp": "$(date -Iseconds)",
    "hostname": "$(hostname)",
    "suspicious_applications": $(printf '%s\n' "${suspicious_apps[@]}" | jq -R . | jq -s .),
    "unauthorized_applications": $(printf '%s\n' "${unauthorized_apps[@]}" | jq -R . | jq -s .),
    "threat_level": "$([ ${#suspicious_apps[@]} -gt 0 ] && echo "high" || echo "low")",
    "recommendations": {
        "immediate_action": $([ ${#suspicious_apps[@]} -gt 0 ] && echo "true" || echo "false"),
        "quarantine_required": $([ ${#suspicious_apps[@]} -gt 2 ] && echo "true" || echo "false"),
        "security_scan": "recommended"
    }
}
EOF
)
    
    echo "$threat_report" > "$threats_file"
    
    if [[ ${#suspicious_apps[@]} -gt 0 ]] || [[ ${#unauthorized_apps[@]} -gt 0 ]]; then
        echo "⚠️  Security threats detected!"
        echo "  Suspicious applications: ${#suspicious_apps[@]}"
        echo "  Unauthorized applications: ${#unauthorized_apps[@]}"
        echo "  Threat report: $threats_file"
        
        # Send immediate alert if high-risk threats found
        if [[ ${#suspicious_apps[@]} -gt 0 ]]; then
            send_security_alert "$threats_file"
        fi
    else
        echo "✅ No security threats detected"
    fi
}

# Productivity analysis
analyze_productivity() {
    local app_data="$1"
    local analysis_file="$REPORTS_DIR/productivity_analysis_$(date +%Y%m%d_%H%M%S).json"
    
    echo "=== Productivity Analysis ==="
    
    # Categorize applications by productivity impact
    local productive_apps=0
    local neutral_apps=0
    local distracting_apps=0
    local total_apps=0
    
    declare -A category_counts
    
    while IFS= read -r app_line; do
        if [[ -n "$app_line" ]]; then
            local app_name=$(echo "$app_line" | jq -r '.name' 2>/dev/null)
            local category=$(categorize_application "$app_name" "" "")
            
            ((total_apps++))
            ((category_counts["$category"]++))
            
            case "$category" in
                "productivity_apps"|"development_tools"|"communication_apps")
                    ((productive_apps++))
                    ;;
                "system_utilities"|"creative_software")
                    ((neutral_apps++))
                    ;;
                "entertainment_media"|"unknown_application")
                    ((distracting_apps++))
                    ;;
            esac
        fi
    done <<< "$(echo "$app_data" | jq -c '.[]' 2>/dev/null)"
    
    # Calculate productivity score (0-100)
    local productivity_score=0
    if [[ $total_apps -gt 0 ]]; then
        productivity_score=$(( (productive_apps * 100 + neutral_apps * 50) / total_apps ))
    fi
    
    # Generate productivity report
    local productivity_report=$(cat << EOF
{
    "analysis_timestamp": "$(date -Iseconds)",
    "hostname": "$(hostname)",
    "productivity_metrics": {
        "total_applications": $total_apps,
        "productive_applications": $productive_apps,
        "neutral_applications": $neutral_apps,
        "distracting_applications": $distracting_apps,
        "productivity_score": $productivity_score
    },
    "category_breakdown": $(for category in "${!category_counts[@]}"; do echo "\"$category\": ${category_counts[$category]}"; done | jq -s 'add'),
    "recommendations": {
        "productivity_level": "$([ $productivity_score -gt 80 ] && echo "excellent" || [ $productivity_score -gt 60 ] && echo "good" || [ $productivity_score -gt 40 ] && echo "moderate" || echo "needs_improvement")",
        "focus_improvement": $([ $distracting_apps -gt 3 ] && echo "true" || echo "false"),
        "suggested_actions": $([ $productivity_score -lt 50 ] && echo "[\"block_distracting_apps\", \"productivity_training\"]" || echo "[\"maintain_current_habits\"]")
    }
}
EOF
)
    
    echo "$productivity_report" > "$analysis_file"
    
    echo "Productivity Analysis Results:"
    echo "  Total Applications: $total_apps"
    echo "  Productive: $productive_apps | Neutral: $neutral_apps | Distracting: $distracting_apps"
    echo "  Productivity Score: $productivity_score/100"
    echo "  Analysis Report: $analysis_file"
}

# Real-time monitoring session
start_monitoring_session() {
    local profile="$1"
    local duration="${2:-3600}" # Default 1 hour
    local session_id="session_$(date +%Y%m%d_%H%M%S)"
    local session_file="$DATA_DIR/${session_id}.json"
    
    log_action "Starting monitoring session: $session_id (Profile: $profile, Duration: ${duration}s)"
    
    # Initialize session data
    cat > "$session_file" << EOF
{
    "session_id": "$session_id",
    "profile": "$profile",
    "start_time": "$(date -Iseconds)",
    "duration": $duration,
    "hostname": "$(hostname)",
    "monitoring_data": []
}
EOF
    
    local end_time=$(($(date +%s) + duration))
    local interval=30 # Monitor every 30 seconds
    
    echo "Monitoring session started: $session_id"
    echo "Duration: $duration seconds"
    echo "Profile: $profile"
    echo "Data file: $session_file"
    
    while [[ $(date +%s) -lt $end_time ]]; do
        local temp_file="/tmp/app_snapshot_$$.json"
        
        # Get current application snapshot
        get_comprehensive_app_info "$temp_file"
        
        if [[ -f "$temp_file" ]]; then
            local timestamp="$(date -Iseconds)"
            
            # Add to session data
            local snapshot=$(cat "$temp_file" | sed 's/^/    /')
            
            # Update session file with new data point
            jq --argjson snapshot "$(cat "$temp_file")" --arg timestamp "$timestamp" \
               '.monitoring_data += [{"timestamp": $timestamp, "applications": $snapshot}]' \
               "$session_file" > "${session_file}.tmp" && mv "${session_file}.tmp" "$session_file"
            
            rm -f "$temp_file"
            
            echo "$(date '+%H:%M:%S') - Snapshot captured ($(jq '.applications | length' <<< "$(cat "$temp_file")" 2>/dev/null || echo "0") apps)"
        fi
        
        sleep $interval
    done
    
    # Finalize session
    jq '.end_time = "'"$(date -Iseconds)"'" | .status = "completed"' "$session_file" > "${session_file}.tmp" && mv "${session_file}.tmp" "$session_file"
    
    log_action "Monitoring session completed: $session_id"
    
    # Generate session analysis
    analyze_monitoring_session "$session_file"
    
    echo "✅ Monitoring session completed: $session_id"
    echo "Session data: $session_file"
}

# Analyze monitoring session data
analyze_monitoring_session() {
    local session_file="$1"
    local analysis_file="${session_file%.json}_analysis.json"
    
    if [[ ! -f "$session_file" ]]; then
        log_error "Session file not found: $session_file"
        return 1
    fi
    
    log_action "Analyzing monitoring session: $(basename "$session_file")"
    
    # Extract session data
    local session_id=$(jq -r '.session_id' "$session_file")
    local profile=$(jq -r '.profile' "$session_file")
    local start_time=$(jq -r '.start_time' "$session_file")
    local end_time=$(jq -r '.end_time' "$session_file")
    
    # Analyze application usage patterns
    local most_used_apps
    most_used_apps=$(jq -r '.monitoring_data[].applications[].name' "$session_file" | sort | uniq -c | sort -nr | head -10)
    
    # Calculate session statistics
    local total_snapshots=$(jq '.monitoring_data | length' "$session_file")
    local unique_apps=$(jq -r '.monitoring_data[].applications[].name' "$session_file" | sort | uniq | wc -l)
    local avg_apps_per_snapshot=$(jq '[.monitoring_data[].applications | length] | add / length' "$session_file")
    
    # Security analysis
    local security_events=0
    local productivity_violations=0
    
    # Generate comprehensive analysis
    cat > "$analysis_file" << EOF
{
    "session_analysis": {
        "session_id": "$session_id",
        "profile": "$profile",
        "duration": {
            "start_time": "$start_time",
            "end_time": "$end_time"
        },
        "statistics": {
            "total_snapshots": $total_snapshots,
            "unique_applications": $unique_apps,
            "average_apps_per_snapshot": $avg_apps_per_snapshot
        },
        "security_analysis": {
            "security_events": $security_events,
            "threat_level": "low",
            "unauthorized_apps": []
        },
        "productivity_analysis": {
            "productivity_violations": $productivity_violations,
            "focus_score": 85,
            "distracting_app_usage": 15
        },
        "compliance": {
            "data_retention_compliant": true,
            "monitoring_policy_compliant": true,
            "privacy_settings_respected": true
        }
    }
}
EOF
    
    echo "Session Analysis Completed:"
    echo "  Session ID: $session_id"
    echo "  Total Snapshots: $total_snapshots"
    echo "  Unique Applications: $unique_apps"
    echo "  Average Apps per Snapshot: $avg_apps_per_snapshot"
    echo "  Analysis File: $analysis_file"
    
    log_action "Session analysis completed: $analysis_file"
}

# Fleet-wide application monitoring
deploy_fleet_monitoring() {
    local fleet_config="$1"
    local monitoring_profile="$2"
    local duration="$3"
    
    log_action "Starting fleet-wide monitoring deployment"
    
    if [[ ! -f "$fleet_config" ]]; then
        log_error "Fleet configuration file not found: $fleet_config"
        return 1
    fi
    
    # Read fleet configuration
    local hosts
    hosts=$(jq -r '.hosts[]' "$fleet_config")
    
    echo "Deploying monitoring to fleet..."
    echo "Profile: $monitoring_profile"
    echo "Duration: $duration seconds"
    
    # Deploy to each host
    while IFS= read -r host; do
        if [[ -n "$host" ]]; then
            echo "Deploying to: $host"
            
            # Copy monitoring script to remote host
            scp "$0" "root@${host}:/tmp/roundfleet_monitor.sh" || {
                log_error "Failed to copy script to $host"
                continue
            }
            
            # Start monitoring session on remote host
            ssh "root@${host}" "chmod +x /tmp/roundfleet_monitor.sh && /tmp/roundfleet_monitor.sh start_monitoring '$monitoring_profile' '$duration' &" || {
                log_error "Failed to start monitoring on $host"
                continue
            }
            
            log_action "✅ Monitoring started on: $host"
        fi
    done <<< "$hosts"
    
    log_action "Fleet monitoring deployment completed"
}

# Generate comprehensive reports
generate_monitoring_report() {
    local report_type="$1"
    local report_name="${2:-monitoring_report_$(date +%Y%m%d_%H%M%S)}"
    local report_file="$REPORTS_DIR/${report_name}.json"
    
    log_action "Generating monitoring report: $report_name (Type: $report_type)"
    
    case "$report_type" in
        "daily_summary")
            generate_daily_summary "$report_file"
            ;;
        "security_audit")
            generate_security_audit "$report_file"
            ;;
        "productivity_analysis")
            generate_productivity_report "$report_file"
            ;;
        "compliance_report")
            generate_compliance_report "$report_file"
            ;;
        *)
            log_error "Unknown report type: $report_type"
            return 1
            ;;
    esac
    
    log_action "✅ Report generated: $report_file"
    echo "Report saved to: $report_file"
}

# Alert system for critical events
send_security_alert() {
    local threat_file="$1"
    local alert_file="$ALERTS_DIR/security_alert_$(date +%Y%m%d_%H%M%S).json"
    
    local threat_data=$(cat "$threat_file")
    local threat_level=$(echo "$threat_data" | jq -r '.threat_level')
    local suspicious_count=$(echo "$threat_data" | jq '.suspicious_applications | length')
    
    # Generate alert
    cat > "$alert_file" << EOF
{
    "alert_type": "security_threat",
    "severity": "$threat_level",
    "timestamp": "$(date -Iseconds)",
    "hostname": "$(hostname)",
    "threat_summary": {
        "suspicious_applications": $suspicious_count,
        "threat_level": "$threat_level",
        "immediate_action_required": $(echo "$threat_data" | jq '.recommendations.immediate_action')
    },
    "details": $threat_data
}
EOF
    
    echo "🚨 SECURITY ALERT GENERATED: $alert_file"
    log_action "Security alert generated: $alert_file"
    audit_log "Security threat detected - Alert: $alert_file"
    
    # In production environment, send to security team
    # send_email_alert "$alert_file"
    # send_slack_notification "$alert_file"
}

# Main function with command routing
main() {
    local command="$1"
    shift
    
    # Initialize
    create_directories
    initialize_app_database
    
    case "$command" in
        "monitor")
            # Basic one-time monitoring
            local temp_file="/tmp/current_apps.json"
            get_comprehensive_app_info "$temp_file"
            if [[ -f "$temp_file" ]]; then
                echo "=== Current Foreground Applications ==="
                jq -r '.[] | "Application: \(.name) | PID: \(.pid) | Path: \(.path)"' "$temp_file"
                rm -f "$temp_file"
            fi
            ;;
        "start_monitoring")
            start_monitoring_session "$@"
            ;;
        "security_scan")
            local temp_file="/tmp/security_scan.json"
            get_comprehensive_app_info "$temp_file"
            if [[ -f "$temp_file" ]]; then
                detect_security_threats "$(cat "$temp_file")"
                rm -f "$temp_file"
            fi
            ;;
        "productivity_analysis")
            local temp_file="/tmp/productivity_analysis.json"
            get_comprehensive_app_info "$temp_file"
            if [[ -f "$temp_file" ]]; then
                analyze_productivity "$(cat "$temp_file")"
                rm -f "$temp_file"
            fi
            ;;
        "deploy_profile")
            deploy_monitoring_profile "$@"
            ;;
        "fleet_deploy")
            deploy_fleet_monitoring "$@"
            ;;
        "generate_report")
            generate_monitoring_report "$@"
            ;;
        "show_categories")
            print_application_categories
            ;;
        "show_policies")
            for policy in strict_compliance productivity_focused security_priority development_optimized creative_workflow executive_monitoring guest_restricted; do
                echo "Policy: $policy"
                echo "  ${MONITORING_POLICIES[$policy]}"
                echo ""
            done
            ;;
        *)
            echo "Roundfleet Enterprise Application Monitoring System"
            echo "Usage: $0 <command> [options]"
            echo ""
            echo "Commands:"
            echo "  monitor                                    - Get current foreground applications"
            echo "  start_monitoring <profile> [duration]     - Start monitoring session"
            echo "  security_scan                             - Scan for security threats"
            echo "  productivity_analysis                     - Analyze productivity metrics"
            echo "  deploy_profile <profile>                  - Deploy monitoring profile"
            echo "  fleet_deploy <fleet_config> <profile> <duration> - Deploy to fleet"
            echo "  generate_report <type> [name]             - Generate monitoring report"
            echo "  show_categories                           - Show application categories"
            echo "  show_policies                             - Show monitoring policies"
            echo ""
            echo "Examples:"
            echo "  $0 monitor"
            echo "  $0 start_monitoring productivity_focused 3600"
            echo "  $0 security_scan"
            echo "  $0 deploy_profile corporate_standard"
            echo "  $0 generate_report daily_summary"
            ;;
    esac
}

# Execute main function with all arguments
main "$@"

Fleet Deployment Configuration

Fleet Configuration Example

{
    "fleet_name": "Roundfleet Corporate Network",
    "deployment_date": "2025-07-07",
    "hosts": [
        "mac-dev-01.company.com",
        "mac-design-01.company.com", 
        "mac-exec-01.company.com",
        "mac-support-01.company.com"
    ],
    "monitoring_schedule": {
        "business_hours": "09:00-18:00",
        "timezone": "UTC-8",
        "weekdays_only": true
    },
    "global_settings": {
        "data_retention": "90_days",
        "reporting_frequency": "daily",
        "alert_escalation": true
    }
}

Monitoring Session Example

# Start 2-hour productivity monitoring session
./roundfleet_monitor.sh start_monitoring productivity_focused 7200

# Security scan of current applications  
./roundfleet_monitor.sh security_scan

# Deploy corporate monitoring profile
./roundfleet_monitor.sh deploy_profile corporate_standard

# Generate daily productivity report
./roundfleet_monitor.sh generate_report daily_summary

Security Considerations

Application Security

  • Threat Detection - Real-time scanning for malicious applications
  • Unauthorized App Blocking - Prevent execution of non-approved software
  • Behavioral Analysis - Monitor application usage patterns for anomalies
  • Data Loss Prevention - Track applications with data access capabilities
  • Encryption Verification - Ensure sensitive applications use proper encryption

Privacy Protection

  • Configurable Screenshot Capture - Respect user privacy based on policies
  • Personal Application Privacy - Separate monitoring for personal vs. business apps
  • Data Anonymization - Remove personally identifiable information from reports
  • Consent Management - Clear user consent for monitoring activities

Compliance Framework

Regulatory Compliance

  • SOX Compliance - Financial application monitoring and controls
  • HIPAA Compliance - Healthcare application security and access tracking
  • GDPR Compliance - Data protection and privacy compliance
  • PCI DSS - Payment application security monitoring
  • NIST Framework - Cybersecurity framework implementation

Audit Requirements

  • Comprehensive Logging - All application activities tracked and logged
  • Data Retention Policies - Configurable retention based on compliance needs
  • Access Controls - Role-based access to monitoring data and reports
  • Chain of Custody - Secure handling of monitoring data and evidence

Troubleshooting Guide

Common Issues

AppleScript Permission Errors

  • Grant permissions to roundfleetagentd for Finder and System Events access
  • Check System Preferences > Security & Privacy > Privacy > Automation
  • Verify MDM profile allows automation access

Incomplete Application Data

  • Some applications may not report full information
  • System applications might have limited metadata
  • Sandboxed applications may restrict access to certain properties

Performance Impact

  • Continuous monitoring can affect system performance
  • Adjust monitoring frequency based on system capabilities
  • Monitor system resources during extended monitoring sessions

Diagnostic Commands

# Test AppleScript execution
osascript -e 'tell application "System Events" to get name of every process whose visible is true'

# Check permissions
sqlite3 ~/Library/Application\ Support/com.apple.TCC/TCC.db "SELECT client,service,auth_value FROM access WHERE service='kTCCServiceAppleEvents';"

# Monitor system resources
top -l 1 -s 0 | grep "CPU usage"

Important Notes

  • User Privacy - Respect user privacy and obtain proper consent for monitoring
  • Performance Impact - Monitor system performance during extended monitoring sessions
  • Data Security - Encrypt and secure all monitoring data and reports
  • Compliance Requirements - Ensure monitoring practices meet regulatory requirements
  • Regular Updates - Keep application signatures and threat detection patterns updated
  • Documentation - Maintain detailed records of monitoring policies and procedures

Font Management on macOS

Manage and deploy fonts across your Roundfleet devices using advanced command-line tools and centralized distribution methods. This tutorial covers font installation, typography standardization, license compliance, and enterprise-grade font management with comprehensive monitoring and reporting capabilities.

Understanding macOS Font Management

macOS provides several locations and methods for font management:

  • ~/Library/Fonts/ - User-specific fonts (current user only)
  • /Library/Fonts/ - System-wide fonts (all users)
  • /Library/User Template/Non_localized/Library/Fonts/ - Template for new users
  • Font Book - macOS native font management application
  • Font formats: TTF (TrueType), OTF (OpenType), WOFF, and legacy formats

Enterprise font management requires careful consideration of licensing, standardization, and deployment strategies.

Basic Font Installation Commands

Install Font for Current User

#!/bin/bash

# Install font for the current user only
install_user_font() {
    local font_path="$1"
    local font_name="$2"
    
    if [[ -z "$font_path" || -z "$font_name" ]]; then
        echo "Usage: install_user_font <source_path> <font_name>"
        return 1
    fi
    
    # Copy font to user's font directory
    cp -R "$font_path/$font_name" ~/Library/Fonts/
    
    if [[ $? -eq 0 ]]; then
        echo "✅ Font '$font_name' installed for current user"
        return 0
    else
        echo "❌ Failed to install font '$font_name'"
        return 1
    fi
}

# Example usage
# install_user_font "/Users/user1/Downloads" "CROCHETPATTERN.ttf"

Install Font System-Wide

#!/bin/bash

# Install font for all users (requires admin privileges)
install_system_font() {
    local font_path="$1"
    local font_name="$2"
    
    if [[ -z "$font_path" || -z "$font_name" ]]; then
        echo "Usage: install_system_font <source_path> <font_name>"
        return 1
    fi
    
    # Verify admin privileges
    if [[ $EUID -ne 0 ]]; then
        echo "❌ Admin privileges required for system-wide font installation"
        return 1
    fi
    
    # Copy font to system font directory
    cp -R "$font_path/$font_name" /Library/Fonts/
    
    if [[ $? -eq 0 ]]; then
        echo "✅ Font '$font_name' installed system-wide"
        
        # Clear font cache to ensure immediate availability
        atsutil databases -remove
        atsutil server -shutdown
        atsutil server -ping
        
        return 0
    else
        echo "❌ Failed to install font '$font_name' system-wide"
        return 1
    fi
}

# Example usage (run with sudo)
# install_system_font "/Users/user1/Downloads" "CROCHETPATTERN.ttf"

Install Font for New User Template

#!/bin/bash

# Install font in user template (affects new users only)
install_template_font() {
    local font_path="$1"
    local font_name="$2"
    
    if [[ -z "$font_path" || -z "$font_name" ]]; then
        echo "Usage: install_template_font <source_path> <font_name>"
        return 1
    fi
    
    # Verify admin privileges
    if [[ $EUID -ne 0 ]]; then
        echo "❌ Admin privileges required for user template modification"
        return 1
    fi
    
    # Create template font directory if it doesn't exist
    mkdir -p "/Library/User Template/Non_localized/Library/Fonts"
    
    # Copy font to user template
    cp -R "$font_path/$font_name" "/Library/User Template/Non_localized/Library/Fonts/"
    
    if [[ $? -eq 0 ]]; then
        echo "✅ Font '$font_name' added to user template"
        return 0
    else
        echo "❌ Failed to add font '$font_name' to user template"
        return 1
    fi
}

# Example usage (run with sudo)
# install_template_font "/Users/user1/Downloads" "CROCHETPATTERN.ttf"

Advanced Font Management

Font Validation and Information

#!/bin/bash

# Validate and extract font information
validate_font() {
    local font_file="$1"
    
    if [[ ! -f "$font_file" ]]; then
        echo "❌ Font file not found: $font_file"
        return 1
    fi
    
    echo "=== Font Validation Report ==="
    echo "Font File: $font_file"
    echo "File Size: $(ls -lh "$font_file" | awk '{print $5}')"
    echo "File Type: $(file "$font_file")"
    echo ""
    
    # Check if it's a valid font file
    local file_type
    file_type=$(file -b "$font_file")
    
    if echo "$file_type" | grep -qi "font\|truetype\|opentype"; then
        echo "✅ Valid font file detected"
        
        # Extract font information using fc-query if available
        if command -v fc-query >/dev/null 2>&1; then
            echo ""
            echo "Font Details:"
            fc-query "$font_file" | grep -E "(family|style|weight|slant)" | head -10
        fi
        
        # Check for potential issues
        local file_size
        file_size=$(stat -f%z "$font_file" 2>/dev/null || stat -c%s "$font_file" 2>/dev/null)
        
        if [[ "$file_size" -gt 10485760 ]]; then  # 10MB
            echo "⚠️  Warning: Large font file (>10MB) - may impact system performance"
        fi
        
        if [[ "$file_size" -lt 1024 ]]; then  # 1KB
            echo "⚠️  Warning: Very small font file (<1KB) - may be corrupted"
        fi
        
        return 0
    else
        echo "❌ Invalid font file format"
        return 1
    fi
}

# Example usage
# validate_font "/Users/user1/Downloads/CROCHETPATTERN.ttf"

Font Discovery and Inventory

#!/bin/bash

# Discover and inventory installed fonts
discover_fonts() {
    echo "=== Font Inventory Report ==="
    echo "Generated: $(date)"
    echo "Hostname: $(hostname)"
    echo "=============================="
    echo ""
    
    # System fonts
    echo "1. SYSTEM FONTS (/Library/Fonts/):"
    echo "-----------------------------------"
    if [[ -d "/Library/Fonts" ]]; then
        local sys_count
        sys_count=$(ls -1 /Library/Fonts/ 2>/dev/null | wc -l | tr -d ' ')
        echo "Total system fonts: $sys_count"
        echo ""
        ls -la /Library/Fonts/ | head -10
        if [[ "$sys_count" -gt 10 ]]; then
            echo "... and $((sys_count - 10)) more fonts"
        fi
    else
        echo "System fonts directory not found"
    fi
    echo ""
    
    # User fonts (current user)
    echo "2. USER FONTS (~/Library/Fonts/):"
    echo "---------------------------------"
    if [[ -d "$HOME/Library/Fonts" ]]; then
        local user_count
        user_count=$(ls -1 ~/Library/Fonts/ 2>/dev/null | wc -l | tr -d ' ')
        echo "Total user fonts: $user_count"
        echo ""
        if [[ "$user_count" -gt 0 ]]; then
            ls -la ~/Library/Fonts/ | head -10
            if [[ "$user_count" -gt 10 ]]; then
                echo "... and $((user_count - 10)) more fonts"
            fi
        else
            echo "No user-specific fonts installed"
        fi
    else
        echo "User fonts directory not found"
    fi
    echo ""
    
    # Font formats analysis
    echo "3. FONT FORMAT ANALYSIS:"
    echo "------------------------"
    local ttf_count otf_count other_count
    ttf_count=$(find /Library/Fonts ~/Library/Fonts -name "*.ttf" 2>/dev/null | wc -l | tr -d ' ')
    otf_count=$(find /Library/Fonts ~/Library/Fonts -name "*.otf" 2>/dev/null | wc -l | tr -d ' ')
    other_count=$(find /Library/Fonts ~/Library/Fonts -type f ! -name "*.ttf" ! -name "*.otf" 2>/dev/null | wc -l | tr -d ' ')
    
    echo "TrueType fonts (.ttf): $ttf_count"
    echo "OpenType fonts (.otf): $otf_count"
    echo "Other formats: $other_count"
}

# Execute font discovery
discover_fonts

Enterprise Font Management System

#!/bin/bash

# Roundfleet Enterprise Font Management System
# Comprehensive font deployment, management, and compliance monitoring

# Configuration
LOG_FILE="/var/log/roundfleet_font_management.log"
FONT_REPO_DIR="/var/lib/roundfleet/fonts"
CONFIG_FILE="/etc/roundfleet/font_config.conf"
BACKUP_DIR="/var/backups/roundfleet/fonts"
STAGING_DIR="/tmp/roundfleet_fonts"

# Create directory structure
setup_directories() {
    mkdir -p "$(dirname "$LOG_FILE")" "$FONT_REPO_DIR" "$BACKUP_DIR" "$STAGING_DIR" "$(dirname "$CONFIG_FILE")"
    touch "$LOG_FILE"
    
    # Set appropriate permissions
    chmod 755 "$FONT_REPO_DIR" "$BACKUP_DIR" "$STAGING_DIR"
}

# Logging function
log_action() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}

# Download and validate font from URL
download_font() {
    local font_url="$1"
    local font_name="$2"
    local license_info="$3"
    
    if [[ -z "$font_url" || -z "$font_name" ]]; then
        log_action "ERROR: Font URL and name required"
        return 1
    fi
    
    log_action "Downloading font: $font_name from $font_url"
    
    # Download to staging directory
    local download_path="$STAGING_DIR/$font_name"
    
    if command -v curl >/dev/null 2>&1; then
        curl -L -o "$download_path" "$font_url"
    elif command -v wget >/dev/null 2>&1; then
        wget -O "$download_path" "$font_url"
    else
        log_action "ERROR: No download tool available (curl or wget required)"
        return 1
    fi
    
    if [[ ! -f "$download_path" ]]; then
        log_action "ERROR: Download failed for $font_name"
        return 1
    fi
    
    # Validate downloaded font
    if validate_font "$download_path" >/dev/null; then
        # Move to font repository
        mv "$download_path" "$FONT_REPO_DIR/"
        
        # Create metadata file
        cat > "$FONT_REPO_DIR/${font_name}.meta" <<EOF
{
    "font_name": "$font_name",
    "download_url": "$font_url",
    "download_date": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
    "license_info": "$license_info",
    "file_size": $(stat -f%z "$FONT_REPO_DIR/$font_name" 2>/dev/null || stat -c%s "$FONT_REPO_DIR/$font_name"),
    "checksum": "$(md5 -q "$FONT_REPO_DIR/$font_name" 2>/dev/null || md5sum "$FONT_REPO_DIR/$font_name" | cut -d' ' -f1)"
}
EOF
        
        log_action "✅ Font downloaded and validated: $font_name"
        return 0
    else
        log_action "❌ Font validation failed: $font_name"
        rm -f "$download_path"
        return 1
    fi
}

# Deploy font across fleet with options
deploy_font() {
    local font_name="$1"
    local deployment_scope="${2:-system}"  # user, system, template
    local force_install="${3:-false}"
    
    if [[ ! -f "$FONT_REPO_DIR/$font_name" ]]; then
        log_action "ERROR: Font not found in repository: $font_name"
        return 1
    fi
    
    log_action "Deploying font: $font_name (scope: $deployment_scope)"
    
    # Create backup of existing fonts
    backup_fonts
    
    case "$deployment_scope" in
        "user")
            # Install for current user
            mkdir -p ~/Library/Fonts
            cp -R "$FONT_REPO_DIR/$font_name" ~/Library/Fonts/
            local result=$?
            ;;
        "system")
            # Install system-wide (requires admin)
            if [[ $EUID -ne 0 ]]; then
                log_action "ERROR: Admin privileges required for system deployment"
                return 1
            fi
            cp -R "$FONT_REPO_DIR/$font_name" /Library/Fonts/
            local result=$?
            ;;
        "template")
            # Install in user template
            if [[ $EUID -ne 0 ]]; then
                log_action "ERROR: Admin privileges required for template deployment"
                return 1
            fi
            mkdir -p "/Library/User Template/Non_localized/Library/Fonts"
            cp -R "$FONT_REPO_DIR/$font_name" "/Library/User Template/Non_localized/Library/Fonts/"
            local result=$?
            ;;
        *)
            log_action "ERROR: Invalid deployment scope: $deployment_scope"
            return 1
            ;;
    esac
    
    if [[ $result -eq 0 ]]; then
        # Clear font cache
        refresh_font_cache
        
        # Verify installation
        verify_font_installation "$font_name" "$deployment_scope"
        
        log_action "✅ Font deployed successfully: $font_name"
        return 0
    else
        log_action "❌ Font deployment failed: $font_name"
        return 1
    fi
}

# Refresh font cache
refresh_font_cache() {
    log_action "Refreshing system font cache..."
    
    # Clear macOS font cache
    atsutil databases -remove 2>/dev/null
    atsutil server -shutdown 2>/dev/null
    sleep 2
    atsutil server -ping 2>/dev/null
    
    # Clear user font cache if fontconfig is available
    if command -v fc-cache >/dev/null 2>&1; then
        fc-cache -f -v 2>/dev/null
    fi
    
    log_action "Font cache refreshed"
}

# Verify font installation
verify_font_installation() {
    local font_name="$1"
    local scope="$2"
    
    local installed=false
    
    case "$scope" in
        "user")
            if [[ -f "$HOME/Library/Fonts/$font_name" ]]; then
                installed=true
            fi
            ;;
        "system")
            if [[ -f "/Library/Fonts/$font_name" ]]; then
                installed=true
            fi
            ;;
        "template")
            if [[ -f "/Library/User Template/Non_localized/Library/Fonts/$font_name" ]]; then
                installed=true
            fi
            ;;
    esac
    
    if [[ "$installed" == "true" ]]; then
        log_action "✅ Font installation verified: $font_name ($scope)"
        return 0
    else
        log_action "❌ Font installation verification failed: $font_name ($scope)"
        return 1
    fi
}

# Backup existing fonts
backup_fonts() {
    local backup_timestamp="$(date +%Y%m%d_%H%M%S)"
    local backup_path="$BACKUP_DIR/fonts_backup_$backup_timestamp"
    
    log_action "Creating font backup: $backup_path"
    
    mkdir -p "$backup_path"
    
    # Backup system fonts
    if [[ -d "/Library/Fonts" ]]; then
        cp -R /Library/Fonts "$backup_path/system_fonts" 2>/dev/null
    fi
    
    # Backup user fonts
    if [[ -d "$HOME/Library/Fonts" ]]; then
        cp -R "$HOME/Library/Fonts" "$backup_path/user_fonts" 2>/dev/null
    fi
    
    # Create backup manifest
    {
        echo "Font Backup Manifest"
        echo "==================="
        echo "Backup Date: $(date)"
        echo "Hostname: $(hostname)"
        echo "User: $(whoami)"
        echo ""
        echo "System Fonts:"
        ls -la "$backup_path/system_fonts/" 2>/dev/null || echo "None"
        echo ""
        echo "User Fonts:"
        ls -la "$backup_path/user_fonts/" 2>/dev/null || echo "None"
    } > "$backup_path/manifest.txt"
    
    log_action "Font backup completed: $backup_path"
}

# Font compliance and licensing audit
audit_font_compliance() {
    local audit_file="$BACKUP_DIR/font_compliance_audit_$(date +%Y%m%d_%H%M%S).txt"
    
    log_action "Performing font compliance audit: $audit_file"
    
    {
        echo "Roundfleet Font Compliance Audit"
        echo "==============================="
        echo "Generated: $(date)"
        echo "Hostname: $(hostname)"
        echo "Auditor: $(whoami)"
        echo ""
        
        echo "FONT REPOSITORY INVENTORY:"
        echo "-------------------------"
        if [[ -d "$FONT_REPO_DIR" ]]; then
            ls -la "$FONT_REPO_DIR" | grep -v "\.meta$"
            echo ""
            
            echo "FONT METADATA REVIEW:"
            echo "--------------------"
            for meta_file in "$FONT_REPO_DIR"/*.meta; do
                if [[ -f "$meta_file" ]]; then
                    echo "Font: $(basename "$meta_file" .meta)"
                    cat "$meta_file" | grep -E "(license_info|download_url)" || echo "No license info available"
                    echo ""
                fi
            done
        else
            echo "Font repository not found"
        fi
        
        echo "INSTALLED FONTS ANALYSIS:"
        echo "-------------------------"
        echo "System Fonts Location: /Library/Fonts/"
        ls -la /Library/Fonts/ | wc -l | awk '{print "Total system fonts: " $1-1}'
        echo ""
        
        echo "User Fonts Location: ~/Library/Fonts/"
        ls -la ~/Library/Fonts/ 2>/dev/null | wc -l | awk '{print "Total user fonts: " $1-1}' || echo "No user fonts directory"
        echo ""
        
        echo "COMPLIANCE RECOMMENDATIONS:"
        echo "--------------------------"
        echo "• Verify all fonts have proper licensing documentation"
        echo "• Remove any fonts without clear licensing terms"
        echo "• Document font usage for corporate compliance"
        echo "• Implement regular font audits"
        echo "• Maintain font deployment logs for tracking"
        
    } > "$audit_file"
    
    log_action "Font compliance audit completed: $audit_file"
    echo "$audit_file"
}

# Remove font
remove_font() {
    local font_name="$1"
    local scope="${2:-all}"  # user, system, template, all
    
    log_action "Removing font: $font_name (scope: $scope)"
    
    local removed=false
    
    case "$scope" in
        "user"|"all")
            if [[ -f "$HOME/Library/Fonts/$font_name" ]]; then
                rm -f "$HOME/Library/Fonts/$font_name"
                log_action "Removed user font: $font_name"
                removed=true
            fi
            ;;& # Continue to next case
        "system"|"all")
            if [[ -f "/Library/Fonts/$font_name" ]]; then
                if [[ $EUID -eq 0 ]]; then
                    rm -f "/Library/Fonts/$font_name"
                    log_action "Removed system font: $font_name"
                    removed=true
                else
                    log_action "WARNING: Admin privileges required to remove system font"
                fi
            fi
            ;;& # Continue to next case
        "template"|"all")
            if [[ -f "/Library/User Template/Non_localized/Library/Fonts/$font_name" ]]; then
                if [[ $EUID -eq 0 ]]; then
                    rm -f "/Library/User Template/Non_localized/Library/Fonts/$font_name"
                    log_action "Removed template font: $font_name"
                    removed=true
                else
                    log_action "WARNING: Admin privileges required to remove template font"
                fi
            fi
            ;;
    esac
    
    if [[ "$removed" == "true" ]]; then
        refresh_font_cache
        log_action "✅ Font removal completed: $font_name"
        return 0
    else
        log_action "❌ Font not found or removal failed: $font_name"
        return 1
    fi
}

# Generate font deployment report
generate_font_report() {
    local report_file="$BACKUP_DIR/font_deployment_report_$(date +%Y%m%d_%H%M%S).json"
    
    log_action "Generating font deployment report: $report_file"
    
    {
        echo "{"
        echo "  \"report_type\": \"font_deployment\","
        echo "  \"timestamp\": \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\","
        echo "  \"hostname\": \"$(hostname)\","
        echo "  \"system_info\": {"
        echo "    \"macos_version\": \"$(sw_vers -productVersion)\","
        echo "    \"user\": \"$(whoami)\""
        echo "  },"
        echo "  \"font_statistics\": {"
        
        # Count fonts
        local sys_fonts user_fonts repo_fonts
        sys_fonts=$(ls -1 /Library/Fonts/ 2>/dev/null | wc -l | tr -d ' ')
        user_fonts=$(ls -1 ~/Library/Fonts/ 2>/dev/null | wc -l | tr -d ' ')
        repo_fonts=$(ls -1 "$FONT_REPO_DIR"/*.ttf "$FONT_REPO_DIR"/*.otf 2>/dev/null | wc -l | tr -d ' ')
        
        echo "    \"system_fonts\": $sys_fonts,"
        echo "    \"user_fonts\": $user_fonts,"
        echo "    \"repository_fonts\": $repo_fonts"
        echo "  },"
        
        echo "  \"repository_fonts\": ["
        local first_font=true
        for font_file in "$FONT_REPO_DIR"/*.ttf "$FONT_REPO_DIR"/*.otf; do
            if [[ -f "$font_file" ]]; then
                if [[ "$first_font" == "false" ]]; then
                    echo ","
                fi
                first_font=false
                
                local font_name
                font_name=$(basename "$font_file")
                local file_size
                file_size=$(stat -f%z "$font_file" 2>/dev/null || stat -c%s "$font_file")
                
                echo "    {"
                echo "      \"name\": \"$font_name\","
                echo "      \"size\": $file_size,"
                echo "      \"path\": \"$font_file\""
                echo -n "    }"
            fi
        done
        echo ""
        echo "  ]"
        echo "}"
    } > "$report_file"
    
    log_action "Font deployment report generated: $report_file"
    echo "$report_file"
}

# Main management function
main() {
    local action="${1:-status}"
    local parameter1="$2"
    local parameter2="$3"
    local parameter3="$4"
    
    setup_directories
    log_action "Roundfleet Font Management started with action: $action"
    
    case "$action" in
        "download")
            download_font "$parameter1" "$parameter2" "$parameter3"
            ;;
        "deploy")
            deploy_font "$parameter1" "$parameter2" "$parameter3"
            ;;
        "remove")
            remove_font "$parameter1" "$parameter2"
            ;;
        "validate")
            validate_font "$parameter1"
            ;;
        "discover")
            discover_fonts
            ;;
        "audit")
            audit_font_compliance
            ;;
        "backup")
            backup_fonts
            ;;
        "cache")
            refresh_font_cache
            ;;
        "report")
            generate_font_report
            ;;
        "status"|*)
            discover_fonts
            ;;
    esac
    
    log_action "Roundfleet Font Management completed with action: $action"
}

# Execute main function with all arguments
main "$@"

Font Management Templates

Corporate Font Package Configuration

# /etc/roundfleet/font_config.conf
# Roundfleet Font Management Configuration

# Corporate font packages
CORPORATE_FONTS=(
    "Arial-Bold.ttf"
    "Helvetica-Light.otf"
    "TimesNewRoman.ttf"
    "Calibri-Regular.ttf"
    "CompanyLogo-Font.otf"
)

# Font repository settings
FONT_REPO_URL="https://fonts.company.com/repository"
AUTO_UPDATE_FONTS="true"
UPDATE_INTERVAL_HOURS="24"

# Deployment settings
DEFAULT_DEPLOYMENT_SCOPE="system"
REQUIRE_LICENSE_VALIDATION="true"
BACKUP_BEFORE_DEPLOYMENT="true"

# Compliance settings
FONT_AUDIT_ENABLED="true"
LICENSE_TRACKING="mandatory"
USAGE_ANALYTICS="enabled"

Font Deployment Script

#!/bin/bash

# Automated font deployment for corporate standards
deploy_corporate_fonts() {
    local font_package_url="$1"
    local deployment_scope="${2:-system}"
    
    echo "🎨 Deploying Corporate Font Package"
    echo "==================================="
    
    # Download font package
    local package_file="/tmp/corporate_fonts.zip"
    echo "Downloading font package..."
    
    if curl -L -o "$package_file" "$font_package_url"; then
        echo "✅ Package downloaded successfully"
    else
        echo "❌ Package download failed"
        return 1
    fi
    
    # Extract and validate fonts
    local extract_dir="/tmp/corporate_fonts"
    mkdir -p "$extract_dir"
    
    if command -v unzip >/dev/null 2>&1; then
        unzip -q "$package_file" -d "$extract_dir"
    else
        echo "❌ unzip command not available"
        return 1
    fi
    
    # Install each font
    local installed_count=0
    local failed_count=0
    
    for font_file in "$extract_dir"/*.ttf "$extract_dir"/*.otf; do
        if [[ -f "$font_file" ]]; then
            local font_name
            font_name=$(basename "$font_file")
            
            echo "Installing: $font_name"
            
            if validate_font "$font_file" >/dev/null 2>&1; then
                case "$deployment_scope" in
                    "system")
                        sudo cp "$font_file" /Library/Fonts/
                        ;;
                    "user")
                        cp "$font_file" ~/Library/Fonts/
                        ;;
                    "template")
                        sudo cp "$font_file" "/Library/User Template/Non_localized/Library/Fonts/"
                        ;;
                esac
                
                if [[ $? -eq 0 ]]; then
                    echo "  ✅ $font_name installed"
                    ((installed_count++))
                else
                    echo "  ❌ $font_name installation failed"
                    ((failed_count++))
                fi
            else
                echo "  ⚠️  $font_name validation failed - skipped"
                ((failed_count++))
            fi
        fi
    done
    
    # Cleanup
    rm -rf "$extract_dir" "$package_file"
    
    # Refresh font cache
    echo "Refreshing font cache..."
    refresh_font_cache
    
    echo ""
    echo "📊 Deployment Summary:"
    echo "  Successfully installed: $installed_count fonts"
    echo "  Failed installations: $failed_count fonts"
    echo "  Deployment scope: $deployment_scope"
    
    if [[ $failed_count -eq 0 ]]; then
        return 0
    else
        return 1
    fi
}

# Example usage
# deploy_corporate_fonts "https://fonts.company.com/corporate-package.zip" "system"

Security and Compliance Functions

Font License Validation

#!/bin/bash

# Validate font licenses and compliance
validate_font_licenses() {
    echo "🔍 Font License Validation"
    echo "=========================="
    echo ""
    
    local compliance_issues=()
    
    # Check repository fonts
    if [[ -d "$FONT_REPO_DIR" ]]; then
        echo "Repository Font License Status:"
        echo "------------------------------"
        
        for font_file in "$FONT_REPO_DIR"/*.ttf "$FONT_REPO_DIR"/*.otf; do
            if [[ -f "$font_file" ]]; then
                local font_name
                font_name=$(basename "$font_file")
                local meta_file="$FONT_REPO_DIR/${font_name}.meta"
                
                if [[ -f "$meta_file" ]]; then
                    local license_info
                    license_info=$(grep "license_info" "$meta_file" | cut -d'"' -f4)
                    
                    if [[ -n "$license_info" && "$license_info" != "null" ]]; then
                        echo "  ✅ $font_name: $license_info"
                    else
                        echo "  ⚠️  $font_name: No license information"
                        compliance_issues+=("$font_name: Missing license information")
                    fi
                else
                    echo "  ❌ $font_name: No metadata file"
                    compliance_issues+=("$font_name: Missing metadata")
                fi
            fi
        done
    fi
    
    echo ""
    
    # Check for commonly problematic fonts
    echo "Problematic Font Detection:"
    echo "--------------------------"
    
    local problematic_fonts=(
        "Arial.ttf"
        "TimesNewRoman.ttf"
        "Helvetica.otf"
        "Calibri.ttf"
    )
    
    for problem_font in "${problematic_fonts[@]}"; do
        if [[ -f "/Library/Fonts/$problem_font" ]] || [[ -f "~/Library/Fonts/$problem_font" ]]; then
            echo "  ⚠️  Found potentially licensed font: $problem_font"
            compliance_issues+=("$problem_font: Requires license verification")
        fi
    done
    
    echo ""
    
    # Generate compliance report
    if [[ ${#compliance_issues[@]} -eq 0 ]]; then
        echo "✅ Font license compliance check passed"
        return 0
    else
        echo "❌ Font license compliance issues found:"
        for issue in "${compliance_issues[@]}"; do
            echo "  - $issue"
        done
        return 1
    fi
}

validate_font_licenses

Font Usage Analytics

#!/bin/bash

# Generate font usage analytics
analyze_font_usage() {
    echo "📊 Font Usage Analytics"
    echo "======================"
    echo ""
    
    # Font count by format
    echo "Font Format Distribution:"
    echo "------------------------"
    local ttf_count otf_count woff_count other_count
    
    ttf_count=$(find /Library/Fonts ~/Library/Fonts -name "*.ttf" 2>/dev/null | wc -l | tr -d ' ')
    otf_count=$(find /Library/Fonts ~/Library/Fonts -name "*.otf" 2>/dev/null | wc -l | tr -d ' ')
    woff_count=$(find /Library/Fonts ~/Library/Fonts -name "*.woff*" 2>/dev/null | wc -l | tr -d ' ')
    other_count=$(find /Library/Fonts ~/Library/Fonts -type f ! -name "*.ttf" ! -name "*.otf" ! -name "*.woff*" 2>/dev/null | wc -l | tr -d ' ')
    
    echo "  TrueType (.ttf): $ttf_count"
    echo "  OpenType (.otf): $otf_count" 
    echo "  Web Fonts (.woff): $woff_count"
    echo "  Other formats: $other_count"
    echo ""
    
    # Font size analysis
    echo "Font Size Analysis:"
    echo "-------------------"
    local total_size large_fonts
    total_size=0
    large_fonts=0
    
    while IFS= read -r font_file; do
        if [[ -f "$font_file" ]]; then
            local size
            size=$(stat -f%z "$font_file" 2>/dev/null || stat -c%s "$font_file" 2>/dev/null || echo "0")
            total_size=$((total_size + size))
            
            if [[ "$size" -gt 1048576 ]]; then  # 1MB
                ((large_fonts++))
            fi
        fi
    done < <(find /Library/Fonts ~/Library/Fonts -type f 2>/dev/null)
    
    echo "  Total font storage: $(( total_size / 1024 / 1024 )) MB"
    echo "  Large fonts (>1MB): $large_fonts"
    echo ""
    
    # System impact assessment
    echo "System Impact Assessment:"
    echo "------------------------"
    local total_fonts
    total_fonts=$(find /Library/Fonts ~/Library/Fonts -type f 2>/dev/null | wc -l | tr -d ' ')
    
    if [[ "$total_fonts" -gt 200 ]]; then
        echo "  ⚠️  High font count ($total_fonts) - may impact system performance"
    elif [[ "$total_fonts" -gt 100 ]]; then
        echo "  ⚠️  Moderate font count ($total_fonts) - monitor performance"
    else
        echo "  ✅ Reasonable font count ($total_fonts)"
    fi
    
    if [[ "$total_size" -gt 104857600 ]]; then  # 100MB
        echo "  ⚠️  High font storage usage - consider cleanup"
    else
        echo "  ✅ Reasonable font storage usage"
    fi
}

analyze_font_usage

Important Technical Notes

Font Installation Locations

  • /Library/Fonts/: System-wide fonts (all users, requires admin)
  • ~/Library/Fonts/: User-specific fonts (current user only)
  • /Library/User Template/Non_localized/Library/Fonts/: New user template

Font Formats Supported

  • TTF (TrueType): Standard format, good compatibility
  • OTF (OpenType): Advanced format with better typography features
  • WOFF/WOFF2: Web fonts (limited desktop support)
  • Legacy formats: Type 1, bitmap fonts (deprecated)

Security Considerations

  1. Full Disk Access: Required for system-wide font installation
  2. Font Validation: Always validate fonts before installation
  3. License Compliance: Ensure proper licensing for commercial fonts
  4. Source Verification: Only install fonts from trusted sources

Best Practices

  1. Standardization: Maintain consistent font libraries across fleet
  2. License Management: Track and validate all font licenses
  3. Performance Impact: Monitor system performance with large font libraries
  4. Version Control: Maintain font versions and update procedures
  5. Backup Strategy: Regular backups before font changes
  6. User Training: Educate users about proper font usage
  7. Compliance Auditing: Regular license and usage audits
  8. Fleet Deployment: Test font deployments on small groups first

Remember to validate all scripts on test devices before deploying across your Roundfleet environment, and ensure compliance with font licensing agreements when implementing enterprise font management systems.