#!/bin/bash

# Example:
#    sh mount_disk.sh -d nvme3n1 -p /data

##################
# Init Parameter #
##################
# Get input parameter
while getopts ':d:p:' opt; do
    case "${opt}" in
        d)
          DISK=${OPTARG}
          ;;
        p)
          DIR=${OPTARG}
          ;;
        *)
          echo "[ERROR] Unknown option ${opt}."
          exit 1
          ;;
    esac
done

# Get partition name
if [ "${DISK:0:4}" = "nvme" ]; then
    PARTITION_NAME="${DISK}p1"
else
    PARTITION_NAME="${DISK}1"
fi

# Parted
echo -e "\n[INFO] Parted(/dev/${DISK})."
parted /dev/${DISK} mklabel gpt
parted /dev/${DISK} print
echo "Ignore" | parted /dev/${DISK} "mkpart primary 0% 100%"

# Format
echo -e "\n[INFO] Format."
sleep 2s
mkfs -t ext4 /dev/${PARTITION_NAME}

# Mount
if [ -n "${DIR}" ]; then
    echo -e "[INFO] Mount."
    mkdir -pv ${DIR}
    mount /dev/${PARTITION_NAME} ${DIR}
    df -TH|grep "/dev/${PARTITION_NAME}"
fi
