aboutsummaryrefslogtreecommitdiff
path: root/cmd/Main.hs
blob: 7af6a8339cd7da5384379f4d8ca1a9c6cf174656 (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
62
63
64
65
{-# LANGUAGE QuasiQuotes #-}

module Main
  ( main
  ) where

import Control.Monad (when)
import Data.Maybe (fromMaybe)
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 (manyToManyLimited)
import Malodivo.Types.District (di2df)
import Malodivo.Types.Ministry (mi2ml)

import qualified Main.Input as I
import qualified Main.Output as O
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
          ministryLimits = mi2ml $ fromMaybe [] (I.ministries si)
      in do when (HM.null suppliedFunds) $ die "We needs at least one district"
            L.putStr . encode . O.encode $
              manyToManyLimited suppliedFunds ministryLimits allBills

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