summaryrefslogtreecommitdiffstats
path: root/app/Main.hs
blob: 03a29605f81620de5577ab5ea6df0a4ff55894ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE NoFieldSelectors #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}

module Main where

import Control.Arrow (first, second)
import Control.Concurrent.ParallelIO.Local (parallel, withPool)
import Control.Exception (Exception, throw, throwIO)
import Control.Monad (forM, unless, when)
import Data.Aeson qualified as J
import Data.Attoparsec.Text qualified as A
import Data.ByteString.Lazy qualified as LB
import Data.Default
import Data.Digest.Pure.SHA (sha1, showDigest)
import Data.Either (partitionEithers)
import Data.Function ((&))
import Data.List
import Data.List.NonEmpty qualified as N
import Data.Map qualified as M
import Data.Maybe (catMaybes, fromMaybe, mapMaybe, maybeToList)
import Data.Ord (comparing)
import Data.Set qualified as S
import Data.String (IsString (fromString))
import Data.Text qualified as T
import Data.Text.Encoding qualified as T
import Data.Text.IO qualified as T
import Data.Time.Clock (UTCTime, getCurrentTime)
import Data.Time.Format.ISO8601 (iso8601ParseM, iso8601Show)
import Debug.Trace
import GHC.Conc (getNumProcessors)
import GHC.Generics (Generic)
import GHC.Records (HasField (..))
import Options.Applicative qualified as O
import Prompt qualified as P
import Settings qualified as S
import System.Directory
import System.Environment (getEnv)
import System.FilePath
import System.IO.LockFile (withLockFile)
import System.IO.Temp (withSystemTempDirectory)
import System.Process.Typed
import Tag qualified as G
import Text.Printf (printf)
import Text.RE.TDFA.Text qualified as R
import Text.Read (readMaybe)

data Args = Args
  { cmd :: Cmd
  }

data Cmd
  = Consume
      { keep :: Bool,
        inputs :: [FilePath],
        prompt :: Bool,
        force :: Bool,
        language :: Maybe String,
        auto :: Bool
      }
  | Edit
      { indexNames :: [FilePath]
      }
  | List
      { filters :: [G.Filter],
        todo :: Bool,
        view :: Bool,
        redo :: Bool,
        edit :: Bool
      }
  | Todo
  | View
      { indexNames :: [FilePath]
      }
  | TopWords
  | Modify
      { indexNames :: [FilePath],
        tags :: [G.Tag],
        untags :: [G.Tag]
      }

args :: O.Parser Args
args =
  Args <$> cmd

cmd :: O.Parser Cmd
cmd =
  O.hsubparser . mconcat $
    [ O.command "consume" . O.info consumeCmd $
        O.progDesc "Consume document(s)",
      O.command "edit" . O.info editCmd $
        O.progDesc "Edit document(s)",
      O.command "list" . O.info listCmd $
        O.progDesc "List document(s)",
      O.command "todo" . O.info todoCmd $
        O.progDesc "Interactively process new documents",
      O.command "view" . O.info viewCmd $
        O.progDesc "View document(s)",
      O.command "topwords" . O.info topWordsCmd $
        O.progDesc "View probability map per tag",
      O.command "modify" . O.info modifyCmd $
        O.progDesc "Modify document(s)"
    ]

consumeCmd :: O.Parser Cmd
consumeCmd =
  Consume
    <$> keepArg
    <*> inputsArg
    <*> promptArg
    <*> forceArg
    <*> languageArg
    <*> autoArg

editCmd :: O.Parser Cmd
editCmd =
  Edit
    <$> indexNamesArg

listCmd :: O.Parser Cmd
listCmd =
  List
    <$> filtersArg
    <*> todoArg
    <*> viewArg
    <*> redoArg
    <*> editArg

todoCmd :: O.Parser Cmd
todoCmd =
  pure Todo

viewCmd :: O.Parser Cmd
viewCmd =
  View
    <$> indexNamesArg

topWordsCmd :: O.Parser Cmd
topWordsCmd =
  pure TopWords

modifyCmd :: O.Parser Cmd
modifyCmd =
  Modify
    <$> indexNamesArg
    <*> tagsArg
    <*> untagsArg

inputsArg :: O.Parser [FilePath]
inputsArg =
  O.many
    ( O.strArgument
        ( O.metavar "FILE"
            <> O.action "file"
        )
    )

