customMergeToDir :: (HasEnvironment m) => FilePath -> FilePath -> FilePath -> m MergeStatus
customMergeToDir stb src dir = do
    src_exists <- liftIO $ doesFileExist src
    stb_exists <- liftIO $ doesFileExist stb
    let outFile = joinPath [dir, src]
        outDir  = joinPath $ init $ splitDirectories outFile
        outMod  = concat $ intersperse "." $ splitDirectories $ dropExtension src
        outTitle = "module " ++ outMod ++ " where \n\n"
    case (src_exists, stb_exists) of
        (False, _) -> return $ 
                MergeFailure ["Source file does not exist : "++src]
        (_, False) -> return $ 
                MergeFailure ["Source file does not exist : "++stb]
        _          -> do
                src_str <- liftIO $ readFile src
                stb_str <- liftIO $ readFile stb
                let (stbimps, stbdecls) = span ( not . isPrefixOf "-- SPLIT HERE") $ lines stb_str
                    mrg_str = outTitle ++ (unlines stbimps) ++ src_str ++ (unlines stbdecls)
                liftIO $ createDirectoryIfMissing True outDir
                hdl <- liftIO $ openFile outFile WriteMode  -- overwrite!
                liftIO $ hPutStr hdl mrg_str 
                liftIO $ hClose hdl
                return $ MergeSuccess ReComp [] outFile -- must have recreated file
 


-- | Given a 'CodeType', return the base path to the code (e.g. @CTController@ -> @App/Controllers@).
customMergeToDir :: (HasEnvironment m) => FilePath -> FilePath -> FilePath -> m MergeStatus
customMergeToDir stb src dir = do
    src_exists <- liftIO $ doesFileExist src
    stb_exists <- liftIO $ doesFileExist stb
    let outFile = joinPath [dir, src]
        outDir  = joinPath $ init $ splitDirectories outFile
        outMod  = concat $ intersperse "." $ splitDirectories $ dropExtension src
        outTitle = "module " ++ outMod ++ " where \n\n"
    case (src_exists, stb_exists) of
        (False, _) -> return $ 
                MergeFailure ["Source file does not exist : "++src]
        (_, False) -> return $ 
                MergeFailure ["Source file does not exist : "++stb]
        _          -> do
                src_str <- liftIO $ readFile src
                stb_str <- liftIO $ readFile stb
                let (stbimps, stbdecls) = span ( not . isPrefixOf "-- SPLIT HERE") $ lines stb_str
                    mrg_str = outTitle ++ (unlines stbimps) ++ src_str ++ (unlines stbdecls)
                liftIO $ createDirectoryIfMissing True outDir
                hdl <- liftIO $ openFile outFile WriteMode  -- overwrite!
                liftIO $ hPutStr hdl mrg_str 
                liftIO $ hClose hdl
                return $ MergeSuccess ReComp [] outFile -- must have recreated file
 


-- | Given a 'CodeType', return the base path to the code (e.g. @CTController@ -> @App/Controllers@).
customMergeToDir stb src dir = do
    src_exists <- liftIO $ doesFileExist src
    stb_exists <- liftIO $ doesFileExist stb
    let outFile = joinPath [dir, src]
        outDir  = joinPath $ init $ splitDirectories outFile
        outMod  = concat $ intersperse "." $ splitDirectories $ dropExtension src
        outTitle = "module " ++ outMod ++ " where \n\n"
    case (src_exists, stb_exists) of
        (False, _) -> return $ 
                MergeFailure ["Source file does not exist : "++src]
        (_, False) -> return $ 
                MergeFailure ["Source file does not exist : "++stb]
        _          -> do
                src_str <- liftIO $ readFile src
                stb_str <- liftIO $ readFile stb
                let (stbimps, stbdecls) = span ( not . isPrefixOf "-- SPLIT HERE") $ lines stb_str
                    mrg_str = outTitle ++ (unlines stbimps) ++ src_str ++ (unlines stbdecls)
                liftIO $ createDirectoryIfMissing True outDir
                hdl <- liftIO $ openFile outFile WriteMode  -- overwrite!
                liftIO $ hPutStr hdl mrg_str 
                liftIO $ hClose hdl
                return $ MergeSuccess ReComp [] outFile -- must have recreated file
 


-- | Given a 'CodeType', return the base path to the code (e.g. @CTController@ -> @App/Controllers@).