#!/bin/bash

#: lmount
#:   open + mount luks encrypted storage
#:
#:   https://unix.stackexchange.com/questions/445652/how-to-mount-and-de-encrypt-a-luks-encrypted-partition-to-recover-files
set -e
set -o pipefail
set -x

device=$1
target=$2


main() {
    if (( $# != 2 )); then
        echo "Must be called with 2 args!"
        echo " $0 $device $target"
        exit 1
    fi

    if [ "$EUID" -ne 0 ]
    then echo "Must be run as root!"
         exit 1
    fi

    luksname=$(basename ${target})

    if [ ! -d "${target}" ]; then
        mkdir -p ${target}
    fi

    echo "Opening ${device} as ${luksname}"
    cryptsetup open ${device} ${luksname}

    echo "  ... mounting to ${target}"
    mount /dev/mapper/${luksname} ${target}

    echo "done..."
}

main $@