summaryrefslogtreecommitdiff
path: root/Main.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Main.hs')
-rw-r--r--Main.hs45
1 files changed, 45 insertions, 0 deletions
diff --git a/Main.hs b/Main.hs
new file mode 100644
index 0000000..4df4426
--- /dev/null
+++ b/Main.hs
@@ -0,0 +1,45 @@
1{-# LANGUAGE NoImplicitPrelude #-}
2{-# LANGUAGE ScopedTypeVariables #-}
3module Main where
4
5import Rebase.Prelude
6import System.Directory (createDirectoryIfMissing, renameFile)
7import System.FilePath (takeFileName, (</>))
8import System.FSNotify (Event (..), watchDir, withManager)
9import System.Process.Typed (proc, runProcess)
10
11verbose :: Bool
12verbose = True
13
14pdfDirectory, seenDir, pdfPrinterExecutable :: FilePath
15pdfDirectory = "."
16seenDir = pdfDirectory </> "seen"
17pdfPrinterExecutable = "PDFtoPrinter.exe"
18
19main :: IO ()
20main = do
21 createDirectoryIfMissing False seenDir
22 withManager $ \mgr -> do
23 void $ watchDir mgr pdfDirectory (const True) handleEvent
24 forever $ threadDelay 1000000
25
26handleEvent :: Event -> IO ()
27handleEvent (Added f _) | (".pdf" `isSuffixOf` f) = handleNewPdf f
28handleEvent x = when verbose $ print x
29
30handleNewPdf :: FilePath -> IO ()
31handleNewPdf f =
32 -- Note: there is no sense in checking the return result, as PDFtoPrinter.exe
33 -- returns success even when it fails to parse the PDF.
34 runProcessVerbose pdfPrinterExecutable [f] >>
35 moveFileIntoDir f seenDir
36
37
38runProcessVerbose :: FilePath -> [String] -> IO ExitCode
39runProcessVerbose exe args = do
40 when verbose $ putStrLn $ "+ " ++ exe ++ " " ++ unwords args
41 runProcess (proc exe args)
42
43moveFileIntoDir :: FilePath -> FilePath -> IO ()
44moveFileIntoDir f d = renameFile f $ d </> takeFileName f
45