#!/bin/bash

# Get the number of physical disks (1,2,3,etc)
physical_disk_num=`lsblk --output NAME,TYPE |grep disk |wc -l`

if [ $physical_disk_num -gt 1 ]
then
    echo "There is an extra disk. The extra disk will be formatted."

    # Get the partition where the system disk is located (/dev/sda2)
    system_disk_partition=`findmnt -n -o SOURCE --target /`

    # Get a list of physical disks  (physical_disk=["sda","sdb","sdc"])
    readarray -t physical_disk <<< `lsblk --output NAME,TYPE |grep disk|awk '{print $1}'`

    # Get a disk other than the system disk (extra_disk=["sdb","sdc"])
    extra_disk=()
    for item in "${physical_disk[@]}"; do
        res=$(echo $system_disk_partition | grep "${item}")
        if [[ "$res" == "" ]];then
            extra_disk+=("$item")
        fi
    done

    # Get additional disk block devices (extra_block_device=["/dev/sdb","/dev/sdc"])
    extra_block_device=()
    for device in "${extra_disk[@]}"; do
        block_device="/dev/$device"
        extra_block_device+=("$block_device")
    done

    # Get no mounts to the data directory to the block device (no_mounted_block_device="/dev/sdc")
    no_mounted_block_device=()
    for block_device in "${extra_block_device[@]}"; do
        if  mount |grep $block_device ; then
            echo "$block_device has been mounted"
        else
            no_mounted_block_device+=("$block_device")
        fi
    done

    # Formatting additional data disks
    echo "The disk on which the data will be formatted is: ${no_mounted_block_device[@]}"

    # exit 0
    
    # Formatting disks one by one
    for format_disk in ${no_mounted_block_device[@]}; do
        echo "Will start formatting the disk: $format_disk"
        parted --script $format_disk mklabel msdos
        echo "y" |mkfs -t ext4 $format_disk
    done
else
    echo "There is only one system disk. No operation is required."
fi

