aboutsummaryrefslogtreecommitdiff
path: root/cmd/Main.hs
blob: 35a42a6b1cb5d23a4187c3e8d74bbb0d271249a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
{-# LANGUAGE QuasiQuotes #-}

module Main
  ( main
  ) where

import Control.Monad (when)
import Data.Version (showVersion)
import System.Environment (getArgs)
import System.Exit (die)

import Data.Aeson (eitherDecode, encode)
import qualified Data.ByteString.Lazy as L
import qualified Data.HashMap.Strict as HM
import System.Console.Docopt.NoTH
       (isPresent, longOption, parseArgsOrExit, parseUsageOrExit, usage)
import Text.InterpolatedString.Perl6 (qc)

import Malodivo.Budget (manyToMany)

import qualified Main.Input as I
import qualified Main.Output as O
import Malodivo.Types.District (di2df)
import Paths_malodivo (version) -- from cabal

usageHelp :: String
usageHelp =
  "malodivo " ++
  showVersion version ++
  " - budget planning tool for the Kingdom of Malodivo" ++
  [qc|

This utility reads input JSON data from standard input
and writes output JSON data to standard output.

Usage: malodivo [options] < input.json > output.json

Options:

  -h, --help               Show this message and exit

|]

process :: IO ()
process = do
  input <- L.getContents
  case eitherDecode input of
    Left err -> die err
    Right si ->
      let allBills = I.bills si
          suppliedFunds = di2df $ I.districts si
      in do when (HM.null suppliedFunds) $ die "We needs at least one district"
            L.putStr . encode . O.encode $ manyToMany suppliedFunds allBills

main :: IO ()
main = do
  doco <- parseUsageOrExit usageHelp
  args <- parseArgsOrExit doco =<< getArgs
  if args `isPresent` longOption "help"
    then putStrLn $ usage doco
    else process