#!/bin/bash
# QEMU SMB Fix - Replaces /usr/bin/smbd with a wrapper
# WARNING: This modifies system files. Use with caution.
#
# Run with sudo: sudo bash install-smbd-wrapper.sh
# To uninstall: sudo bash install-smbd-wrapper.sh --uninstall

set -e

REAL_SMBD="/usr/bin/smbd"
BACKUP_SMBD="/usr/bin/smbd.bin"
WRAPPER_SMBD="/usr/bin/smbd"

UNINSTALL=false

if [ "$1" = "--uninstall" ]; then
    UNINSTALL=true
fi

if [ "$EUID" -ne 0 ]; then
    echo "This script requires sudo to modify system files."
    echo "Please run: sudo $0"
    exit 1
fi

# Check for smbd
if [ ! -x "$REAL_SMBD" ]; then
    echo "Error: smbd not found at $REAL_SMBD"
    exit 1
fi

if [ "$UNINSTALL" = true ]; then
    echo "=== Uninstalling SMBD Wrapper ==="
    if [ -x "$BACKUP_SMBD" ]; then
        mv "$BACKUP_SMBD" "$WRAPPER_SMBD"
        echo "Restored original smbd"
    else
        echo "Backup not found, nothing to restore"
    fi
    exit 0
fi

echo "=== Installing SMBD Wrapper for QEMU Compatibility ==="
echo ""
echo "This will create a wrapper at $WRAPPER_SMBD"
echo "that creates required directories before calling the real smbd"
echo ""

# Create backup if not exists
if [ ! -x "$BACKUP_SMBD" ]; then
    cp "$REAL_SMBD" "$BACKUP_SMBD"
    echo "Created backup at $BACKUP_SMBD"
fi

# Create the wrapper
cat > "$WRAPPER_SMBD" << 'WRAPPER'
#!/bin/bash
# QEMU SMBD Wrapper - Creates required directories for modern Samba
# This wrapper is auto-generated by install-smbd-wrapper.sh

REAL_SMBD="/usr/bin/smbd.bin"

# If called with -s (config file), ensure ncalrpc directory exists
if [ "$1" = "-s" ] && [ -n "$2" ]; then
    SMB_CONF="$2"
    SMB_DIR=$(dirname "$SMB_CONF")

    # Create required directories for Samba 4.x
    if [ -d "$SMB_DIR" ]; then
        mkdir -p "$SMB_DIR/ncalrpc" 2>/dev/null || true
        mkdir -p "$SMB_DIR/cores" 2>/dev/null || true
        chmod 700 "$SMB_DIR/cores" 2>/dev/null || true
        chmod 755 "$SMB_DIR" 2>/dev/null || true
    fi
fi

# Run the real smbd with all arguments
exec "$REAL_SMBD" "$@"
WRAPPER

chmod +x "$WRAPPER_SMBD"

echo "Created wrapper at $WRAPPER_SMBD"
echo ""
echo "=== Testing ==="

# Test the wrapper
TEST_DIR=$(mktemp -d)
mkdir -p "$TEST_DIR/ncalrpc"  # Pre-create so testparm doesn't fail

cat > "$TEST_DIR/smb.conf" << 'EOF'
[global]
private dir=TEST_DIR
interfaces=127.0.0.1
pid directory=TEST_DIR
lock directory=TEST_DIR
state directory=TEST_DIR
cache directory=TEST_DIR
ncalrpc dir=TEST_DIR/ncalrpc
log file=TEST_DIR/log.smbd
smb passwd file=TEST_DIR/smbpasswd
security = user
map to guest = Bad User
[qemu]
path=/tmp
guest ok = yes
EOF

# Replace TEST_DIR with actual path
sed -i "s|TEST_DIR|$TEST_DIR|g" "$TEST_DIR/smb.conf"

# Run wrapper to create directories
"$WRAPPER_SMBD" -s "$TEST_DIR/smb.conf" -F --no-process-group &
sleep 2

# Check if directories were created
if [ -d "$TEST_DIR/ncalrpc" ] && [ -d "$TEST_DIR/cores" ]; then
    echo "✓ Wrapper created required directories"
else
    echo "✗ Wrapper did not create directories"
fi

# Check if smbd is running
if pgrep -f "smbd.bin.*$TEST_DIR" > /dev/null; then
    echo "✓ smbd is running"
    pkill -f "smbd.bin.*$TEST_DIR" 2>/dev/null || true
else
    echo "✗ smbd is not running (may be expected if test failed)"
fi

rm -rf "$TEST_DIR"

echo ""
echo "=== Installation Complete ==="
echo "QEMU SMB should now work with modern Samba (4.x)"
echo ""
echo "To uninstall: sudo bash $0 --uninstall"
