summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2018-03-10 11:39:05 -0500
committerAndrew Cady <d@jerkface.net>2018-03-10 11:39:05 -0500
commite4ad9a4b5512ed3a7779ae848f2c95df24ee46b8 (patch)
tree6890e698b3e613cf9f23489c15ffa306db85b002
parentd0d5ec92d912a286d51ea4df2c95f7cc5fd44bb1 (diff)
add client & server modes
-rw-r--r--Main.hs59
-rw-r--r--package.yaml1
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 @@
1{-# LANGUAGE NoImplicitPrelude #-} 1{-# LANGUAGE NoImplicitPrelude #-}
2{-# LANGUAGE ScopedTypeVariables #-}
3module Main where 2module Main where
4
5import Rebase.Prelude 3import Rebase.Prelude
4
5import Options.Applicative (execParser, help, helper, info, long,
6 metavar, strOption)
6import System.Directory (createDirectoryIfMissing, renameFile) 7import System.Directory (createDirectoryIfMissing, renameFile)
7import System.FilePath (takeFileName, (</>)) 8import System.FilePath (takeFileName, (</>))
8import System.FSNotify (Event (..), watchDir, withManager) 9import System.FSNotify (Event (..), watchDir, withManager)
9import System.Process.Typed (proc, runProcess) 10import System.Process.Typed (proc, runProcess)
10 11
11verbose :: Bool
12verbose = True
13
14pdfDirectory, seenDir, pdfPrinterExecutable :: FilePath 12pdfDirectory, seenDir, pdfPrinterExecutable :: FilePath
15pdfDirectory = "." 13pdfDirectory = "."
16seenDir = pdfDirectory </> "seen" 14seenDir = pdfDirectory </> "seen"
17pdfPrinterExecutable = "PDFtoPrinter.exe" 15pdfPrinterExecutable = "PDFtoPrinter.exe"
18 16
17verbose :: Bool
18verbose = True
19
20data Options = Options
21 { sendTo :: Maybe String
22 }
23
19main :: IO () 24main :: IO ()
20main = do 25main = execParser (info (options <**> helper) mempty) >>= chooseMain
26 where
27 options = fmap Options $ optional $ strOption $ long "send-to"
28 <> metavar "RSYNC-DEST"
29 <> help "Where to send the PDFs via rsync, in rsync target format (host:path)"
30
31 chooseMain :: Options -> IO ()
32 chooseMain (Options Nothing) = serverMain
33 chooseMain (Options (Just target)) = clientMain target
34
35serverMain :: IO ()
36serverMain = do
21 createDirectoryIfMissing False seenDir 37 createDirectoryIfMissing False seenDir
22 withManager $ \mgr -> do 38 handlePdfsForever pdfDirectory pdfPrinter
23 void $ watchDir mgr pdfDirectory (const True) handleEvent
24 forever $ threadDelay 1000000
25 39
26handleEvent :: Event -> IO () 40clientMain :: String -> IO ()
27handleEvent (Added f _) | (".pdf" `isSuffixOf` f) = handleNewPdf f 41clientMain target = handlePdfsForever pdfDirectory (pdfSender target)
28handleEvent x = when verbose $ print x
29 42
30handleNewPdf :: FilePath -> IO () 43pdfPrinter :: FilePath -> IO ()
31handleNewPdf f = 44pdfPrinter f =
32 -- Note: there is no sense in checking the return result, as PDFtoPrinter.exe 45 -- Note: there is no sense in checking the return result, as PDFtoPrinter.exe
33 -- returns success even when it fails to parse the PDF. 46 -- returns success even when it fails to parse the PDF.
34 runProcessVerbose pdfPrinterExecutable [f] >> 47 runProcessVerbose pdfPrinterExecutable [f] >>
35 moveFileIntoDir f seenDir 48 moveFileIntoDir f seenDir
36 49
50pdfSender :: String -> FilePath -> IO ()
51pdfSender target f =
52 runProcessVerbose "rsync" ["--remove-source-files", f, target] >> return ()
53
54handlePdfsForever :: FilePath -> (FilePath -> IO ()) -> IO ()
55handlePdfsForever dir h = handleEventsForever dir (handleAdds (".pdf" `isSuffixOf`) h)
56
57handleAdds :: (String -> Bool) -> (FilePath -> IO ()) -> Event -> IO ()
58handleAdds predicate handleFile (Added f _) | predicate f = handleFile f
59handleAdds _ _ x = when verbose $ print x
60
61handleEventsForever :: FilePath -> (Event -> IO ()) -> IO ()
62handleEventsForever dir eventHandler = do
63 withManager $ \mgr -> do
64 void $ watchDir mgr dir (const True) eventHandler
65 forever $ threadDelay 1000000
37 66
38runProcessVerbose :: FilePath -> [String] -> IO ExitCode 67runProcessVerbose :: FilePath -> [String] -> IO ExitCode
39runProcessVerbose exe args = do 68runProcessVerbose 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:
12- fsnotify 12- fsnotify
13- typed-process 13- typed-process
14- filepath 14- filepath
15- optparse-applicative
15 16
16executables: 17executables:
17 pdf-autoprint: 18 pdf-autoprint: