#!/bin/bash

# Description: This script automates the mounting of encrypted Btrfs volumes using short disk serials.
# It is specifically designed for setting up disks in a RAID1c3 configuration, making it ideal
# for systems requiring high availability and data redundancy. The script uses 'cryptsetup' to
# handle LUKS encryption, opening multiple disks by their serials and mounting one of them
# to a designated mount point. This setup is particularly useful in managing RAID arrays
# where disk identification by serial is crucial for precise operations.

# Usage:
# This script does not take command line arguments. Edit the script to set the mount directory
# and disk IDs as needed before running.

# Define the mount directory and array of disk serials

mountdirectoryOne="/mnt/external-hdd-storage/20tb-1-raid1c3"
disk_serial=(
"ata-TOSHIBA_MG10ACA20TE_MOOOOOO"
"ata-TOSHIBA_MG10ACA20TE_MOOOOOO"
"ata-TOSHIBA_MG10ACA20TE_MOOOOOO"
)

for id in "${disk_serial[@]}"; do
    sudo cryptsetup open --type luks /dev/disk/by-id/"$id" "encrypted_${id##*-}"
done

sudo mount -t \
btrfs -o space_cache=v2,defaults,noatime,compress=zstd /dev/mapper/encrypted_"${disk_serial[1]##*-}"\
 $mountdirectoryOne

cd "$mountdirectoryOne" || exit
