#!/bin/bash

# Define your Hetzner Storage Box details
STORAGE_BOX_USER="u554364"
STORAGE_BOX_HOST="u554364.your-storagebox.de"
MOUNT_POINT="/mnt/data"
SSH_KEY_PATH="/root/.ssh/id_ed25519"
PORT="23"

# Install required dependencies
echo "Installing required dependencies..."
apt update
apt install -y sshfs fuse3

# Ensure FUSE is loaded
echo "Loading FUSE kernel module..."
sudo modprobe fuse

# Ensure fuse.conf allows user mounts
echo "Configuring fuse.conf..."
echo "user_allow_other" | sudo tee -a /etc/fuse.conf

# Create a systemd mount unit file
echo "Creating systemd mount unit..."
cat <<EOF | sudo tee /etc/systemd/system/mnt-data.mount
[Unit]
Description=Mount Hetzner Storage Box

[Mount]
What=${STORAGE_BOX_USER}@${STORAGE_BOX_HOST}:/home
Where=${MOUNT_POINT}
Type=fuse.sshfs
Options=port=${PORT},IdentityFile=${SSH_KEY_PATH},allow_other,default_permissions,ServerAliveInterval=15,reconnect,_netdev,StrictHostKeyChecking=no

[Install]
WantedBy=multi-user.target
EOF

# Reload systemd to apply changes
echo "Reloading systemd daemon..."
sudo systemctl daemon-reload

# Enable and start the mount service
echo "Enabling and starting mount service..."
sudo systemctl enable mnt-data.mount
sudo systemctl start mnt-data.mount

# Check the status
echo "Checking mount status..."
sudo systemctl status mnt-data.mount

# Display filesystem information
echo "Displaying filesystem information..."
df -h