keepArg :: O.Parser Bool
keepArg =
  O.switch
    ( O.long "keep"
        <> O.short 'k'
        <> O.help "Keep input document"
    )

promptArg :: O.Parser Bool
promptArg =
  O.switch
    ( O.long "prompt"
        <> O.short 'p'
        <> O.help "Prompt after consuming document(s)"
    )

forceArg :: O.Parser Bool
forceArg =
  O.switch
    ( O.long "force"
        <> O.short 'f'
        <> O.help "Force operation, overriding safety checks"
    )

autoArg :: O.Parser Bool
autoArg =
  O.switch
    ( O.long "auto"
        <> O.short 'a'
        <> O.help "Automatically tag document(s)"
    )

filtersArg :: O.Parser [G.Filter]
filtersArg =
  O.many $
    O.option
      (O.eitherReader (A.parseOnly G.filterParser . T.pack))
      ( O.long "filter"
          <> O.short 'f'
          <> O.help "Filter documents by tag"
      )

tagsArg :: O.Parser [G.Tag]
tagsArg =
  O.many $
    O.option
      (O.eitherReader (A.parseOnly G.tagParser . T.pack))
      ( O.long "tag"
          <> O.help "Tag to add"
      )

untagsArg :: O.Parser [G.Tag]
untagsArg =
  O.many $
    O.option
      (O.eitherReader (A.parseOnly G.tagParser . T.pack))
      ( O.long "untag"
          <> O.help "Tag to remove"
      )

languageArg :: O.Parser (Maybe String)
languageArg =
  O.optional
    ( O.strOption
        ( O.long "language"
            <> O.short 'l'
            <> O.help "Specify document language"
        )
    )

indexNamesArg :: O.Parser [FilePath]
indexNamesArg =
  O.many $
    O.strArgument
      ( O.metavar "ID"
          <> O.action "file"
      )

todoArg :: O.Parser Bool
todoArg =
  O.switch
    ( O.long "todo"
        <> O.help "Run command `todo` on listed document(s)"
    )

redoArg :: O.Parser Bool
redoArg =
  O.switch
    ( O.long "redo"
        <> O.help "Redo OCR on listed document(s)"
    )

editArg :: O.Parser Bool
editArg =
  O.switch
    ( O.long "edit"
        <> O.help "Run command `edit` on listed document(s)"
    )

viewArg :: O.Parser Bool
viewArg =
  O.switch
    ( O.long "view"
        <> O.help "Run command `view` on listed document(s)"
    )

main :: IO ()
main = do
  settings <- S.readSettings
  S.writeSettings "apaperless.yaml" settings

  cwd <- getCurrentDirectory
  setCurrentDirectory =<< getEnv "APAPERLESS_STORE_DIR"
  ensureGit
  ensureDir "originals"
  ensureDir "index"

  O.execParser (O.info (args O.<**> O.helper) O.idm) >>= \case
    Args {cmd = Consume {keep, inputs, force, prompt, language, auto}} -> do
      indexNames <-
        parMapM
          ( consume1
              (fromMaybe settings.defaultLanguage language)
              force
              keep
          )
          (map (cwd </>) inputs)
      docs <- mapM (readDocument . (<.> "json")) indexNames
      allDocs <- getDocuments
      docs' <-
        if
            | auto -> processDocuments settings allDocs docs
            | prompt -> processDocumentsInteractively settings allDocs docs
            | otherwise -> pure docs
      mapM_
        ( \doc -> do
            printf "%s\n" (takeBaseName doc.iFileName)
            printTags doc
        )
        docs'
    Args {cmd = Edit {indexNames}} -> do
      editDocuments
        =<< mapM (readDocument . (<.> "json")) indexNames
    Args {cmd = List {filters, redo, todo = False, view = False, edit = False}} -> do
      doRedoIf filters redo
      mapM_
        ( \doc -> do
            printf "%s\n" (takeBaseName doc.iFileName)
            printTags doc
        )
        . filter (G.applyFilters filters . (.index.tags))
        =<< getDocuments
    Args {cmd = List {filters, redo, edit = True}} -> do
      doRedoIf filters redo
      editDocuments
        . filter (G.applyFilters filters . (.index.tags))
        =<< getDocuments
    Args {cmd = List {filters, redo, todo = True}} -> do
      doRedoIf filters redo
      allDocs <- getDocuments
      _ <-
        processDocumentsInteractively settings allDocs
          . filter (G.applyFilters filters . (.index.tags))
          $ allDocs
      pure ()
    Args {cmd = Todo} -> do
      allDocs <- getDocuments
      _ <-
        processDocumentsInteractively settings allDocs
          . filter (G.applyFilters [G.filter G.include "todo" Nothing] . (.index.tags))
          $ allDocs
      pure ()
    Args {cmd = List {filters, redo, view = True}} -> do
      doRedoIf filters redo
      viewDocuments
        . filter (G.applyFilters filters . (.index.tags))
        =<< getDocuments
    Args {cmd = View {indexNames}} -> do
      viewDocuments
        =<< mapM (readDocument . (<.> "json")) indexNames
    Args {cmd = TopWords} -> do
      allDocs <- getDocuments
      mapM_
        ( \(tag, xs) -> do
            print tag
            mapM_ (\(word, p) -> printf "  %s: %.4f\n" word p) xs
        )
        $ map (second (sortBy (comparing (negate . snd))))
        $ map (second (filter ((> 0) . snd)))
        $ M.toList
        $ foldl
          ( \wordProbabilityPerTag (word, tag, p) ->
              M.insertWith (++) tag [(word, p)] wordProbabilityPerTag
          )
          M.empty
          (probabilityMap allDocs)
    Args {cmd = Modify {indexNames, tags, untags}} -> do
      docs <- mapM (readDocument . (<.> "json")) indexNames
      mapM_
        ( \doc -> do
            let doc' = addTags tags (removeTags untags doc)
            withGit do
              J.encodeFile doc'.iFilePath doc'.index
              commitAll [doc.iFilePath] (printf "tag %s" (takeBaseName doc.iFilePath))
        )
        docs

