#!/bin/bash

# Set the username
USERNAME="ENTER_USER_NAME"

# Check if the user exists
if id "$USERNAME" &>/dev/null; then
    # Check if the user is in an active session
    if who | grep -wq "$USERNAME"; then
        # Note: Unsaved data may be lost or may result in data corruption
        # Log out the user if in an active session
        pkill -KILL -u "$USERNAME"
        echo "User $USERNAME logged out."
    fi

    # Add the user to the sudo group
    sudo usermod -aG sudo "$USERNAME"
    echo "User $USERNAME added to the sudo group."

    # Detect the user's timezone
    USER_TIMEZONE=$(sudo -u "$USERNAME" timedatectl | grep "Time zone" | awk '{print $3}')

    # Print the current timestamp based on the user's timezone
    echo "Current timestamp in $USER_TIMEZONE: $(sudo -u "$USERNAME" date)"

    # Schedule the removal of sudo access after X minutes using sleep
    (sleep ENTER_SECONDS && sudo gpasswd -d "$USERNAME" sudo && echo "Removed sudo access for $USERNAME." && pkill -KILL -u "$USERNAME") &
    echo "Scheduled removal of sudo access in X minutes, and user will be logged out."

    # Print the current timestamp based on the user's timezone
    echo "Current timestamp in $USER_TIMEZONE: $(sudo -u "$USERNAME" date)"

else
    echo "User $USERNAME not found."
fi

exit 0
