{-# LANGUAGE RecordWildCards #-} import Options.Applicative import System.FilePath import Data.Char import Trivmix.Types data Options = Options { baseDirectory :: FilePath , targetDirectory :: FilePath , levelFile :: FilePath , adjustment :: Adjustment Level } optionParser :: Parser Options optionParser = Options <$> strOption ( long "base" <> metavar "DIRECTORY" <> value "/dev/shm/mix" <> showDefault ) <*> strOption ( long "target" <> short 't' <> metavar "DIRECTORY" <> help "Directory relative to ‘--base’ containing the level file" ) <*> strOption ( long "level" <> metavar "FILENAME" <> value "level" <> help "Filename of the level file" <> showDefault ) <*> ( ( Set <$> option auto ( long "set" <> short 'o' <> metavar "LEVEL" ) ) <|> ( Add <$> option auto ( long "add" <> short 'a' <> metavar "LEVEL" ) ) <|> ( Sub <$> option auto ( long "sub" <> short 's' <> metavar "LEVEL" ) ) ) main :: IO () main = execParser opts >>= adjmix where opts = info (helper <*> optionParser) ( fullDesc <> progDesc "Adjust the level file of a trivmix" <> header "Adjmix — A trivial interface to a trivial mixer" ) adjmix :: Options -> IO () adjmix Options{..} = do oldLevel <- readFile levelFile' >>= readIO . stripSpace let newLevel = oldLevel `doAdjustment` adjustment writeFile levelFile' (show newLevel ++ "\n") where levelFile' = baseDirectory targetDirectory levelFile stripSpace = reverse . stripSpace' . reverse . stripSpace' stripSpace' [] = [] stripSpace' l@(x:xs) = if isSpace x then stripSpace' xs else l