printTags :: Document -> IO ()
printTags doc =
  mapM_
    ( \tag ->
        case G.tagValue tag of
          Nothing -> printf "@%s\n" (G.tagKey tag)
          Just tagValue -> printf "@%s %s\n" (G.tagKey tag) tagValue
    )
    (doc.index.tags `S.union` doc.index.internalTags)

doRedoIf :: [G.Filter] -> Bool -> IO ()
doRedoIf filters redo =
  when redo do
    parMapM_ doRedo
      . filter (G.applyFilters filters . (.index.tags))
      =<< getDocuments
  where
    doRedo doc = do
      content <- ocr doc.index.language doc.oFilePath
      withGit do
        J.encodeFile doc.iFilePath doc.index {content = content}
        commitAll [doc.iFilePath] (printf "redo %s" (takeBaseName doc.iFilePath))

data Document = Document
  { iFileName :: String,
    index :: Index
  }
  deriving (Show)

instance HasField "oFilePath" Document FilePath where
  getField doc = "originals" </> takeBaseName doc.iFileName <.> "pdf"

instance HasField "iFilePath" Document FilePath where
  getField doc = "index" </> doc.iFileName

hasWord :: T.Text -> Document -> Bool
hasWord word doc = S.member word doc.index.originalWords

hasTag :: G.Tag -> Document -> Bool
hasTag tag doc = S.member tag doc.index.tags

hasWordAndTag :: T.Text -> G.Tag -> Document -> Bool
hasWordAndTag word tag doc = hasTag tag doc && hasWord word doc

tagValues :: [Document] -> M.Map T.Text (S.Set T.Text)
tagValues docs =
  M.unionsWith S.union $
    mapMaybe
      ( \tag ->
          M.singleton (G.tagKey tag) . S.singleton <$> (G.tagValue tag)
      )
      (S.toList (S.unions (map (.index.tags) docs)))

getDocuments :: IO [Document]
getDocuments =
  parMapM readDocument
    =<< sort <$> listDirectory "index"

readDocument :: FilePath -> IO Document
readDocument iFileName =
  Document iFileName
    <$> decodeFile @Index ("index" </> iFileName)

