#!/bin/bash
set -euo pipefail

########################################
# Setup Mount System
# --------------------------------------
# - Copies mount scripts and config
# - Installs required packages
# - Installs systemd service/timer
# - Ensures smb_user exists and is in fs_data group
# - Reloads systemd and enables timer
########################################

if [[ "$EUID" -ne 0 ]]; then
  echo "❌ This script must be run as root. Please use sudo."
  exit 1
fi

REMOTE_HOST="ket.inxs.me"
REMOTE_USER="dbi"
REMOTE_PATH="/myshares"
LOCAL_SCRIPT_DIR="/opt/scripts_local"
SYSTEMD_REMOTE_DIR="$REMOTE_PATH/shared/etc/systemd/system"
SYSTEMD_LOCAL_DIR="/etc/systemd/system"
UPDATED=0

MOUNT_SCRIPT="mount_delayed.sh"
MOUNT_CONF="mount_delayed_table.conf"
SERVICE_FILE="mount_delayed.service"
TIMER_FILE="mount_delayed.timer"
SMB_USER="smb_user"
REQUIRED_GROUP="fs_data"


install_if_missing() {
  local pkg="$1"
  if ! dpkg -s "$pkg" &>/dev/null; then
    if [[ "$pkg" == "apprise" ]]; then
      echo "📦 'apprise' not found via apt — trying pip install"
      if ! command -v pip3 &>/dev/null; then
        echo "Installing pip..."
        apt-get install -y python3-pip
      fi
      pip3 install --quiet apprise
      return
    fi

    if [[ "$UPDATED" -eq 0 ]]; then
      echo "Running apt-get update..."
      apt-get update
      UPDATED=1
    fi

    echo "Installing: $pkg"
    apt-get install -y "$pkg"
  else
    echo "✓ Package '$pkg' is already installed"
  fi
}

for pkg in rsync; do
  install_if_missing "$pkg"
done

# Ensure script dir exists
mkdir -p "$LOCAL_SCRIPT_DIR"

# Copy .smb_credentials file
echo "✅ Copying .smb_credentials from $REMOTE_HOST using rsync..."
rsync -avz "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH/shared/etc/.smb_credentials" "/etc/"
chmod 600 "/etc/.smb_credentials"
chown root:root "/etc/.smb_credentials"

# Copy mount scripts and config using rsync
echo "✅ Copying mount script and config from $REMOTE_HOST using rsync..."
rsync -avz "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH/scripts/setup/$MOUNT_SCRIPT" "$LOCAL_SCRIPT_DIR/"
chmod +x "$LOCAL_SCRIPT_DIR/$MOUNT_SCRIPT"

rsync -avz "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH/scripts/setup/$MOUNT_CONF" "$LOCAL_SCRIPT_DIR/"

# Install required packages
echo "⚙ Installing required packages..."
apt-get update
apt-get install -y bindfs jq cifs-utils tailscale virt-what

# Ensure fs_data group exists
if ! getent group "$REQUIRED_GROUP" > /dev/null; then
  echo "➕ Creating missing group: $REQUIRED_GROUP"
  groupadd "$REQUIRED_GROUP"
else
  echo "✅ Group $REQUIRED_GROUP already exists"
fi

# Ensure smb_user exists and is in fs_data group
if ! id "$SMB_USER" &>/dev/null; then
  echo "➕ Creating missing user: $SMB_USER"
  useradd -r -s /usr/sbin/nologin -G "$REQUIRED_GROUP" "$SMB_USER"
else
  echo "✅ User $SMB_USER already exists"
  usermod -aG "$REQUIRED_GROUP" "$SMB_USER"
fi

# Copy systemd unit files using rsync
echo "✅ Installing systemd unit files from $REMOTE_HOST using rsync..."
rsync -avz "$REMOTE_USER@$REMOTE_HOST:$SYSTEMD_REMOTE_DIR/$SERVICE_FILE" "$SYSTEMD_LOCAL_DIR/"
rsync -avz "$REMOTE_USER@$REMOTE_HOST:$SYSTEMD_REMOTE_DIR/$TIMER_FILE" "$SYSTEMD_LOCAL_DIR/"

# Reload and enable systemd services
echo "♻ Reloading systemd daemon and enabling timer..."
systemctl daemon-reexec
systemctl daemon-reload
systemctl enable --now "$TIMER_FILE"

echo "✔ Mount system setup complete."
