#!/bin/bash

i=0
required_keys_nb=3
path=${1:-'/var/vault/keys'}
numbers=(1 2 3 4 5)
selected_numbers=(0)
selected_keys=()

select_key() {
  while [[ "$i" -lt "$required_keys_nb" ]]; do
    local random_number=0

    while [[ "${selected_numbers[*]}" =~ "$random_number" ]]; do
        index=$((RANDOM % ${#numbers[@]}))
        random_number=${numbers[$index]}
    done

    selected_numbers+=($random_number)
    numbers=(${numbers[@]/$random_number/})
    selected_keys+=($(sed -n "${random_number}p" $path))

    ((i++))
  done

  echo ${selected_keys[@]}
}

keys=$(select_key)

vault operator unseal "$(echo $keys| awk '{print $1}')"
vault operator unseal "$(echo $keys| awk '{print $2}')"
vault operator unseal "$(echo $keys| awk '{print $3}')"