processDocumentsInteractively :: S.Settings -> [Document] -> [Document] -> IO [Document]
processDocumentsInteractively settings allDocs docs =
  mapM (uncurry processDocumentInteractively) (zip [1 :: Int ..] docs)
  where
    numDocs = length docs
    processDocumentInteractively n doc = do
      choice <-
        P.prompt
          ( P.choice
              ( printf
                  "%s\n%s\n\n(%d/%d) Process this document?"
                  (takeBaseName doc.iFileName)
                  doc.index.shortText
                  n
                  numDocs
              )
              (("f" :: String) N.:| ["p", "s", "v"])
              & P.help
                ( \a -> case a of
                    "f" -> "view full text"
                    "p" -> "process document"
                    "s" -> "skip document"
                    "v" -> "view document"
                )
          )
      case choice of
        "f" -> do
          printf "%s\n" (takeBaseName doc.iFileName)
          printf "%s\n" doc.index.content
          processDocumentInteractively n doc
        "p" -> tagDocumentInteractively settings allDocs doc
        "s" -> pure doc
        "v" -> do
          viewDocuments [doc]
          processDocumentInteractively n doc

viewDocuments :: [Document] -> IO ()
viewDocuments docs =
  sh_
    ( "zathura "
        <> ( intercalate
               " "
               (map (\doc -> printf "'%s'" doc.oFilePath) docs)
           )
    )

editDocuments :: [Document] -> IO ()
editDocuments docs =
  withSystemTempDirectory "apaperless" $ \tmp -> do
    let fp doc = tmp </> takeBaseName doc.iFileName <.> "txt"
    mapM_
      ( \doc ->
          T.writeFile (fp doc) doc.index.content
      )
      docs
    sh_
      ( "vim "
          <> ( intercalate
                 " "
                 (map (\doc -> printf "'%s'" (fp doc)) docs)
             )
      )
    mapM_
      ( \doc -> do
          content <- T.readFile (fp doc)
          withGit do
            J.encodeFile doc.iFilePath doc.index {content = content}
            commitAll [doc.iFilePath] (printf "edit %s" (takeBaseName doc.iFilePath))
          pure content
      )
      docs

type ProbabilityMap = [(T.Text, G.Tag, Double)]

-- TODO Consider words that contribute to NOT adding a tag
--
-- If there is a document that should, say, not have a `@correspondent`, we should score words that contribute to that fact as well.
--
-- @topic probability-map
probabilityMap :: [Document] -> ProbabilityMap
probabilityMap allDocs =
  [ let docsWithWord = filter (hasWord word) allDocs
        docsWithTag = filter (hasTag tag) allDocs
        docsWithWordAndTag = filter (hasWordAndTag word tag) allDocs
        p =
          fi (length docsWithWordAndTag)
            / fi
              ( length docsWithWord
                  + length docsWithTag
                  - length docsWithWordAndTag
              )
     in (word, tag, p)
    | word <- S.toList allWords,
      tag <- S.toList allTags
  ]
  where
    allTags = foldl S.union S.empty (map (.index.tags) allDocs)
    allWords = foldl S.union S.empty (map (.index.originalWords) allDocs)
    fi = fromIntegral @Int @Double

suggestTags :: S.Settings -> [Document] -> Document -> IO [(G.Tag, [G.Tag])]
suggestTags settings allDocs doc = do
  forM settings.suggestedTags $ \suggestedTag -> do
    case suggestedTag of
      S.SuggestTagByRE tagName pattern -> do
        let tagValues =
              nub . catMaybes . map R.matchedText . R.allMatches $
                doc.index.content
                  R.*=~ pattern
        pure (G.tag tagName (Just ""), map (G.tag tagName . Just) tagValues)
      S.SuggestTagByTags tagName -> do
        let tagValues =
              -- TODO Cache `probabilityMap`
              --
              -- @topic probability-map
              probabilityMap allDocs
                & filter
                  ( \(word, tag, _) ->
                      G.tagKey tag == tagName && hasWord word doc
                  )
                & foldl'
                  ( \scorePerTagValue (_, tag, p) ->
                      M.insertWith
                        (+)
                        (G.tagValue tag)
                        p
                        scorePerTagValue
                  )
                  M.empty
                & M.toList
                & sortBy (comparing (negate . snd))
                & map fst
        pure (G.tag tagName (Just ""), map (G.tag tagName) tagValues)

autoApplySuggestedTags :: [(G.Tag, [G.Tag])] -> [G.Tag]
autoApplySuggestedTags =
  mapMaybe
    ( \(_, suggestedTags) ->
        if null suggestedTags
          then Nothing
          else Just (head suggestedTags)
    )

