{-# LANGUAGE FlexibleInstances #-}

-- | A module for representing data types that can be compiled by taking a list of files.
module Lastik.Compile (
                       Compile,
                       compile,
                       environmentCommand
                      ) where

import Control.Monad
import Data.List
import Data.Maybe
import System.Directory
import System.Cmd
import System.Exit
import System.Environment
import System.FilePath

-- | A class of compilable data types.
class Compile c where
  compile :: c -> [FilePath] -> String

instance Compile [Char] where
  compile s _ = s

instance Compile ([FilePath] -> String) where
  compile = id

environmentCommand :: String -> String -> IO String
environmentCommand v c = (\e -> (case lookup v e of Nothing -> c
                                                    Just k -> k </> c) ++ " ") `fmap` getEnvironment