
import System
import System.IO

import Parse

withLines :: Handle -> (String -> IO ()) -> IO ()
withLines h f = do
    eof <- hIsEOF h
    if eof
        then return ()
        else hGetLine h >>= f >> withLines h f

formatPair (a, b) = concat [".", a, " { fill: #", b, "; }\n"]

makeCSS  = concat . map formatPair

main = do
    [svgFile, colorsFile] <- getArgs
    colors <- readFile colorsFile
    withParse parseCommaPairs colors $ \pairs -> do
        h <- openFile svgFile ReadMode
        withLines h $ \line -> do
            if line == "</style>\r"
                then putStrLn (makeCSS pairs)
                else return ()
            putStrLn line

