#!/bin/bash
#
# Fix CIFS mount permissions
# This script helps resolve common CIFS mount permission issues
#

set -e

MOUNT_POINT="/mnt/aumentum_contentstore"
SHARE="//10.10.10.5/LRS_STORAGE"

echo "=========================================="
echo "CIFS Mount Permission Fix"
echo "=========================================="
echo ""

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

# Install CIFS utilities if not installed
if ! command -v mount.cifs &> /dev/null; then
    echo "Installing cifs-utils..."
    apt-get install -y cifs-utils
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
    echo "Warning: User $APP_USER not found, using current user"
    USER_ID=$(id -u)
    GROUP_ID=$(id -g)
fi

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

# Option 1: Create credentials file (more secure)
echo "Option 1: Using credentials file (recommended)"
read -p "Enter Windows username [Administrator]: " WIN_USER
WIN_USER=${WIN_USER:-Administrator}
read -sp "Enter Windows password: " WIN_PASS
echo ""

CREDS_FILE="/root/.smb_credentials_aumentum"
cat > "$CREDS_FILE" << EOF
username=$WIN_USER
password=$WIN_PASS
domain=
EOF
chmod 600 "$CREDS_FILE"

echo "Credentials file created: $CREDS_FILE"
echo ""

# Try mounting with credentials file
echo "Attempting to mount with credentials file..."
if mount -t cifs "$SHARE" "$MOUNT_POINT" \
    -o credentials="$CREDS_FILE",uid=$USER_ID,gid=$GROUP_ID,file_mode=0755,dir_mode=0755; then
    echo "✅ Mount successful!"
    echo ""
    echo "To make this permanent, add to /etc/fstab:"
    echo "$SHARE $MOUNT_POINT cifs credentials=$CREDS_FILE,uid=$USER_ID,gid=$GROUP_ID,file_mode=0755,dir_mode=0755 0 0"
    exit 0
fi

# If that failed, try with username/password directly
echo ""
echo "Option 2: Trying direct mount with username/password..."
if mount -t cifs "$SHARE" "$MOUNT_POINT" \
    -o username="$WIN_USER",password="$WIN_PASS",uid=$USER_ID,gid=$GROUP_ID,file_mode=0755,dir_mode=0755; then
    echo "✅ Mount successful!"
    echo ""
    echo "To make this permanent, add to /etc/fstab:"
    echo "$SHARE $MOUNT_POINT cifs username=$WIN_USER,password=$WIN_PASS,uid=$USER_ID,gid=$GROUP_ID,file_mode=0755,dir_mode=0755 0 0"
    exit 0
fi

# If still failing, show troubleshooting info
echo ""
echo "❌ Mount failed. Troubleshooting steps:"
echo ""
echo "1. Check network connectivity:"
echo "   ping 10.10.10.5"
echo ""
echo "2. Check SMB port:"
echo "   telnet 10.10.10.5 445"
echo ""
echo "3. Check kernel messages:"
echo "   dmesg | tail -20"
echo ""
echo "4. Try different SMB version:"
echo "   mount -t cifs $SHARE $MOUNT_POINT -o credentials=$CREDS_FILE,uid=$USER_ID,gid=$GROUP_ID,file_mode=0755,dir_mode=0755"
echo ""
echo "5. Check if share is accessible:"
echo "   smbclient -L //10.10.10.5 -U $WIN_USER"
echo ""

exit 1

