content: #!/usr/bin/env stack
  {- stack --install-ghc
      runghc
      --package amqp
      --package bytestring
  -}
  {-# LANGUAGE OverloadedStrings #-}
  
  import Network.AMQP
  
  import qualified Data.ByteString.Lazy.Char8 as BL
  import           Data.Monoid ((<>))
  import           Control.Concurrent (threadDelay)
  
  logsExchange = "logs"
         
  main :: IO ()
  main = do
       conn <- openConnection "127.0.0.1" "/" "guest" "guest"
       ch   <- openChannel conn
  
       declareExchange ch newExchange {exchangeName    = logsExchange,
                                       exchangeType    = "fanout",
                                       exchangeDurable = False}
       (q, _, _) <- declareQueue ch newQueue {queueName       = "",
                                              queueAutoDelete = True,
                                              queueDurable    = False}
       bindQueue ch q logsExchange ""
  
       BL.putStrLn " [*] Waiting for messages. To exit press CTRL+C"
       consumeMsgs ch q Ack deliveryHandler
  
       -- waits for keypresses
       getLine
       closeConnection conn
  
  deliveryHandler :: (Message, Envelope) -> IO ()
  deliveryHandler (msg, metadata) = do
    BL.putStrLn $ " [x] Received " <> body
    threadDelay (1000000 * n)
    BL.putStrLn " [x] Done"
    ackEnv metadata
    where
      body = msgBody msg
      n    = countDots body
  
  countDots :: BL.ByteString -> Int
  countDots = fromIntegral . BL.count '.'
  