{-# LANGUAGE OverloadedStrings #-} module RunCmd ( runCmd ) where import Options.Applicative import Data.Semigroup ( (<>) ) import Data.Text import System.Directory import System.FilePath.Posix import System.Exit import Sound.MusicDirTrans data Options = Options { revert :: Bool , inputPath :: !FilePath } deriving Show options :: Parser Options options = Options <$> switch ( long "revert" <> short 'r' <> help "Revert the Directory name based on the dir.orig.name.txt file" ) <*> strArgument (metavar "DIR" <> help "Directory to transform") optsInfo :: ParserInfo Options optsInfo = info (options <**> helper) ( fullDesc <> progDesc "Transform an Album directory into an Artist -> Album structure" <> header "mds - metadata based directory transformer" ) runCmd :: IO () runCmd = do opts <- execParser optsInfo dir <- makeAbsolute $ inputPath opts dirExist <- doesDirectoryExist dir if dirExist then do let rev = revert opts if rev then runRevert dir else do paths <- runCreate dir runCopy paths runRevertFile paths exitSuccess else missingDir dir >> exitFailure runCreate :: FilePath -> IO ArtistPath runCreate path = do paths <- genArtistPathFromTracks path mkNewDirs paths return paths runCopy :: ArtistPath -> IO () runCopy = copyToNewPath runRevertFile :: ArtistPath -> IO () runRevertFile = mkRevertFile runRevert dir = do let fpath = dir ++ revertFile fileExist <- doesFileExist fpath if fileExist then do origPath <- readFile fpath let ap = mkRevertArtistPath dir origPath createDirectoryIfMissing False (childPath ap) copyToNewPath ap removeFile $ childPath ap ++ "/" ++ revertFile exitSuccess else missingFile dir >> exitFailure missingDir :: FilePath -> IO () missingDir dir = putStrLn errorMsg where errorMsg = "mds can't find the given directory: " ++ dir ++ " does not exist. Goodbye." missingFile :: FilePath -> IO () missingFile dir = putStrLn errorMsg where errorMsg = "mds can't find dir.orig.name.txt file in the given directory:" ++ " dir.orig.name.txt does not exist in " ++ dir ++ " . Goodbye."