#!/bin/sh

set -eu

list_swap_partitions() {
  lsblk --properties-by blkid -n -o PATH -Q 'FSTYPE == "swap"'
}

if [ "$(puavo-conf puavo.swap.local.enabled)" != 'true' ]; then
  exit 0
fi

status=0

# Enable local swap partition if found on local disk.
# XXX we should also make sure swap is encrypted
for swap_partition in $(list_swap_partitions); do
  if ! swapon "$swap_partition"; then
    echo "error turning swap on device ${swap_partition}" >&2
    status=1
  fi
done

exit $status
