module Makedo.Contained where

import Makedo.Target
import System.FilePath

-- | redo 0.06 and under do not like it if you try to redo something that's
--   in a directory that does not yet exist, so unfortunately, we need some way
--   to create the parent directories before invoking the targets
data Contained = Contained
  { containers :: [FilePath]
  , targets    :: [ExactTarget]
  , allTargets :: [Target]      -- ^ should be superset of targets
  }

-- | Just one of the possible 'a' used to generate a path component
--   which will then just be chopped off again with takeDirectory
class Containable a where
  czero       :: a

mkContained :: Containable b
            => [b -> ExactTarget]
            -> [b] -- ^ possibilities
            -> Contained
mkContained fs xs =
  Contained { containers = map (\f -> takeDirectory . ePath $ f czero) fs
            , targets    = ts
            , allTargets = map target ts
            }
 where
  ts = [ f x | f <- fs, x <- xs ]