checkReloadCode ct cmap cstat cl = do
    debugM $ "    CodeStore : checkReloadCode : loading   " ++ (fst cl) ++ " - " ++ (snd cl)
    (exists, reload) <- needReloadCode (fst cl) (getDate cstat)
    case (exists, reload) of
        (False, _)    -> do debugM $ "    CodeStore : checkReloadCode : Code missing"
                            return $ insert cl CodeLoadMissing cmap
        (True, False) -> do debugM $ "    CodeStore : checkReloadCode : No reload neeeded"
                            return cmap
        (True, True)  -> do debugM $ "    CodeStore : checkReloadCode : Need reload"
                            loadCode ct cmap cl
  where needReloadCode :: (HasEnvironment m) => FilePath -> CodeDate -> m (Bool, Bool)
        needReloadCode fp fd = do
            fe <- liftIO $ doesFileExist fp
            case fe of
                True -> do TOD mt _ <- liftIO $ getModificationTime fp    
                           return $ (True, fromIntegral mt > utcTimeToPOSIXSeconds fd)
                False-> return (False, True)

        
--------------------------------------------------------------------------
-- The beast
--------------------------------------------------------------------------
       
-- | Begins the code load process, which comprises merging the code with
-- the appropriate Stub (in Turbinado/Stubs) to the tmp/compiled directory,
-- making the code, and loading it. 
checkReloadCode ct cmap cstat cl = do
    debugM $ "    CodeStore : checkReloadCode : loading   " ++ (fst cl) ++ " - " ++ (snd cl)
    (exists, reload) <- needReloadCode (fst cl) (getDate cstat)
    case (exists, reload) of
        (False, _)    -> do debugM $ "    CodeStore : checkReloadCode : Code missing"
                            return $ insert cl CodeLoadMissing cmap
        (True, False) -> do debugM $ "    CodeStore : checkReloadCode : No reload neeeded"
                            return cmap
        (True, True)  -> do debugM $ "    CodeStore : checkReloadCode : Need reload"
                            loadCode ct cmap cl
  where needReloadCode :: (HasEnvironment m) => FilePath -> CodeDate -> m (Bool, Bool)
        needReloadCode fp fd = do
            fe <- liftIO $ doesFileExist fp
            case fe of
                True -> do TOD mt _ <- liftIO $ getModificationTime fp    
                           return $ (True, fromIntegral mt > utcTimeToPOSIXSeconds fd)
                False-> return (False, True)

        
--------------------------------------------------------------------------
-- The beast
--------------------------------------------------------------------------
       
-- | Begins the code load process, which comprises merging the code with
-- the appropriate Stub (in Turbinado/Stubs) to the tmp/compiled directory,
-- making the code, and loading it. 
checkReloadCode ct cmap cstat cl = do
    debugM $ "    CodeStore : checkReloadCode : loading   " ++ (fst cl) ++ " - " ++ (snd cl)
    (exists, reload) <- needReloadCode (fst cl) (getDate cstat)
    case (exists, reload) of
        (False, _)    -> do debugM $ "    CodeStore : checkReloadCode : Code missing"
                            return $ insert cl CodeLoadMissing cmap
        (True, False) -> do debugM $ "    CodeStore : checkReloadCode : No reload neeeded"
                            return cmap
        (True, True)  -> do debugM $ "    CodeStore : checkReloadCode : Need reload"
                            loadCode ct cmap cl
  where needReloadCode :: (HasEnvironment m) => FilePath -> CodeDate -> m (Bool, Bool)
        needReloadCode fp fd = do
            fe <- liftIO $ doesFileExist fp
            case fe of
                True -> do TOD mt _ <- liftIO $ getModificationTime fp    
                           return $ (True, fromIntegral mt > utcTimeToPOSIXSeconds fd)
                False-> return (False, True)

        
--------------------------------------------------------------------------
-- The beast
--------------------------------------------------------------------------
       
-- | Begins the code load process, which comprises merging the code with
-- the appropriate Stub (in Turbinado/Stubs) to the tmp/compiled directory,
-- making the code, and loading it. 