module Billboard.IOUtils (bbdir, getBBFiles, getBBFile) where
import System.Directory
import System.FilePath
import Text.Printf (printf)
import Control.Monad (filterM)
bbdir :: (FilePath -> IO a) -> FilePath -> IO [a]
bbdir f fp = getDirectoryContents fp >>= filterM isBillboardDir
>>= mapM (\d -> f (fp </> d </> "salami_chords.txt"))
getBBFiles :: FilePath -> IO [(FilePath, Int)]
getBBFiles p = getDirectoryContents p >>= filterM isBillboardDir
>>= mapM (\d -> return (p </> d </> "salami_chords.txt", read d))
isBillboardDir :: String -> IO Bool
isBillboardDir x = case length x of
4 -> do let n = read x :: Int
return (n >= 0 && n <= 1000)
_ -> return False
getBBFile :: FilePath -> Int -> IO (FilePath)
getBBFile billboardLoc nr =
do let fp = billboardLoc </> printf "%04d" nr </> "salami_chords.txt"
fpExist <- doesFileExist fp
case fpExist of
True -> return fp
False -> error ("Error: " ++ printf "%04d" nr
++ " is not a valid billboard id, or the directory "
++ billboardLoc ++"does not point to the billboard collection"
++ " Regardless, the file " ++ fp ++ " does not exist" )