#!/bin/bash
#
# Diagnose contentstore mount issues
#

echo "=========================================="
echo "Contentstore Mount Diagnostics"
echo "=========================================="
echo ""

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

# 1. Check if mount point exists
echo "1. Mount Point Directory:"
if [ -d "$MOUNT_POINT" ]; then
    echo "   ✅ Directory exists: $MOUNT_POINT"
    ls -ld "$MOUNT_POINT"
else
    echo "   ❌ Directory missing: $MOUNT_POINT"
    echo "   Fix: sudo mkdir -p $MOUNT_POINT"
fi
echo ""

# 2. Check credentials file
echo "2. Credentials File:"
if [ -f "$CREDS_FILE" ]; then
    echo "   ✅ File exists: $CREDS_FILE"
    echo "   Permissions: $(stat -c '%a %U:%G' "$CREDS_FILE")"
    if [ "$(stat -c '%a' "$CREDS_FILE")" != "600" ]; then
        echo "   ⚠️  Should be 600, current: $(stat -c '%a' "$CREDS_FILE")"
        echo "   Fix: sudo chmod 600 $CREDS_FILE"
    fi
else
    echo "   ❌ File missing: $CREDS_FILE"
    echo "   Fix: Create credentials file with username and password"
fi
echo ""

# 3. Check network connectivity
echo "3. Network Connectivity:"
if ping -c 1 -W 2 10.10.10.5 &>/dev/null; then
    echo "   ✅ Can reach 10.10.10.5"
else
    echo "   ❌ Cannot reach 10.10.10.5"
    echo "   Check network connection"
fi
echo ""

# 4. Check current mount status
echo "4. Current Mount Status:"
if mountpoint -q "$MOUNT_POINT" 2>/dev/null; then
    echo "   ✅ Currently mounted"
    df -h "$MOUNT_POINT" | tail -1
    echo ""
    echo "   Mount details:"
    mount | grep aumentum_contentstore
else
    echo "   ❌ Not currently mounted"
fi
echo ""

# 5. Check fstab
echo "5. fstab Configuration:"
if grep -q "aumentum_contentstore" /etc/fstab 2>/dev/null; then
    echo "   ✅ Entry found in /etc/fstab:"
    grep "aumentum_contentstore" /etc/fstab | sed 's/^/      /'
else
    echo "   ⚠️  No entry in /etc/fstab"
fi
echo ""

# 6. Check systemd mount unit
echo "6. Systemd Mount Unit:"
MOUNT_UNIT="mnt-aumentum_contentstore.mount"
if systemctl list-unit-files | grep -q "$MOUNT_UNIT"; then
    echo "   ✅ Unit exists: $MOUNT_UNIT"
    if systemctl is-enabled "$MOUNT_UNIT" &>/dev/null; then
        echo "   ✅ Unit is enabled"
    else
        echo "   ⚠️  Unit is NOT enabled"
        echo "   Fix: sudo systemctl enable $MOUNT_UNIT"
    fi
    echo "   Status:"
    systemctl status "$MOUNT_UNIT" --no-pager -l | head -10 | sed 's/^/      /'
else
    echo "   ⚠️  Unit not found: $MOUNT_UNIT"
fi
echo ""

# 7. Check fallback service
echo "7. Fallback Mount Service:"
FALLBACK_SERVICE="ensure-contentstore-mount.service"
if systemctl list-unit-files | grep -q "$FALLBACK_SERVICE"; then
    echo "   ✅ Service exists: $FALLBACK_SERVICE"
    if systemctl is-enabled "$FALLBACK_SERVICE" &>/dev/null; then
        echo "   ✅ Service is enabled"
    else
        echo "   ⚠️  Service is NOT enabled"
        echo "   Fix: sudo systemctl enable $FALLBACK_SERVICE"
    fi
    echo "   Status:"
    systemctl status "$FALLBACK_SERVICE" --no-pager -l | head -10 | sed 's/^/      /'
else
    echo "   ⚠️  Service not found: $FALLBACK_SERVICE"
fi
echo ""

# 8. Check API service dependencies
echo "8. API Service Dependencies:"
if systemctl list-unit-files | grep -q "plagis-aumentum-api.service"; then
    echo "   Dependencies:"
    systemctl list-dependencies plagis-aumentum-api.service --no-pager | grep -E "(mount|contentstore)" | sed 's/^/      /' || echo "      (none found)"
else
    echo "   ⚠️  API service not found"
fi
echo ""

# 9. Test manual mount
echo "9. Manual Mount Test:"
echo "   Attempting manual mount (will fail if already mounted)..."
if ! mountpoint -q "$MOUNT_POINT" 2>/dev/null; then
    if sudo mount -t cifs "$SHARE" "$MOUNT_POINT" \
        -o credentials="$CREDS_FILE",uid=1000,gid=1000,file_mode=0755,dir_mode=0755,_netdev,soft,timeo=30,retrans=3 2>&1; then
        echo "   ✅ Manual mount successful"
        sudo umount "$MOUNT_POINT" 2>/dev/null
    else
        echo "   ❌ Manual mount failed"
        echo "   Check credentials and network connectivity"
    fi
else
    echo "   ℹ️  Already mounted, skipping test"
fi
echo ""

# 10. Summary
echo "=========================================="
echo "Summary & Recommendations"
echo "=========================================="
echo ""

ISSUES=0

[ ! -d "$MOUNT_POINT" ] && ISSUES=$((ISSUES+1))
[ ! -f "$CREDS_FILE" ] && ISSUES=$((ISSUES+1))
! mountpoint -q "$MOUNT_POINT" 2>/dev/null && ISSUES=$((ISSUES+1))
! systemctl is-enabled mnt-aumentum_contentstore.mount &>/dev/null && ! systemctl is-enabled ensure-contentstore-mount.service &>/dev/null && ISSUES=$((ISSUES+1))

if [ $ISSUES -eq 0 ]; then
    echo "✅ All checks passed!"
    echo ""
    echo "If mount still doesn't work on boot, check:"
    echo "  1. System logs: sudo journalctl -u mnt-aumentum_contentstore.mount -u ensure-contentstore-mount.service"
    echo "  2. Boot logs: sudo journalctl -b | grep -i cifs"
    echo "  3. Network timing: The mount may be trying before network is fully ready"
else
    echo "⚠️  Found $ISSUES issue(s) that need attention"
    echo ""
    echo "Run setup script to fix:"
    echo "  sudo bash scripts/setup_mount.sh"
fi
echo ""

