#!/bin/bash

# Define directories to lock
DIRECTORIES=("Documents" "Downloads" "Music" "Pictures" "Videos" "snap" "Public" "Templates")

# Function to lock down and make folders immutable for a given user
lock_and_immute_folders() {
    local user="$1"
    local home_dir="/home/$user"
    
    echo "Processing user: $user"

    # Check if home directory exists
    if [ ! -d "$home_dir" ]; then
        echo "Error: Home directory $home_dir not found for user $user"
        return
    fi
    
    # Loop through each directory and lock it
    for DIR in "${DIRECTORIES[@]}"; do
        local folder="$home_dir/$DIR"
        if [ -d "$folder" ]; then
            sudo chattr +i "$folder"
            if [ $? -eq 0 ]; then
                echo "Locked $folder for user $user"
            else
                echo "Failed to lock $folder for user $user"
            fi
        else
            echo "Directory $folder does not exist for user $user"
        fi
    done
}

# Check if script is run with root privileges
if [ "$(id -u)" != "0" ]; then
    echo "This script must be run with root privileges. Exiting."
    exit 1
fi

# Discover all user directories under /home except for system users (UID >= 1000)
for user_dir in /home/*; do
    user=$(basename "$user_dir")
    if [ "$user" != "lost+found" ] && [ $(id -u "$user") -ge 1000 ]; then
        lock_and_immute_folders "$user"
    fi
done

echo "All specified directories are now locked for all users."
