From 610122edc937bfd74c4879cc85df365c9db437aa Mon Sep 17 00:00:00 2001
From: Luka Racic <104823464+racicLuka@users.noreply.github.com>
Date: Thu, 12 Oct 2023 14:22:01 +0200
Subject: [PATCH] Add possibility to provide path as argument and default value
 for fallback (#374)

---
 .../agent/robotmk_collector.ps1               | 25 +++++++++++++++----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/checkmk_extensions/agent/robotmk_collector.ps1 b/checkmk_extensions/agent/robotmk_collector.ps1
index 96915704..01f36f38 100644
--- a/checkmk_extensions/agent/robotmk_collector.ps1
+++ b/checkmk_extensions/agent/robotmk_collector.ps1
@@ -2,10 +2,7 @@ Set-StrictMode -Version 3.0
 $ErrorActionPreference = "Stop"
 
 function Main {
-    Param (
-        [parameter(Mandatory=$true, Position=0)]
-        [string]$ConfigPath
-    )
+    $ConfigPath = DetermineConfigPath -CommandLineArgs $args
     Write-Output "<<<robotmk_v2:sep(10)>>>"
 
     try {
@@ -27,6 +24,24 @@ function Main {
 }
 
 
+function DetermineConfigPath {
+    [OutputType([string])]
+    Param (
+        [parameter(Mandatory=$false, Position=0)]
+        [String[]]$CommandLineArgs
+    )
+
+    if ($CommandLineArgs -ne "" -and $CommandLineArgs.Count -gt 0) {
+        return $CommandLineArgs[0]
+    }
+
+    $configDir = $env:MK_CONFDIR
+    if ($null -eq $configDir) {
+        $configDir = 'C:\ProgramData\checkmk\agent\config'
+    }
+
+    return Join-Path $configDir 'robotmk.json'
+}
 function SerializeConfigReadingError {
     [OutputType([string])]
     Param (
@@ -56,4 +71,4 @@ function GetResultsDirectory {
     return $configData.results_directory -as [string]
 }
 
-Main("${env:MK_CONFDIR}\robotmk.json")
+Main $args