]> gitweb.michael.orlitzky.com - dead/lwn-epub.git/blob - src/Main.hs
Add http-conduit and http-types dependencies.
[dead/lwn-epub.git] / src / Main.hs
1 {-# LANGUAGE DoAndIfThenElse #-}
2 module Main
3 where
4
5 import Control.Concurrent.ParallelIO (stopGlobalPool)
6 import System.Directory (doesFileExist)
7 import System.IO (
8 Handle,
9 IOMode (WriteMode),
10 openBinaryFile,
11 stdout)
12
13 import CommandLine (show_help)
14 import Configuration (Cfg(..), get_cfg)
15 import LWN.Article (real_article_path)
16 import LWN.HTTP (get_login_cookie)
17 import LWN.Page (epublish, page_from_url)
18
19
20 -- | If we're given an empty path, return a handle to
21 -- 'stdout'. Otherwise, open the given file and return a read/write
22 -- handle to that.
23 get_output_handle :: FilePath -> IO Handle
24 get_output_handle path =
25 if (null path) then
26 return stdout
27 else
28 openBinaryFile path WriteMode
29
30
31 argument_is_file :: Cfg -> IO Bool
32 argument_is_file cfg = do
33 path <- real_article_path (article cfg)
34 doesFileExist path
35
36 main :: IO ()
37 main = do
38 cfg' <- get_cfg
39 aif <- argument_is_file cfg'
40 cfg <- case aif of
41 False -> get_login_cookie cfg'
42 True -> return cfg'
43 page <- page_from_url cfg (article cfg)
44 case page of
45 Just p -> do
46 output_handle <- get_output_handle (output cfg)
47 epublish p output_handle
48 Nothing -> do
49 _ <- show_help
50 return ()
51
52 -- Necessary, for some reason.
53 stopGlobalPool