processDocuments :: S.Settings -> [Document] -> [Document] -> IO [Document]
processDocuments settings allDocs docs =
  mapM processDocument docs
  where
    processDocument doc = do
      tags <-
        S.fromList . autoApplySuggestedTags
          <$> suggestTags settings allDocs doc
      let doc' =
            doc
              { index =
                  doc.index
                    { tags = doc.index.tags `S.union` tags,
                      todo = False
                    }
              }
      withGit do
        J.encodeFile doc.iFilePath doc'.index
        commitAll [doc.iFilePath] (printf "process %s (auto)" doc.iFilePath)
      pure doc'

applyTags :: [Either T.Text G.Tag] -> Document -> Document
applyTags tags' doc = do
  addTags tags (removeTags untags doc)
  where
    (untags, tags) =
      first (map (\tagKey -> G.tag tagKey Nothing)) $
        partitionEithers tags'

data AddTagException = AddTagException G.Tag
  deriving (Show)

instance Exception AddTagException

addTags :: [G.Tag] -> Document -> Document
addTags =
  flip (foldl (flip addTag))

addTag :: G.Tag -> Document -> Document
addTag tag doc =
  if
      | G.tagKey tag == "todo" ->
          doc {index = doc.index {todo = True}}
      | G.tagKey tag == "addedAt" ->
          maybe
            (throw (AddTagException tag))
            ( \addedAt ->
                doc {index = doc.index {addedAt = addedAt}}
            )
            (iso8601ParseM . T.unpack =<< (G.tagValue tag))
      | G.tagKey tag == "modifiedAt" ->
          maybe
            (throw (AddTagException tag))
            ( \modifiedAt ->
                doc {index = doc.index {modifiedAt = Just modifiedAt}}
            )
            (iso8601ParseM . T.unpack =<< (G.tagValue tag))
      | G.tagKey tag == "content" ->
          maybe
            (throw (AddTagException tag))
            ( \content ->
                doc {index = doc.index {content = content}}
            )
            (G.tagValue tag)
      | G.tagKey tag == "language" ->
          throw (AddTagException tag)
      | otherwise ->
          doc
            { index =
                doc.index
                  { tags = G.replace tag doc.index.tags
                  }
            }

data RemoveTagException = RemoveTagException G.Tag
  deriving (Show)

instance Exception RemoveTagException

removeTags :: [G.Tag] -> Document -> Document
removeTags =
  flip (foldl (flip removeTag))

removeTag :: G.Tag -> Document -> Document
removeTag tag doc =
  if
      | G.tagKey tag == "todo" ->
          doc {index = doc.index {todo = False}}
      | G.tagKey tag == "addedAt" ->
          throw (RemoveTagException tag)
      | G.tagKey tag == "modifiedAt" ->
          throw (RemoveTagException tag)
      | G.tagKey tag == "language" ->
          throw (RemoveTagException tag)
      | G.tagKey tag == "content" ->
          throw (RemoveTagException tag)
      | otherwise ->
          doc
            { index =
                doc.index
                  { tags =
                      maybe
                        (G.deleteAll (G.tagKey tag))
                        (\_ -> G.delete tag)
                        (G.tagValue tag)
                        doc.index.tags
                  }
            }

