#!/bin/sh
set -e

if [ -z "$1" ]; then
  echo "Usage: $0 <volume_filename>"
  exit 1
fi

FILE="/data/$1"
MAPPER_NAME="encrypted_volume"
MOUNT_POINT="/mnt/${MAPPER_NAME}"


cryptsetup close "$MAPPER_NAME" || true
cryptsetup open "$FILE" "$MAPPER_NAME"

mkdir -p "$MOUNT_POINT"

if ! mountpoint -q "$MOUNT_POINT"; then
  mount /dev/mapper/"$MAPPER_NAME" "$MOUNT_POINT"
  echo "Mounted /dev/mapper/$MAPPER_NAME at $MOUNT_POINT"
else
  echo "$MOUNT_POINT is already mounted."
fi

