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