#!/bin/bash

# Prompt the user for the filesystem type
echo "Select the filesystem type:"
echo "1. NTFS"
echo "2. EXT4"
read -p "Enter your choice (1 or 2): " FS_CHOICE

# Set filesystem type and default mount options based on user choice
case $FS_CHOICE in
    1)
        FS_TYPE="ntfs"
        MOUNT_OPTIONS="defaults"
        ;;
    2)
        FS_TYPE="ext4"
        MOUNT_OPTIONS="defaults"
        ;;
    *)
        echo "Invalid choice. Exiting."
        exit 1
        ;;
esac

# Prompt the user for the UUID
read -p "Enter the UUID: " UUID

# Prompt the user for the mount point
read -p "Enter the mount point (e.g., /mnt/data): " MOUNT_POINT

# Check if the mount point directory exists, and if not, create it
if [ ! -d "$MOUNT_POINT" ]; then
    echo "Creating mount point directory..."
    sudo mkdir -p "$MOUNT_POINT"
    if [ $? -ne 0 ]; then
        echo "Failed to create mount point directory. Please check your permissions."
        exit 1
    fi
fi

# Check if the UUID is already in /etc/fstab
if grep -q "$UUID" /etc/fstab; then
    echo "UUID $UUID is already in /etc/fstab. Skipping."
else
    # Add the volume to /etc/fstab
    echo "UUID=$UUID $MOUNT_POINT $FS_TYPE $MOUNT_OPTIONS 0 0" | sudo tee -a /etc/fstab > /dev/null
    if [ $? -eq 0 ]; then
        echo "Added UUID $UUID to /etc/fstab for a $FS_TYPE volume."
    else
        echo "Failed to add UUID to /etc/fstab. Please check your permissions."
        exit 1
    fi
fi

# Mount the volume
echo "Attempting to mount the $FS_TYPE volume..."
sudo mount -a
if [ $? -ne 0 ]; then
    echo "Failed to mount. Please check the UUID and your permissions."
    exit 1
fi

# Check if the mount was successful
if mount | grep -q "$MOUNT_POINT"; then
    echo "$FS_TYPE volume with UUID $UUID has been successfully mounted to $MOUNT_POINT."
else
    echo "Failed to mount $FS_TYPE volume with UUID $UUID to $MOUNT_POINT."
    exit 1
fi
