#!/bin/bash

if [ -z "$STRATIS_ROOTFS_UUID" ]; then
	echo STRATIS_ROOTFS_UUID is a required environment variable. >&2
	exit 1
fi

i=0
while ! stratis-min pool is-locked "$STRATIS_ROOTFS_UUID" >/dev/null; do
	echo Waiting on pool with UUID $STRATIS_ROOTFS_UUID...
	sleep 1
	if [ "$i" = 5 ]; then
		break
	fi
	i=$(($i + 1))
done

if $(stratis-min pool is-locked "$STRATIS_ROOTFS_UUID"); then
	if ! plymouth ask-for-password \
		--command="stratis-min pool unlock --prompt keyring $STRATIS_ROOTFS_UUID" \
		--prompt="Enter password for Stratis pool with UUID $STRATIS_ROOTFS_UUID containing root filesystem" \
		--number-of-tries=3
	then
		echo Failed to unlock pool with UUID $STRATIS_ROOTFS_UUID using a passphrase >&2
		exit 1
	fi
fi
