aboutsummaryrefslogtreecommitdiff
path: root/src/Sound/MusicDirTrans/Directory.hs
blob: 0cc1b8fc2030e9e0de79faac3a67852ef7bd9c90 (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
{-# LANGUAGE OverloadedStrings  #-}
module Sound.MusicDirTrans.Directory
        ( copyToNewPath
        , getTracksInDir
        , genArtistPathFromTracks
        , mkNewDirs
        , mkRevertFile
        , mkRevertArtistPath
        , revertFile
        )
where
import           Data.Monoid
import           Data.List
import           Data.Text                      ( Text
                                                , append
                                                , pack
                                                )
import           Control.Monad.IO.Class
import           Distribution.Simple.Utils
import           Distribution.Verbosity
import           Sound.MusicDirTrans.File
import           Sound.MusicDirTrans.Type
import           Sound.HTagLib
import           System.Directory
import           System.FilePath.Glob
import           System.FilePath.Posix



revertFile :: FilePath
revertFile = "dir.orig.name.txt"

getTracksInDir :: FilePath -> IO [AudioTrack]
getTracksInDir path =
        map AudioTrack <$> (concat <$> globDir trackFilePatterns path)

mkNewDirs :: ArtistPath -> IO ()
mkNewDirs (ArtistPath _ p c) =
        createDirectoryIfMissing False p
                >> createDirectoryIfMissing False (p ++ "/" ++ c)

genArtistPathFromTracks :: FilePath -> IO ArtistPath
genArtistPathFromTracks path = do
        ats      <- getTracksInDir path
        metadata <- mapM (getMetadata . currentPath) ats
        return $ mkArtistPath path metadata

copyToNewPath :: ArtistPath -> IO ()
copyToNewPath ap = copyDirectoryRecursive verbosity (rootPath ap) (newPath ap)
        where verbosity = normal

mkRevertFile :: ArtistPath -> IO ()
mkRevertFile ap = writeFile filePath fileContent
    where
        filePath    = newPath ap ++ revertFile
        fileContent = last $ splitDirectories $ rootPath ap

mkRevertArtistPath :: FilePath -> FilePath -> ArtistPath
mkRevertArtistPath p op = ArtistPath p "" rp
    where
        rrp = intercalate "/" . init $ splitDirectories p
        rp  = rrp ++ "/" ++ op