#!/bin/bash
# wget -O /tmp/disk_space_linux.sh https://git.technozone.com.au/vijay/Scripts/raw/branch/main/disk_space_linux && bash /tmp/disk_space_linux.sh && rm -f /tmp/disk_space_linux.sh

# Function to list directories
list_directories() {
    local dir=$1
    echo "Listing directories under: $dir"
    du -h --max-depth=1 "$dir" 2>/dev/null | sort -hr
}

# Function to delete a directory
delete_directory() {
    local dir_to_delete=$1
    echo "Are you sure you want to delete the directory: $dir_to_delete? (y/n)"
    read -r confirmation
    if [[ $confirmation == "y" ]]; then
        rm -rf "$dir_to_delete"
        echo "Deleted: $dir_to_delete"
    else
        echo "Deletion cancelled."
    fi
}

# Function to list largest files in a directory
list_largest_files() {
    local dir=$1
    echo "Listing largest files in: $dir"
    find "$dir" -type f -exec du -h {} + | sort -hr | head -n 10
}

# Function to display the menu
display_menu() {
    echo "Menu Options:"
    echo "1. List directories"
    echo "2. Delete a directory"
    echo "3. Dig into a directory to find largest files"
    echo "4. Quit"
}

# Main function to manage disk cleanup
disk_cleanup() {
    local current_dir="/"

    while true; do
        display_menu
        read -r choice

        case $choice in
            1)
                list_directories "$current_dir"
                ;;
            2)
                echo "Enter the directory path to delete (relative to $current_dir):"
                read -r user_input
                if [[ -d "$current_dir/$user_input" ]]; then
                    delete_directory "$current_dir/$user_input"
                else
                    echo "Invalid directory. Please try again."
                fi
                ;;
            3)
                echo "Enter the directory path to dig into (relative to $current_dir):"
                read -r user_input
                if [[ -d "$current_dir/$user_input" ]]; then
                    list_largest_files "$current_dir/$user_input"
                else
                    echo "Invalid directory. Please try again."
                fi
                ;;
            4)
                echo "Exiting..."
                break
                ;;
            *)
                echo "Invalid option. Please try again."
                ;;
        esac
    done
}

# Start the disk cleanup process
disk_cleanup
