#!/bin/bash

#set -o errexit
#set -o nounset
#set -o pipefail
#set -o xtrace

if [ $# -ne 1 ] ; then
  echo "usage:$0 <path of encrypted lv>"
  exit 10
fi

lv=$1
zkk_crypt_conf=/etc/zkk-crypt/zkk-crypt.conf

if [ ! -f ${zkk_crypt_conf} ] ; then
  echo "${zkk_crypt_conf} missing"
  exit 11
fi

. ${zkk_crypt_conf}

luks=$(grep " $1 " ${zkk_clucrypttab} | awk '{print $1}')
if [ ${#luks} -lt 1 ] ; then
  echo "path to lv $lv not found"
  exit
fi

cryptsetup luksClose ${luks}
if [[ -e /dev/mapper/${luks} ]] ; then
  echo /dev/mapper/${luks} at $i is still online!
  exit 1
fi

lvremove $lv
logger "logical volume ${lv} removed"
if [ $? -eq 0 ] ; then
  sed -i "/^${luks}[[:space:]].*[[:space:]]luks[[:space:]]*$/d" ${zkk_clucrypttab}
  exit 0
fi
exit 1
