aboutsummaryrefslogtreecommitdiff
path: root/app/RunCmd.hs
blob: 40079677afa79f3ad7adc0868c9d8d97c805085c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
{-# 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."