From e4ad9a4b5512ed3a7779ae848f2c95df24ee46b8 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Sat, 10 Mar 2018 11:39:05 -0500 Subject: add client & server modes --- Main.hs | 59 ++++++++++++++++++++++++++++++++++++++++++++--------------- package.yaml | 1 + 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/Main.hs b/Main.hs index 4df4426..2e00d1e 100644 --- a/Main.hs +++ b/Main.hs @@ -1,39 +1,68 @@ -{-# LANGUAGE NoImplicitPrelude #-} -{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE NoImplicitPrelude #-} module Main where - import Rebase.Prelude + +import Options.Applicative (execParser, help, helper, info, long, + metavar, strOption) 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" +verbose :: Bool +verbose = True + +data Options = Options + { sendTo :: Maybe String + } + main :: IO () -main = do +main = execParser (info (options <**> helper) mempty) >>= chooseMain + where + options = fmap Options $ optional $ strOption $ long "send-to" + <> metavar "RSYNC-DEST" + <> help "Where to send the PDFs via rsync, in rsync target format (host:path)" + + chooseMain :: Options -> IO () + chooseMain (Options Nothing) = serverMain + chooseMain (Options (Just target)) = clientMain target + +serverMain :: IO () +serverMain = do createDirectoryIfMissing False seenDir - withManager $ \mgr -> do - void $ watchDir mgr pdfDirectory (const True) handleEvent - forever $ threadDelay 1000000 + handlePdfsForever pdfDirectory pdfPrinter -handleEvent :: Event -> IO () -handleEvent (Added f _) | (".pdf" `isSuffixOf` f) = handleNewPdf f -handleEvent x = when verbose $ print x +clientMain :: String -> IO () +clientMain target = handlePdfsForever pdfDirectory (pdfSender target) -handleNewPdf :: FilePath -> IO () -handleNewPdf f = +pdfPrinter :: FilePath -> IO () +pdfPrinter 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 +pdfSender :: String -> FilePath -> IO () +pdfSender target f = + runProcessVerbose "rsync" ["--remove-source-files", f, target] >> return () + +handlePdfsForever :: FilePath -> (FilePath -> IO ()) -> IO () +handlePdfsForever dir h = handleEventsForever dir (handleAdds (".pdf" `isSuffixOf`) h) + +handleAdds :: (String -> Bool) -> (FilePath -> IO ()) -> Event -> IO () +handleAdds predicate handleFile (Added f _) | predicate f = handleFile f +handleAdds _ _ x = when verbose $ print x + +handleEventsForever :: FilePath -> (Event -> IO ()) -> IO () +handleEventsForever dir eventHandler = do + withManager $ \mgr -> do + void $ watchDir mgr dir (const True) eventHandler + forever $ threadDelay 1000000 runProcessVerbose :: FilePath -> [String] -> IO ExitCode runProcessVerbose exe args = do diff --git a/package.yaml b/package.yaml index 435c2f7..3dfc2f3 100644 --- a/package.yaml +++ b/package.yaml @@ -12,6 +12,7 @@ dependencies: - fsnotify - typed-process - filepath +- optparse-applicative executables: pdf-autoprint: -- cgit v1.2.3