From d0d5ec92d912a286d51ea4df2c95f7cc5fd44bb1 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Fri, 9 Mar 2018 23:11:20 -0500 Subject: implement pdf-autoprint --- Main.hs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Main.hs (limited to 'Main.hs') diff --git a/Main.hs b/Main.hs new file mode 100644 index 0000000..4df4426 --- /dev/null +++ b/Main.hs @@ -0,0 +1,45 @@ +{-# LANGUAGE NoImplicitPrelude #-} +{-# LANGUAGE ScopedTypeVariables #-} +module Main where + +import Rebase.Prelude +import System.Directory (createDirectoryIfMissing, renameFile) +import System.FilePath (takeFileName, ()) +import System.FSNotify (Event (..), watchDir, withManager) +import System.Process.Typed (proc, runProcess) + +verbose :: Bool +verbose = True + +pdfDirectory, seenDir, pdfPrinterExecutable :: FilePath +pdfDirectory = "." +seenDir = pdfDirectory "seen" +pdfPrinterExecutable = "PDFtoPrinter.exe" + +main :: IO () +main = do + createDirectoryIfMissing False seenDir + withManager $ \mgr -> do + void $ watchDir mgr pdfDirectory (const True) handleEvent + forever $ threadDelay 1000000 + +handleEvent :: Event -> IO () +handleEvent (Added f _) | (".pdf" `isSuffixOf` f) = handleNewPdf f +handleEvent x = when verbose $ print x + +handleNewPdf :: FilePath -> IO () +handleNewPdf f = + -- Note: there is no sense in checking the return result, as PDFtoPrinter.exe + -- returns success even when it fails to parse the PDF. + runProcessVerbose pdfPrinterExecutable [f] >> + moveFileIntoDir f seenDir + + +runProcessVerbose :: FilePath -> [String] -> IO ExitCode +runProcessVerbose exe args = do + when verbose $ putStrLn $ "+ " ++ exe ++ " " ++ unwords args + runProcess (proc exe args) + +moveFileIntoDir :: FilePath -> FilePath -> IO () +moveFileIntoDir f d = renameFile f $ d takeFileName f + -- cgit v1.2.3