From a990c0fd1b6919a7a97d92342a4046ee485080ff Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Fri, 15 Sep 2017 12:32:59 +0200
Subject: [PATCH] Try to create incoming and managed directory trees if
 missing.

Check on startup if they exist, try to create them otherwise. Remove the
corresponding section from the installer script.

Refers to issue #7.
---
 AutoTx/AutoTx.cs                     | 24 +++++++++++++++++++++---
 AutoTx/Resources/Install-Service.ps1 |  9 ---------
 2 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/AutoTx/AutoTx.cs b/AutoTx/AutoTx.cs
index 6ab83e7..fee6fea 100644
--- a/AutoTx/AutoTx.cs
+++ b/AutoTx/AutoTx.cs
@@ -144,18 +144,23 @@ namespace AutoTx
                              @" and a backslash, e.g. 'D:\'!");
                     configInvalid = true;
                 }
-                // IncomingDirectory
+
+                // spooling directories: IncomingDirectory + ManagedDirectory
                 if (_config.IncomingDirectory.StartsWith(@"\")) {
                     writeLog("ERROR: IncomingDirectory must not start with a backslash!");
                     configInvalid = true;
                 }
-                _incomingPath = Path.Combine(_config.SourceDrive, _config.IncomingDirectory);
-                // ManagedDirectory
                 if (_config.ManagedDirectory.StartsWith(@"\")) {
                     writeLog("ERROR: ManagedDirectory must not start with a backslash!");
                     configInvalid = true;
                 }
+                _incomingPath = Path.Combine(_config.SourceDrive, _config.IncomingDirectory);
                 _managedPath = Path.Combine(_config.SourceDrive, _config.ManagedDirectory);
+                if (CheckSpoolingDirectories() == false) {
+                    writeLog("ERROR checking spooling directories (incoming / managed)!");
+                    configInvalid = true;
+                }
+
                 if (_config.ServiceTimer < 1000) {
                     writeLog("ERROR: ServiceTimer must not be smaller than 1000 ms!");
                     configInvalid = true;
@@ -833,6 +838,19 @@ namespace AutoTx
             return CreateNewDirectory(path, false) == path;
         }
 
+        /// <summary>
+        /// Ensure the required spooling directories (managed/incoming) exist.
+        /// </summary>
+        /// <returns>True if all dirs exist or were created successfully.</returns>
+        private bool CheckSpoolingDirectories() {
+            var retval = CheckForDirectory(_incomingPath);
+            retval &= CheckForDirectory(_managedPath);
+            retval &= CheckForDirectory(Path.Combine(_managedPath, "PROCESSING"));
+            retval &= CheckForDirectory(Path.Combine(_managedPath, "DONE"));
+            retval &= CheckForDirectory(Path.Combine(_managedPath, "UNMATCHED"));
+            return retval;
+        }
+
         #endregion
 
     }
diff --git a/AutoTx/Resources/Install-Service.ps1 b/AutoTx/Resources/Install-Service.ps1
index c22c5b4..daf0470 100644
--- a/AutoTx/Resources/Install-Service.ps1
+++ b/AutoTx/Resources/Install-Service.ps1
@@ -112,13 +112,6 @@ function Uninstall-FileCopyService {
 }
 
 
-function Create-Directories {
-    $foo = New-Item -ItemType Container -Force -Path "$IncomingDir"
-    $foo = New-Item -ItemType Container -Force -Path "$($ManagedDir)\DONE"
-    $foo = New-Item -ItemType Container -Force -Path "$($ManagedDir)\PROCESSING"
-    $foo = New-Item -ItemType Container -Force -Path "$($ManagedDir)\UNMATCHED"
-}
-
 
 $LocalConfiguration = ".\ScriptsConfig.ps1"
 if (Test-Path $LocalConfiguration) {
@@ -136,12 +129,10 @@ try {
     Write-Host "Service $($ServiceName) already installed, updating."
     Stop-MyService
     Copy-ServiceFiles
-    Create-Directories
 }
 catch {
     Copy-ServiceFiles
     Install-Service
-    Create-Directories
 }
 
 Start-MyService
-- 
GitLab