#!/bin/bash
#
# Mount contentstore with proper error handling
#

set -e

MOUNT_POINT="/mnt/aumentum_contentstore"
SHARE="//10.10.10.5/LRS_STORAGE"
CREDS_FILE="/root/.smb_credentials_aumentum"

echo "=========================================="
echo "Mounting Contentstore"
echo "=========================================="
echo ""

# Check if running as root
if [ "$EUID" -ne 0 ]; then 
    echo "Please run as root (use sudo)"
    exit 1
fi

# Create mount point
mkdir -p "$MOUNT_POINT"

# Get user/group IDs
APP_USER="plagis"
if id "$APP_USER" &>/dev/null; then
    USER_ID=$(id -u "$APP_USER")
    GROUP_ID=$(id -g "$APP_USER")
else
    USER_ID=1000
    GROUP_ID=1000
fi

echo "Mount point: $MOUNT_POINT"
echo "Share: $SHARE"
echo "User ID: $USER_ID"
echo "Group ID: $GROUP_ID"
echo ""

# Check if already mounted
if mountpoint -q "$MOUNT_POINT"; then
    echo "⚠ Already mounted at $MOUNT_POINT"
    echo "Unmounting first..."
    umount "$MOUNT_POINT" || true
fi

# Check if credentials file exists
if [ ! -f "$CREDS_FILE" ]; then
    echo "❌ Credentials file not found: $CREDS_FILE"
    echo ""
    echo "Please create it first:"
    echo "  sudo nano $CREDS_FILE"
    echo ""
    echo "Add these lines (replace YOURPASS with actual password):"
    echo "  username=Administrator"
    echo "  password=YOURPASS"
    echo "  domain="
    echo ""
    echo "Then set permissions:"
    echo "  sudo chmod 600 $CREDS_FILE"
    exit 1
fi

# Test network connectivity
echo "Testing network connectivity..."
if ! ping -c 1 -W 2 10.10.10.5 &> /dev/null; then
    echo "❌ Cannot ping 10.10.10.5 - check network connectivity"
    exit 1
fi
echo "✅ Network connectivity OK"

# Try different mount methods
echo ""
echo "Attempting to mount..."

# Method 1: With credentials file and vers=3.0
echo "Trying method 1: credentials file with SMB 3.0..."
if mount -t cifs "$SHARE" "$MOUNT_POINT" \
    -o credentials="$CREDS_FILE",uid=$USER_ID,gid=$GROUP_ID,file_mode=0755,dir_mode=0755 2>&1; then
    echo "✅ Mount successful with SMB 3.0!"
    df -h | grep aumentum
    exit 0
fi

# Method 2: With credentials file and vers=2.1
echo ""
echo "Trying method 2: credentials file with SMB 2.1..."
if mount -t cifs "$SHARE" "$MOUNT_POINT" \
    -o credentials="$CREDS_FILE",uid=$USER_ID,gid=$GROUP_ID,file_mode=0755,dir_mode=0755,vers=2.1 2>&1; then
    echo "✅ Mount successful with SMB 2.1!"
    df -h | grep aumentum
    exit 0
fi

# Method 3: With credentials file and vers=2.0
echo ""
echo "Trying method 3: credentials file with SMB 2.0..."
if mount -t cifs "$SHARE" "$MOUNT_POINT" \
    -o credentials="$CREDS_FILE",uid=$USER_ID,gid=$GROUP_ID,file_mode=0755,dir_mode=0755,vers=2.0 2>&1; then
    echo "✅ Mount successful with SMB 2.0!"
    df -h | grep aumentum
    exit 0
fi

# If all methods failed
echo ""
echo "❌ All mount methods failed"
echo ""
echo "Troubleshooting steps:"
echo ""
echo "1. Check credentials file:"
echo "   sudo cat $CREDS_FILE"
echo ""
echo "2. Test SMB connection:"
echo "   smbclient -L //10.10.10.5 -U Administrator"
echo ""
echo "3. Check kernel messages:"
echo "   dmesg | tail -20"
echo ""
echo "4. Try manual mount with password prompt:"
echo "   sudo mount -t cifs $SHARE $MOUNT_POINT -o credentials=$CREDS_FILE,uid=$USER_ID,gid=$GROUP_ID,file_mode=0755,dir_mode=0755"
echo ""

exit 1

