Fix -o option for conllu output

Change-Id: I74339433c7234b89dbcb0be6c5a077294725230e
diff --git a/app/src/main/kotlin/de/ids_mannheim/korapxmltools/KorapXmlTool.kt b/app/src/main/kotlin/de/ids_mannheim/korapxmltools/KorapXmlTool.kt
index e5d2c43..e037ded 100644
--- a/app/src/main/kotlin/de/ids_mannheim/korapxmltools/KorapXmlTool.kt
+++ b/app/src/main/kotlin/de/ids_mannheim/korapxmltools/KorapXmlTool.kt
@@ -719,6 +719,24 @@
         }
 
         // Normal ZIP processing mode
+        if (outputFile != null && (outputFormat == OutputFormat.CONLLU || outputFormat == OutputFormat.WORD2VEC || outputFormat == OutputFormat.NOW)) {
+            val finalOutputPath = if (outputDir != ".") {
+                File(outputDir, File(outputFile!!).name).path
+            } else {
+                outputFile!!
+            }
+            val file = File(finalOutputPath)
+            if (file.exists()) {
+                if (!overwrite) {
+                    System.err.println("ERROR: Output file $finalOutputPath already exists. Use --force to overwrite.")
+                    return 1
+                }
+                file.delete()
+            }
+            // Create parent directories
+            file.parentFile?.mkdirs()
+        }
+
         LOGGER.info("Processing zip files: " + zipFileNames!!.joinToString(", "))
 
         korapxml2conllu(zipFileNames!!)
@@ -736,10 +754,7 @@
                 outputFile!!
             }
             
-            // Create parent directories if they don't exist
-            File(finalOutputPath).parentFile?.mkdirs()
-            
-            File(finalOutputPath).writeText(content)
+            File(finalOutputPath).appendText(content)
         } else {
             println(content)
         }
@@ -1174,7 +1189,12 @@
                      parseAndWriteAnnotatedConllu(annotatedConllu, task)
                 }, stderrLogPath = currentLog)
             } else {
-                annotationWorkerPool = AnnotationWorkerPool(annotateWith, maxThreads, LOGGER, null)
+                val handler: ((String, AnnotationWorkerPool.AnnotationTask?) -> Unit)? = if (outputFile != null) {
+                    { output, _ -> writeOutput(output) }
+                } else {
+                    null
+                }
+                annotationWorkerPool = AnnotationWorkerPool(annotateWith, maxThreads, LOGGER, handler)
             }
         }