diff options
Diffstat (limited to 'Main.hs')
-rw-r--r-- | Main.hs | 45 |
1 files changed, 45 insertions, 0 deletions
@@ -0,0 +1,45 @@ | |||
1 | {-# LANGUAGE NoImplicitPrelude #-} | ||
2 | {-# LANGUAGE ScopedTypeVariables #-} | ||
3 | module Main where | ||
4 | |||
5 | import Rebase.Prelude | ||
6 | import System.Directory (createDirectoryIfMissing, renameFile) | ||
7 | import System.FilePath (takeFileName, (</>)) | ||
8 | import System.FSNotify (Event (..), watchDir, withManager) | ||
9 | import System.Process.Typed (proc, runProcess) | ||
10 | |||
11 | verbose :: Bool | ||
12 | verbose = True | ||
13 | |||
14 | pdfDirectory, seenDir, pdfPrinterExecutable :: FilePath | ||
15 | pdfDirectory = "." | ||
16 | seenDir = pdfDirectory </> "seen" | ||
17 | pdfPrinterExecutable = "PDFtoPrinter.exe" | ||
18 | |||
19 | main :: IO () | ||
20 | main = do | ||
21 | createDirectoryIfMissing False seenDir | ||
22 | withManager $ \mgr -> do | ||
23 | void $ watchDir mgr pdfDirectory (const True) handleEvent | ||
24 | forever $ threadDelay 1000000 | ||
25 | |||
26 | handleEvent :: Event -> IO () | ||
27 | handleEvent (Added f _) | (".pdf" `isSuffixOf` f) = handleNewPdf f | ||
28 | handleEvent x = when verbose $ print x | ||
29 | |||
30 | handleNewPdf :: FilePath -> IO () | ||
31 | handleNewPdf 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 | |||
38 | runProcessVerbose :: FilePath -> [String] -> IO ExitCode | ||
39 | runProcessVerbose exe args = do | ||
40 | when verbose $ putStrLn $ "+ " ++ exe ++ " " ++ unwords args | ||
41 | runProcess (proc exe args) | ||
42 | |||
43 | moveFileIntoDir :: FilePath -> FilePath -> IO () | ||
44 | moveFileIntoDir f d = renameFile f $ d </> takeFileName f | ||
45 | |||