trap {
    Write-Host ""
    Write-Host "WakeMouse could not be started." -ForegroundColor Red
    Write-Host $_.Exception.Message -ForegroundColor Red
    Write-Host ""
    Read-Host "Press Enter to close"
    exit 1
}

$ErrorActionPreference = "Stop"

$exeName = "WakeMouse_v1.8.exe"
$scriptDir = $PSScriptRoot

if ([string]::IsNullOrWhiteSpace($scriptDir)) {
    $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
}

if ([string]::IsNullOrWhiteSpace($scriptDir)) {
    $scriptDir = (Get-Location).Path
}

$exePath = Join-Path -Path $scriptDir -ChildPath $exeName

Write-Host "WakeMouse starter"
Write-Host "Folder: $scriptDir"
Write-Host ""

if (-not (Test-Path -LiteralPath $exePath -PathType Leaf)) {
    Write-Host "Not found: $exePath" -ForegroundColor Red
    Write-Host "Please extract the ZIP file first, then save start.txt as start.ps1 in the same folder as $exeName and run it."
    Write-Host ""
    Read-Host "Press Enter to close"
    exit 1
}

try {
    Unblock-File -LiteralPath $PSCommandPath -ErrorAction SilentlyContinue
}
catch {
}

try {
    Unblock-File -LiteralPath $exePath -ErrorAction SilentlyContinue
}
catch {
}

Set-Location -LiteralPath $scriptDir

Write-Host "Starting $exeName..."
Start-Process -FilePath $exePath -WorkingDirectory $scriptDir
