# set-executionpolicy unrestricted

# Check if running as administrator
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    Write-Host "This script requires administrator privileges. Attempting to restart as administrator..." -ForegroundColor Yellow
    
    # Get the current script path
    $scriptPath = $MyInvocation.MyCommand.Path
    
    # Restart the script with administrator privileges
    try {
        Start-Process PowerShell -Verb RunAs -ArgumentList "-ExecutionPolicy Bypass -File `"$scriptPath`""
        exit
    }
    catch {
        Write-Error "Failed to restart as administrator. Please run this script as administrator manually."
        Write-Host "Right-click on PowerShell and select 'Run as administrator', then run this script again." -ForegroundColor Red
        pause
        exit 1
    }
}

Write-Host "Running with administrator privileges." -ForegroundColor Green

$uid = $Env:UserName
# Get the directory where this script is located
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path

# Copy SSH directory from Nextcloud synchronized location if it exists
$sshSourceDir = "C:\Users\$uid\Nextcloud\Documents\Important_Docs\.ssh"
$sshDestDir = "C:\Users\$uid\.ssh"
if (Test-Path -Path $sshSourceDir) {
    # Ensure the destination .ssh directory exists
    if (-not (Test-Path -Path $sshDestDir)) {
        New-Item -Path $sshDestDir -ItemType Directory -Force | Out-Null
        Write-Host "Created .ssh directory at: $sshDestDir"
    }
    
    # Copy the contents of the .ssh directory (not the directory itself)
    Copy-Item -Path "$sshSourceDir\*" -Destination $sshDestDir -Recurse -Force
    Write-Host "Copied SSH files from Nextcloud to: $sshDestDir"
} else {
    Write-Host "Important_Docs/.ssh directory not found at: $sshSourceDir. Skipping SSH key copy." -ForegroundColor Yellow
}

python "$scriptDir\Python\NextcloudClientFix.py"

# Nextcloud - Copy sync-exclude.lst to AppData
Write-Host "Configuring Nextcloud sync exclusions..."

# Define the source and destination paths for sync-exclude.lst
$syncExcludeSource = "$scriptDir\sync-exclude.lst"
$nextcloudAppDataDir = "$env:APPDATA\Nextcloud"
$syncExcludeDestination = Join-Path -Path $nextcloudAppDataDir -ChildPath "sync-exclude.lst"

# Check if the source file exists
if (Test-Path -Path $syncExcludeSource) {
    # Ensure the Nextcloud AppData directory exists
    if (-not (Test-Path -Path $nextcloudAppDataDir)) {
        try {
            New-Item -Path $nextcloudAppDataDir -ItemType Directory -Force | Out-Null
            Write-Host "Created Nextcloud directory at: $nextcloudAppDataDir"
        } catch {
            Write-Warning "Failed to create Nextcloud directory: $_"
        }
    }

    # Copy the sync-exclude.lst file
    try {
        Copy-Item -Path $syncExcludeSource -Destination $syncExcludeDestination -Force
        Write-Host "Copied sync-exclude.lst to $syncExcludeDestination"
    } catch {
        Write-Warning "Failed to copy sync-exclude.lst: $_"
    }
} else {
    Write-Warning "sync-exclude.lst not found at: $syncExcludeSource"
}