-- | Snaplet initialization
initSass :: SnapletInit b Sass
initSass = makeSnaplet "sass" description datadir $ do
  config <- getSnapletUserConfig
  fp <- getSnapletFilePath

  (opts, errs) <- runWriterT $ do
    cmStr <- logErr "Must specify compileMode" $ C.lookup config "compileMode"
    cm    <- case cmStr of
                  Just x -> logErr "Invalid compileMode" . return $ compileModeFromString x
                  Nothing -> return Nothing
    stStr <- logErr "Must specify style" $ C.lookup config "style"
    st    <- case stStr of
                  Just x  -> logErr "Invalid style" . return $ styleFromString x
                  Nothing -> return Nothing
    sm    <- logErr "Must specify sourcemap" $ C.lookup config "sourcemap"
    v     <- logErr "Must specify verbose" $ C.lookup config "verbose"
    return (cm, st, sm, v)

  let sass = case opts of
              (Just cm, Just st, Just sm, Just v) ->
                Sass
                  { snapletFilePath = fp
                  , compileMode     = cm
                  , style           = st
                  , sourcemap       = sm
                  , verbose         = v
                  }
              _ -> error $ intercalate "\n" errs

  -- Make sure snaplet/sass, snaplet/sass/src, snaplet/sass/css are present.
  liftIO $ mapM_ createDirUnlessExists [fp, srcDir sass, destDir sass]

  when (Production == compileMode sass) (liftIO $ compileAll sass)

  return sass

  where
    datadir = Just $ liftM (++ "/resources") getDataDir

    description = "Automatic (re)compilation and serving of Sass files"

    logErr :: MonadIO m => t -> IO (Maybe a) -> WriterT [t] m (Maybe a)
    logErr err m = do
        res <- liftIO m
        when (isNothing res) (tell [err])
        return res


-- | Serves the compiled Fay scripts using the chosen compile mode.
initSass :: SnapletInit b Sass
initSass = makeSnaplet "sass" description datadir $ do
  config <- getSnapletUserConfig
  fp <- getSnapletFilePath

  (opts, errs) <- runWriterT $ do
    cmStr <- logErr "Must specify compileMode" $ C.lookup config "compileMode"
    cm    <- case cmStr of
                  Just x -> logErr "Invalid compileMode" . return $ compileModeFromString x
                  Nothing -> return Nothing
    stStr <- logErr "Must specify style" $ C.lookup config "style"
    st    <- case stStr of
                  Just x  -> logErr "Invalid style" . return $ styleFromString x
                  Nothing -> return Nothing
    sm    <- logErr "Must specify sourcemap" $ C.lookup config "sourcemap"
    v     <- logErr "Must specify verbose" $ C.lookup config "verbose"
    return (cm, st, sm, v)

  let sass = case opts of
              (Just cm, Just st, Just sm, Just v) ->
                Sass
                  { snapletFilePath = fp
                  , compileMode     = cm
                  , style           = st
                  , sourcemap       = sm
                  , verbose         = v
                  }
              _ -> error $ intercalate "\n" errs

  -- Make sure snaplet/sass, snaplet/sass/src, snaplet/sass/css are present.
  liftIO $ mapM_ createDirUnlessExists [fp, srcDir sass, destDir sass]

  when (Production == compileMode sass) (liftIO $ compileAll sass)

  return sass

  where
    datadir = Just $ liftM (++ "/resources") getDataDir

    description = "Automatic (re)compilation and serving of Sass files"

    logErr :: MonadIO m => t -> IO (Maybe a) -> WriterT [t] m (Maybe a)
    logErr err m = do
        res <- liftIO m
        when (isNothing res) (tell [err])
        return res


-- | Serves the compiled Fay scripts using the chosen compile mode.
initSass = makeSnaplet "sass" description datadir $ do
  config <- getSnapletUserConfig
  fp <- getSnapletFilePath

  (opts, errs) <- runWriterT $ do
    cmStr <- logErr "Must specify compileMode" $ C.lookup config "compileMode"
    cm    <- case cmStr of
                  Just x -> logErr "Invalid compileMode" . return $ compileModeFromString x
                  Nothing -> return Nothing
    stStr <- logErr "Must specify style" $ C.lookup config "style"
    st    <- case stStr of
                  Just x  -> logErr "Invalid style" . return $ styleFromString x
                  Nothing -> return Nothing
    sm    <- logErr "Must specify sourcemap" $ C.lookup config "sourcemap"
    v     <- logErr "Must specify verbose" $ C.lookup config "verbose"
    return (cm, st, sm, v)

  let sass = case opts of
              (Just cm, Just st, Just sm, Just v) ->
                Sass
                  { snapletFilePath = fp
                  , compileMode     = cm
                  , style           = st
                  , sourcemap       = sm
                  , verbose         = v
                  }
              _ -> error $ intercalate "\n" errs

  -- Make sure snaplet/sass, snaplet/sass/src, snaplet/sass/css are present.
  liftIO $ mapM_ createDirUnlessExists [fp, srcDir sass, destDir sass]

  when (Production == compileMode sass) (liftIO $ compileAll sass)

  return sass

  where
    datadir = Just $ liftM (++ "/resources") getDataDir

    description = "Automatic (re)compilation and serving of Sass files"

    logErr :: MonadIO m => t -> IO (Maybe a) -> WriterT [t] m (Maybe a)
    logErr err m = do
        res <- liftIO m
        when (isNothing res) (tell [err])
        return res


-- | Serves the compiled Fay scripts using the chosen compile mode.