]> gitweb.michael.orlitzky.com - dead/lwn-epub.git/blob - src/Main.hs
Print an error before usage if we can't parse the page.
[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 hPutStrLn,
11 openBinaryFile,
12 stderr,
13 stdout
14 )
15
16 import CommandLine (show_help)
17 import Configuration (Cfg(..), get_cfg)
18 import LWN.Article (real_article_path)
19 import LWN.HTTP (get_login_cookie)
20 import LWN.Page (epublish, page_from_url)
21
22
23 -- | If we're given an empty path, return a handle to
24 -- 'stdout'. Otherwise, open the given file and return a read/write
25 -- handle to that.
26 get_output_handle :: FilePath -> IO Handle
27 get_output_handle path =
28 if (null path) then
29 return stdout
30 else
31 openBinaryFile path WriteMode
32
33
34 argument_is_file :: Cfg -> IO Bool
35 argument_is_file cfg = do
36 path <- real_article_path (article cfg)
37 doesFileExist path
38
39 main :: IO ()
40 main = do
41 cfg' <- get_cfg
42 aif <- argument_is_file cfg'
43 cfg <- case aif of
44 False -> get_login_cookie cfg'
45 True -> return cfg'
46
47 page <- page_from_url cfg (article cfg)
48 case page of
49 Just p -> do
50 output_handle <- get_output_handle (output cfg)
51 epublish p output_handle
52 Nothing -> do
53 hPutStrLn stderr "ERROR: could not parse an LWN page from the given URL."
54 _ <- show_help
55 return ()
56
57 -- Necessary, for some reason.
58 stopGlobalPool