module Main where

import Data.ByteString.Lazy as B
import Data.Aeson (Value(..), Result(..), encode)
import Language.Virgil (eitherDecode)
import System.Environment (getArgs)
import System.Exit (die)

usage :: String
usage = "USAGE: virgil2json [source file]"

main :: IO ()
main = do
  args <- getArgs
  inp <- case args of
    []         -> B.getContents
    ["-"]      -> B.getContents
    ["--help"] -> die usage
    [f]        -> B.readFile f
    _          -> die usage
  case eitherDecode inp of
    Error err   -> die err
    Success val -> B.putStrLn (encode (val ::Value))
