module Main (main) where

import Cardano.Crypto.Init (cryptoInit)
import Cardano.Tools.DBTruncater.Run
import Cardano.Tools.DBTruncater.Types
import DBAnalyser.Parsers (BlockType (..))
import qualified DBTruncater.Parsers as DBTruncater
import Main.Utf8 (withStdTerminalHandles)
import Options.Applicative
  ( execParser
  , fullDesc
  , helper
  , info
  , progDesc
  , (<**>)
  )
import Ouroboros.Consensus.Storage.ImmutableDB.Impl ()
import Prelude hiding (truncate)

main :: IO ()
IO ()
main = IO () -> IO ()
forall (m :: * -> *) r. (MonadIO m, MonadMask m) => m r -> m r
withStdTerminalHandles (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
  IO ()
cryptoInit
  (conf, blocktype) <- IO (DBTruncaterConfig, BlockType)
getCommandLineConfig
  case blocktype of
    ByronBlock ByronBlockArgs
args -> DBTruncaterConfig -> ByronBlockArgs -> IO ()
forall block.
(RunNode block, HasProtocolInfo block) =>
DBTruncaterConfig -> Args block -> IO ()
truncate DBTruncaterConfig
conf ByronBlockArgs
args
    ShelleyBlock ShelleyBlockArgs
args -> DBTruncaterConfig -> ShelleyBlockArgs -> IO ()
forall block.
(RunNode block, HasProtocolInfo block) =>
DBTruncaterConfig -> Args block -> IO ()
truncate DBTruncaterConfig
conf ShelleyBlockArgs
args
    CardanoBlock CardanoBlockArgs
args -> DBTruncaterConfig -> CardanoBlockArgs -> IO ()
forall block.
(RunNode block, HasProtocolInfo block) =>
DBTruncaterConfig -> Args block -> IO ()
truncate DBTruncaterConfig
conf CardanoBlockArgs
args

getCommandLineConfig :: IO (DBTruncaterConfig, BlockType)
getCommandLineConfig :: IO (DBTruncaterConfig, BlockType)
getCommandLineConfig = ParserInfo (DBTruncaterConfig, BlockType)
-> IO (DBTruncaterConfig, BlockType)
forall a. ParserInfo a -> IO a
execParser ParserInfo (DBTruncaterConfig, BlockType)
opts
 where
  opts :: ParserInfo (DBTruncaterConfig, BlockType)
opts =
    Parser (DBTruncaterConfig, BlockType)
-> InfoMod (DBTruncaterConfig, BlockType)
-> ParserInfo (DBTruncaterConfig, BlockType)
forall a. Parser a -> InfoMod a -> ParserInfo a
info
      (Parser (DBTruncaterConfig, BlockType)
DBTruncater.commandLineParser Parser (DBTruncaterConfig, BlockType)
-> Parser
     ((DBTruncaterConfig, BlockType) -> (DBTruncaterConfig, BlockType))
-> Parser (DBTruncaterConfig, BlockType)
forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> Parser
  ((DBTruncaterConfig, BlockType) -> (DBTruncaterConfig, BlockType))
forall a. Parser (a -> a)
helper)
      (InfoMod (DBTruncaterConfig, BlockType)
forall a. InfoMod a
fullDesc InfoMod (DBTruncaterConfig, BlockType)
-> InfoMod (DBTruncaterConfig, BlockType)
-> InfoMod (DBTruncaterConfig, BlockType)
forall a. Semigroup a => a -> a -> a
<> String -> InfoMod (DBTruncaterConfig, BlockType)
forall a. String -> InfoMod a
progDesc String
"Utility for truncating an ImmutableDB")