-----------------------------------------------------------------------------
-- Module      :   Convert umlaut written as ae, oe or ue into ä, ö and ü
--              in a txt file
-----------------------------------------------------------------------------
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE TypeSynonymInstances  #-}
{-# LANGUAGE OverloadedStrings     #-}

module Lib.ProcTxt  -- (openMain, htf_thisModuelsTests)
                   where
-- import Uniform.Strings
-- import Uniform.TypedFile
-- import           Uniform.FileIO
-- import Uniform.Error
import           Lib.ProcWord
import UniformBase
import Lib.FileHandling
import Control.Monad.Trans.Writer.Strict

procTxt :: Bool -> [Text] -> Path Abs File -> ErrIO Bool
-- ^ replace umlaut unless it is an permitted group
-- in a file with extension txt
procTxt :: Bool -> [Text] -> Path Abs File -> ErrIO Bool
procTxt Bool
debug [Text]
erl2 Path Abs File
fn = do
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
debug forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadIO m => [Text] -> m ()
putIOwords [Text
"procTxt start", forall {a}. Show a => a -> Text
showT Path Abs File
fn]
    -- erl2         <- readErlaubt fnErl
    -- erl  <- read6 fnErl txtFile -- reads lines
    -- let erlaubt = concat . map words' $ erl :: [Text]

    [Text]
ls :: [Text] <- forall a b.
TypedFiles7a a b =>
Path Abs File -> TypedFile5 a b -> ErrIO b
read8 Path Abs File
fn TypedFile5 Text [Text]
textlinesFile

    -- let ls2 = map (procLine2 erl2) ls
    let ([Text]
ls2,Text
report) = forall w a. Writer w a -> (a, w)
runWriter forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ([Text] -> Text -> Writer Text Text
procLine2Rep [Text]
erl2) [Text]
ls
    let changed :: Bool
changed = forall z. Zeros z => z
zero forall a. Eq a => a -> a -> Bool
/= forall a. CharChains a => a -> a
trim' Text
report
    -- let res = False -- reports change - todo

    -- newfn <- changeExtensionBakOrNew debug fn
    Bool
-> Path Abs File
-> TypedFile5 Text [Text]
-> [Text]
-> ExceptT Text IO ()
writeWithBak Bool
debug Path Abs File
fn TypedFile5 Text [Text]
textlinesFile [Text]
ls2 
    forall (m :: * -> *) a. Monad m => a -> m a
return Bool
changed

    -- rest is copied
    -- if debug
    --     then do
    --     let fnnew = makeAbsFile (toFilePath fn <> "NEW")
    --     putIOwords ["procMd result in new", showT fnnew]
    --     write8 fnnew textlinesFile res
    --     putIOwords ["procMd result in new written", showT res, "fn", showT fnnew]
    --     else do
    --     let fnrename = fn <.> bakExtension :: Path Abs File
    --     renameOneFile (fn <.> txtExtension) fnrename
    --     putIOwords ["procMd renamed to bak", showT fnrename]
    -- write8 fn textlinesFile ls2