tagDocumentInteractively :: S.Settings -> [Document] -> Document -> IO Document
tagDocumentInteractively settings allDocs doc = do
  suggestedTags <- suggestTags settings allDocs doc
  tags <- mapM (uncurry tagDocumentInteractively') suggestedTags
  let doc' =
        (applyTags tags doc)
          { index = doc.index {todo = False}
          }
  withGit do
    J.encodeFile doc.iFilePath doc'.index
    commitAll [doc.iFilePath] (printf "process %s (interactive)" doc.iFilePath)
    pure doc'
  where
    tagDocumentInteractively' :: G.Tag -> [G.Tag] -> IO (Either T.Text G.Tag)
    tagDocumentInteractively' tag tags
      | Nothing <- G.tagValue tag = do
          choice <-
            P.prompt $
              P.choice
                (printf "tag with %s?" (G.tagKey tag))
                (("n" :: String) N.:| ["y"])
          pure $
            if (choice == "y")
              then Right tag
              else Left (G.tagKey tag)
      | Just _ <- G.tagValue tag = do
          tagValue <-
            fmap T.pack . P.prompt $
              P.string
                (printf "tag with %s?" (G.tagKey tag))
                (mapMaybe (fmap T.unpack . G.tagValue) tags ++ ["-"])
          pure $
            if tagValue == "-"
              then Left (G.tagKey tag)
              else Right tag

ensureGit :: IO ()
ensureGit = do
  doesExist <- doesDirectoryExist ".git"
  unless doesExist $ sh_ ">/dev/null git init --initial-branch main"

ensureDir :: FilePath -> IO ()
ensureDir dirName =
  createDirectoryIfMissing False dirName

debug :: Show a => String -> a -> a
debug s x =
  trace (printf "%s: %s\n" s (show x)) x

fileKey :: FilePath -> IO FilePath
fileKey filePath =
  showDigest . sha1 <$> LB.readFile filePath

consume1 :: String -> Bool -> Bool -> FilePath -> IO FilePath
consume1 language force keep filePath = do
  fKey <- fileKey filePath
  let oFilePath = "originals" </> fKey <.> takeExtension filePath
  originalExists <- doesFileExist oFilePath
  when (originalExists && not force) do
    error (printf "error: error adding %s: duplicate of %s\n" filePath oFilePath)
  let iFilePath = "index" </> fKey <.> "json"
  content <- do
    content' <-
      T.decodeUtf8 . LB.toStrict
        <$> sh (printf "pdftotext -layout '%s' -" filePath)
    let hasText = (not . T.null) . T.strip $ content'
    if not hasText
      then ocr language filePath
      else pure content'
  addedAt <- getCurrentTime
  withGit do
    J.encodeFile
      iFilePath
      Index
        { tags = S.empty,
          todo = True,
          modifiedAt = Nothing,
          ..
        }
    if keep
      then copyFile filePath oFilePath
      else renameFile filePath oFilePath
    commitAll
      [iFilePath, oFilePath]
      (printf "add %s" (takeFileName filePath))
  pure (takeBaseName iFilePath)

withGit :: IO a -> IO a
withGit = withLockFile def ".gitlock"

commitAll :: [FilePath] -> String -> IO ()
commitAll fps m = do
  sh_ (">/dev/null git add -- " ++ intercalate " " (map (printf "'%s'") fps))
  sh_ (printf ">/dev/null git commit -m '%s' || :" m)

data DecodeException = DecodeException FilePath String
  deriving (Show)

instance Exception DecodeException

decodeFile :: J.FromJSON a => FilePath -> IO a
decodeFile fp =
  either (throwIO . DecodeException fp) pure . J.eitherDecode
    =<< LB.readFile fp

ocr :: String -> FilePath -> IO T.Text
ocr language input =
  withSystemTempDirectory (takeBaseName input) $ \tmp -> do
    let fn suffix = tmp </> takeBaseName input <> suffix
    pdfInfo <- parsePdfInfo <$> sh (printf "pdfinfo '%s'" input)
    pdfImages <- parsePdfImages <$> sh (printf "pdfimages -list '%s'" input)
    let isScan =
          length pdfImages == pdfInfo.numPages
            && all ((pdfInfo.pageSize ==) . imageSize) pdfImages
    if isScan
      then sh_ (printf "pdfimages '%s' '%s' -tiff" input (fn ""))
      else sh_ (printf "pdftoppm '%s' '%s' -r 300 -tiff" input (fn "-%d.pdf"))
    imageFiles <- sort <$> listDirectory tmp
    -- XXX add DPI information to image so that resulting pdf preserves DPI
    parMapM_
      ( \(pdfImage, imageFile) ->
          sh_
            ( printf
                "convert -density %dx%d -units PixelsPerInch '%s' '%s'"
                pdfImage.xPpi
                pdfImage.yPpi
                (tmp </> imageFile)
                (tmp </> imageFile)
            )
      )
      (zip pdfImages imageFiles)
    T.unlines <$> mapM (ocr1 language tmp . (tmp </>)) imageFiles

ocr1 :: String -> FilePath -> FilePath -> IO T.Text
ocr1 language tmp input =
  T.decodeUtf8 . LB.toStrict
    -- XXX `--oem 1` seems to be unavailable
    <$> sh (printf "tesseract '%s' - -l '%s' --oem 3 --psm 1" (tmp </> input) language)

data Index = Index
  { content :: T.Text,
    tags :: S.Set G.Tag,
    addedAt :: UTCTime,
    modifiedAt :: Maybe UTCTime,
    todo :: Bool,
    language :: String
  }
  deriving (Show, Generic, Eq)

instance J.ToJSON Index

instance J.FromJSON Index

instance HasField "shortText" Index T.Text where
  getField =
    (T.unlines . take 10 . T.lines)
      . (.content)

instance HasField "internalTags" Index (S.Set G.Tag) where
  getField index =
    index.tags
      `S.union` internalTags index

-- TODO Cache `originalWords`
--
-- @related cache-probabilitymap
instance HasField "originalWords" Index (S.Set T.Text) where
  getField index =
    S.fromList (T.words index.content)

internalTags :: Index -> S.Set G.Tag
internalTags index =
  S.fromList
    ( concat
        [ [ G.tag "addedAt" (Just (T.pack (iso8601Show index.addedAt))),
            G.tag "language" (Just (T.pack index.language))
          ],
          maybeToList (G.tag "modifiedAt" . Just . T.pack . iso8601Show <$> index.modifiedAt),
          if index.todo then [G.tag "todo" Nothing] else []
        ]
    )

data PdfInfo = PdfInfo
  { numPages :: Int,
    pageSize :: (Double, Double)
  }
  deriving (Show)

data PdfInfoException = PdfInfoException
  deriving (Show)

instance Exception PdfInfoException

parsePdfInfo :: LB.ByteString -> PdfInfo
parsePdfInfo out' =
  fromMaybe (throw PdfInfoException) $ do
    numPages <- readMaybe . T.unpack =<< M.lookup "Pages" kvs
    pageSize <-
      rightToMaybe . A.parseOnly pageSizeParser
        =<< M.lookup "Page size" kvs
    pure PdfInfo {..}
  where
    out = T.decodeUtf8 (LB.toStrict out')
    kvs =
      M.fromList
        . map (second T.stripStart)
        . map (second T.tail . T.break (== ':'))
        . filter (not . T.null)
        . T.lines
        $ out
    pageSizeParser =
      (,)
        <$> (A.double <* A.string " x ")
        <*> (A.double <* A.string " pts (A4)")
        <* A.endOfInput

type PdfImages = [PdfImage]

data PdfImage = PdfImage
  { page :: Int,
    num :: Int,
    type_ :: String,
    width :: Int,
    height :: Int,
    color :: String,
    comp :: Int,
    bpc :: Int,
    enc :: String,
    interp :: String,
    object :: Int,
    id :: Int,
    xPpi :: Int,
    yPpi :: Int,
    size :: String,
    ratio :: String
  }
  deriving (Show)

imageSize :: PdfImage -> (Double, Double)
imageSize (PdfImage {..}) =
  let f ppi p = 72 * fromIntegral p / fromIntegral ppi
   in (f xPpi width, f yPpi height)

data PdfImagesException = PdfImagesException
  deriving (Show)

instance Exception PdfImagesException

data ProcessException = ProcessException Int LB.ByteString
  deriving (Show)

instance Exception ProcessException

parsePdfImages :: LB.ByteString -> PdfImages
parsePdfImages out' =
  map
    ( \(page' : num' : type_ : width' : height' : color : comp' : bpc' : enc : interp : object' : id' : xPpi' : yPpi' : size : ratio : []) ->
        PdfImage
          { page = read page',
            num = read num',
            width = read width',
            height = read height',
            comp = read comp',
            bpc = read bpc',
            object = read object',
            id = read id',
            xPpi = read xPpi',
            yPpi = read yPpi',
            ..
          }
    )
    . map (map T.unpack)
    . map T.words
    . drop 2
    . filter (not . T.null)
    . T.lines
    $ out
  where
    out = T.decodeUtf8 (LB.toStrict out')

sh :: String -> IO LB.ByteString
sh cmd = do
  -- printf "+ %s\n" cmd
  (exitCode, out, err) <- readProcess (fromString cmd)
  case exitCode of
    ExitSuccess -> return out
    ExitFailure exitCode' -> throwIO $ ProcessException exitCode' err

sh_ :: String -> IO ()
sh_ cmd = do
  -- printf "+ %s\n" cmd
  (exitCode, err) <- readProcessStderr (fromString cmd)
  case exitCode of
    ExitSuccess -> return ()
    ExitFailure exitCode' -> throwIO $ ProcessException exitCode' err

rightToMaybe :: Either e a -> Maybe a
rightToMaybe = either (const Nothing) Just

parMapM :: (a -> IO b) -> [a] -> IO [b]
parMapM f xs = do
  n <- getNumProcessors
  withPool n $ \pool -> parallel pool (map f xs)

parMapM_ :: (a -> IO b) -> [a] -> IO ()
parMapM_ f = fmap (const ()) . parMapM f