module Hledger.Cli.Main where
#if defined(WEB) || defined(WEB610)
import System.Info (os)
#endif
#if __GLASGOW_HASKELL__ <= 610
import Prelude hiding (putStr, putStrLn)
import System.IO.UTF8
#endif
import Hledger.Cli.Commands.All
import Hledger.Data
import Hledger.Cli.Options
import Hledger.Cli.Tests
import Hledger.Cli.Utils (withJournalDo)
import Hledger.Cli.Version (versionmsg, binaryfilename)
main :: IO ()
main = do
(opts, cmd, args) <- parseArguments
run cmd opts args
where
run cmd opts args
| Help `elem` opts = putStr help1
| HelpOptions `elem` opts = putStr help2
| HelpAll `elem` opts = putStr $ help1 ++ "\n" ++ help2
| Version `elem` opts = putStrLn versionmsg
| BinaryFilename `elem` opts = putStrLn binaryfilename
| null cmd = maybe (putStr help1) (withJournalDo opts args cmd) defaultcmd
| cmd `isPrefixOf` "balance" = withJournalDo opts args cmd balance
| cmd `isPrefixOf` "convert" = withJournalDo opts args cmd convert
| cmd `isPrefixOf` "print" = withJournalDo opts args cmd print'
| cmd `isPrefixOf` "register" = withJournalDo opts args cmd register
| cmd `isPrefixOf` "histogram" = withJournalDo opts args cmd histogram
| cmd `isPrefixOf` "add" = withJournalDo opts args cmd add
| cmd `isPrefixOf` "stats" = withJournalDo opts args cmd stats
#ifdef VTY
| cmd `isPrefixOf` "vty" = withJournalDo opts args cmd vty
#endif
#if defined(WEB) || defined(WEB610)
| cmd `isPrefixOf` "web" = withJournalDo opts args cmd web
#endif
#ifdef CHART
| cmd `isPrefixOf` "chart" = withJournalDo opts args cmd chart
#endif
| cmd `isPrefixOf` "test" = runtests opts args >> return ()
| otherwise = putStr help1
#if defined(WEB) || defined(WEB610)
defaultcmd | os=="mingw32" = Just web
| otherwise = Nothing
#else
defaultcmd = Nothing
#endif