-- -------------------------------------------------------------------
-- Copyright (C) 2017 by Sascha Wilde <wilde@sha-bang.de>

-- This program is free software under the GNU GPL (>=v2)
-- Read the file COPYING coming with the software for details.
-- -------------------------------------------------------------------

-- | 
-- = dihaa
-- Convert simple ASCII art diagrams to nice images.
-- 
-- == Usage:
-- >  Usage: dihaa [OPTION...] files...
-- >    -p  --png   Output as PNG file.
-- >    -u  --utf8  Output as UTF-8 to stdout.
-- 
-- 
-- == Drawing
-- The input file(s) can contain certain “graphical” ASCII art
-- elements, which will be converted to real graphics by dihaa.
-- Everything else wil just be rendered as plain text.
-- 
-- === The recognised ASCII art elements are:
-- 
-- Boxes can be drawn using @|@ and @-@ with @+@ as corners:
-- 
-- >  +-------+
-- >  | A box |
-- >  +-------+
-- generates:
-- 
-- <<../examples/box.dihaa.png>>
-- 
-- Lines can be drawn with the same characters, where @+@ can be used
-- for corners, tees and crossings:
-- 
-- >  --- | ---+     --+--    |
-- >      |    |       |    --+--
-- >      |    +-------+      |
-- generates:
-- 
-- <<../examples/lines.dihaa.png>>
-- 
-- Using @<@, @>@, @^@ and @v@ as heads, lines become Arrows:
-- 
-- >  --> ^ ---+     <-+->    ^
-- >      |    |       |    <-+->
-- >      |    +-------+      v
-- generates:
-- 
-- <<../examples/arrows.dihaa.png>>
-- 
-- 
-- == Styles
-- 
-- To create colored boxes you may put a special string `:#RGB` with RGB
-- being three single digit hexadezimal numbers into a box, like so:
-- 
-- >   +----------------------+
-- >   |:#fe8                 |
-- >   | Nice warm yellow box |
-- >   |                      |
-- >   +----------------------+
-- generates:
-- 
-- <<../examples/color.dihaa.png>>
-- 
-- 
-- == Example:
-- The input
-- 
-- >  Example 1
-- >                                     
-- >  +----------------------+           
-- >  | This is a simple box +----+---- + Color Code ":#0f0"
-- >  +----------------------+    |      
-- >                              |      
-- >                              v      
-- >                        +-----------+
-- >                        |:#0f0      |
-- >                        | Green Box |
-- >                        |           |
-- >                        +-----------+
-- 
-- produces this image (using the @-p@ option):
-- 
-- <<../examples/example1.dihaa.png>>


module Main where

import Dihaa
import Dihaa.OutputPNG
import Dihaa.OutputUTF8
import System.Console.GetOpt
import System.Environment (getArgs, getProgName)
import System.IO (stderr, hPutStrLn, readFile)

data Options = Options
  { optOutUTF8 :: Bool,
    optOutPNG :: Bool
  } deriving Show

defaultOptions :: Options
defaultOptions = Options
  { optOutUTF8 = False,
    optOutPNG = False
  }

options :: [OptDescr (Options -> Options)]
options =
  [ Option ['p'] ["png"]
    (NoArg (\opts -> opts { optOutPNG = True }))
    "Output as PNG file."
  , Option ['u'] ["utf8"]
    (NoArg (\opts -> opts { optOutUTF8 = True }))
    "Output as UTF-8 to stdout."
  ]

compilerOpts :: [String] -> IO (Options, [String])
compilerOpts argv =
  case getOpt Permute options argv of
    (o,s,[]  ) -> return $ (foldl (flip id) defaultOptions o, s)
    (_,_,errs) -> do
      h <- header
      ioError (userError (concat errs ++ usageInfo h options))
  where header = do
          prg <- getProgName
          return ("Usage: " ++ prg ++ " [OPTION...] files...")

main :: IO ()
main = do
  args <- getArgs
  (opts, argv) <- compilerOpts args
  mapM_ (doFile opts) argv
  where
    doFile opts name = do
      f <- readFile name
      if optOutPNG opts
        then
        outputFilePNG (name ++ ".png") $ text2dia $ lines2text $ lines f
        else if optOutUTF8 opts
             then
               putStrLn $ outputUTF8 $ text2dia $ lines2text $ lines f
             else
               hPutStrLn stderr "You should choose an output mode -u or -p"