#!/bin/bash

# Prompt for the username whose Orca configuration should be copied
read -p "Enter the username to copy Orca settings from: " USER

# Define locations
USER_ORCA_CONFIG="/home/$USER/.local/share/orca"
TEMP_ORCA_CONFIG="/tmp/orca-config"
GDM_ORCA_CONFIG="/var/lib/gdm3/.local/share/orca"

# Check if the source configuration exists
if [ ! -d "$USER_ORCA_CONFIG" ]; then
    echo "The user's Orca configuration directory does not exist: $USER_ORCA_CONFIG"
    exit 1
fi

# Create a temporary directory to avoid direct copying
mkdir -p "$TEMP_ORCA_CONFIG"

# Copy configuration files to temporary location
echo "Copying user's Orca configuration to temporary location..."
cp -r "$USER_ORCA_CONFIG/"* "$TEMP_ORCA_CONFIG/"

# Create directory structure for GDM if it doesn't exist
echo "Creating Orca configuration directory for GDM..."
sudo mkdir -p "$GDM_ORCA_CONFIG"

# Copy from temporary location to GDM's configuration directory
echo "Copying Orca configuration to GDM..."
sudo cp -r "$TEMP_ORCA_CONFIG/"* "$GDM_ORCA_CONFIG/"

# Change ownership and permissions
echo "Setting ownership and permissions..."
sudo chown -R gdm:gdm "$GDM_ORCA_CONFIG"
sudo chmod -R 700 "$GDM_ORCA_CONFIG"

# Cleanup temporary files
echo "Cleaning up temporary files..."
rm -rf "$TEMP_ORCA_CONFIG"

echo "Complete."