#!/usr/bin/env nix-shell
#!nix-shell -i bash -p bash -p coreutils -p systemdMinimal

umask 027

if ! systemctl is-active private-storage.target > /dev/null; then
  echo "unable to access encrypted data store."
  echo "unlock it now or cancel with Ctrl+C."
fi

while ! systemctl is-active private-storage.target > /dev/null; do
  # see: <https://stackoverflow.com/a/2654096>
  IFS= read -s -r -p "password: " line
  echo
  if [[ "$?" -eq 0 && -n "$line" ]]; then
    echo -n "$line" > /run/gocryptfs/private.key
    # give time for the store to be unlocked
    timeout 3s systemctl start private-storage.target
  fi
done

