#!/bin/sh

# { Notes
# - Script called by scripts/init-premount/preparing_for_unlock for protocol pre August 2014.

# { Arguments
# .$1
#    luks passphrase
# .$2
#    uuid associated to the passphrase
luks_passphrase="${1}"
related_uuid="${2}"

if test -z "${related_uuid}" ; then
  # no uuid set, just try the passphrase always.
  sleep 2 # initial wait
  while test $(ps | grep -i -c "cryptsetup.*luksopen") -le 1 ; do
    # checking for the process that uses the passphrase. Until it spawns
    sleep 1
  done
else
  sleep 2 # initial wait
  while test $(ps | grep -i -c "cryptsetup.*luksopen.*${related_uuid}" ) -le 1 ; do
    # checking for the process that uses the passphrase with the given uuid. Until it spawns
    sleep 1
  done
fi

# opening as soon the process is ready.
echo -ne "${luks_passphrase}" > /lib/cryptsetup/passfifo