-- | Run @futhark doc@.
main :: String -> [String] -> IO ()
main = mainWithOptions initialDocConfig commandLineOptions "options... -o outdir programs..." f
  where
    f [dir] config = Just $ do
      res <- runFutharkM (m config dir) Verbose
      case res of
        Left err -> liftIO $ do
          dumpError newFutharkConfig err
          exitWith $ ExitFailure 2
        Right () ->
          return ()
    f _ _ = Nothing

    m :: DocConfig -> FilePath -> FutharkM ()
    m config dir =
      case docOutput config of
        Nothing -> liftIO $ do
          hPutStrLn stderr "Must specify output directory with -o."
          exitWith $ ExitFailure 1
        Just outdir -> do
          files <- liftIO $ futFiles dir
          when (docVerbose config) $
            liftIO $ do
              mapM_ (hPutStrLn stderr . ("Found source file " <>)) files
              hPutStrLn stderr "Reading files..."
          (_w, imports, _vns) <- readProgramFiles [] files
          liftIO $ printDecs config outdir files $ nubBy sameImport imports

    sameImport (x, _) (y, _) = x == y
main :: String -> [String] -> IO ()
main = mainWithOptions initialDocConfig commandLineOptions "options... -o outdir programs..." f
  where
    f [dir] config = Just $ do
      res <- runFutharkM (m config dir) Verbose
      case res of
        Left err -> liftIO $ do
          dumpError newFutharkConfig err
          exitWith $ ExitFailure 2
        Right () ->
          return ()
    f _ _ = Nothing

    m :: DocConfig -> FilePath -> FutharkM ()
    m config dir =
      case docOutput config of
        Nothing -> liftIO $ do
          hPutStrLn stderr "Must specify output directory with -o."
          exitWith $ ExitFailure 1
        Just outdir -> do
          files <- liftIO $ futFiles dir
          when (docVerbose config) $
            liftIO $ do
              mapM_ (hPutStrLn stderr . ("Found source file " <>)) files
              hPutStrLn stderr "Reading files..."
          (_w, imports, _vns) <- readProgramFiles [] files
          liftIO $ printDecs config outdir files $ nubBy sameImport imports

    sameImport (x, _) (y, _) = x == y
main = mainWithOptions initialDocConfig commandLineOptions "options... -o outdir programs..." f
  where
    f [dir] config = Just $ do
      res <- runFutharkM (m config dir) Verbose
      case res of
        Left err -> liftIO $ do
          dumpError newFutharkConfig err
          exitWith $ ExitFailure 2
        Right () ->
          return ()
    f _ _ = Nothing

    m :: DocConfig -> FilePath -> FutharkM ()
    m config dir =
      case docOutput config of
        Nothing -> liftIO $ do
          hPutStrLn stderr "Must specify output directory with -o."
          exitWith $ ExitFailure 1
        Just outdir -> do
          files <- liftIO $ futFiles dir
          when (docVerbose config) $
            liftIO $ do
              mapM_ (hPutStrLn stderr . ("Found source file " <>)) files
              hPutStrLn stderr "Reading files..."
          (_w, imports, _vns) <- readProgramFiles [] files
          liftIO $ printDecs config outdir files $ nubBy sameImport imports

    sameImport (x, _) (y, _) = x == y