retrieveCode :: (HasEnvironment m) => CodeType -> CodeLocation -> m CodeStatus
retrieveCode ct cl' = do
    e <- getEnvironment
    let (CodeStore mv) = fromJust' "CodeStore: retrieveCode" $ getCodeStore e
        path  = getDir ct
    cl <- return (addExtension (joinPath $ map normalise [path, dropExtension $ fst cl']) "hs", snd cl')
    debugM $ "  CodeStore : retrieveCode : loading   " ++ (fst cl) ++ " - " ++ (snd cl)
    cmap <- liftIO $ takeMVar mv
    let c= lookup cl cmap
    cmap' <- case c of
               Nothing                  -> do debugM ((fst cl) ++ " : " ++ (snd cl) ++ " : fresh load")
                                              loadCode ct cmap cl
               Just (CodeLoadFailure _) -> do debugM ((fst cl) ++ " : " ++ (snd cl) ++ " : previous failure; try load") 
                                              loadCode ct cmap cl
               _                        -> do debugM ((fst cl) ++ " : " ++ (snd cl) ++ " : checking reload") 
                                              checkReloadCode ct cmap (fromJust' "CodeStore: retrieveCode2" c) cl
    liftIO $ putMVar mv cmap'
    -- We _definitely_ have a code entry now, though it may have a MakeFailure
    let c' = lookup cl cmap'
    case c' of
        Nothing                             -> do debugM (fst cl ++ " : Not found in CodeStore") 
                                                  return  CodeLoadMissing
        Just CodeLoadMissing                -> do debugM (fst cl ++ " : Not found in CodeStore") 
                                                  return  CodeLoadMissing
        Just (CodeLoadFailure e)            -> do debugM (fst cl ++ " : CodeLoadFailure " ) 
                                                  return (CodeLoadFailure e)
        Just clc@(CodeLoadController   _ _) -> do debugM (fst cl ++ " : CodeLoadController " ) 
                                                  return clc  
        Just clv@(CodeLoadView         _ _) -> do debugM (fst cl ++ " : CodeLoadView" ) 
                                                  return clv
        Just clc@(CodeLoadComponentController   _ _) -> do debugM (fst cl ++ " : CodeLoadComponentController " ) 
                                                           return clc  
        Just clv@(CodeLoadComponentView         _ _) -> do debugM (fst cl ++ " : CodeLoadComponentView" ) 
                                                           return clv
        
-- | Checks to see if the file exists and if the file is newer than the loaded function.  If the 
-- code needs to be reloaded, then 'loadCode' will be called.
retrieveCode :: (HasEnvironment m) => CodeType -> CodeLocation -> m CodeStatus
retrieveCode ct cl' = do
    e <- getEnvironment
    let (CodeStore mv) = fromJust' "CodeStore: retrieveCode" $ getCodeStore e
        path  = getDir ct
    cl <- return (addExtension (joinPath $ map normalise [path, dropExtension $ fst cl']) "hs", snd cl')
    debugM $ "  CodeStore : retrieveCode : loading   " ++ (fst cl) ++ " - " ++ (snd cl)
    cmap <- liftIO $ takeMVar mv
    let c= lookup cl cmap
    cmap' <- case c of
               Nothing                  -> do debugM ((fst cl) ++ " : " ++ (snd cl) ++ " : fresh load")
                                              loadCode ct cmap cl
               Just (CodeLoadFailure _) -> do debugM ((fst cl) ++ " : " ++ (snd cl) ++ " : previous failure; try load") 
                                              loadCode ct cmap cl
               _                        -> do debugM ((fst cl) ++ " : " ++ (snd cl) ++ " : checking reload") 
                                              checkReloadCode ct cmap (fromJust' "CodeStore: retrieveCode2" c) cl
    liftIO $ putMVar mv cmap'
    -- We _definitely_ have a code entry now, though it may have a MakeFailure
    let c' = lookup cl cmap'
    case c' of
        Nothing                             -> do debugM (fst cl ++ " : Not found in CodeStore") 
                                                  return  CodeLoadMissing
        Just CodeLoadMissing                -> do debugM (fst cl ++ " : Not found in CodeStore") 
                                                  return  CodeLoadMissing
        Just (CodeLoadFailure e)            -> do debugM (fst cl ++ " : CodeLoadFailure " ) 
                                                  return (CodeLoadFailure e)
        Just clc@(CodeLoadController   _ _) -> do debugM (fst cl ++ " : CodeLoadController " ) 
                                                  return clc  
        Just clv@(CodeLoadView         _ _) -> do debugM (fst cl ++ " : CodeLoadView" ) 
                                                  return clv
        Just clc@(CodeLoadComponentController   _ _) -> do debugM (fst cl ++ " : CodeLoadComponentController " ) 
                                                           return clc  
        Just clv@(CodeLoadComponentView         _ _) -> do debugM (fst cl ++ " : CodeLoadComponentView" ) 
                                                           return clv
        
-- | Checks to see if the file exists and if the file is newer than the loaded function.  If the 
-- code needs to be reloaded, then 'loadCode' will be called.
retrieveCode ct cl' = do
    e <- getEnvironment
    let (CodeStore mv) = fromJust' "CodeStore: retrieveCode" $ getCodeStore e
        path  = getDir ct
    cl <- return (addExtension (joinPath $ map normalise [path, dropExtension $ fst cl']) "hs", snd cl')
    debugM $ "  CodeStore : retrieveCode : loading   " ++ (fst cl) ++ " - " ++ (snd cl)
    cmap <- liftIO $ takeMVar mv
    let c= lookup cl cmap
    cmap' <- case c of
               Nothing                  -> do debugM ((fst cl) ++ " : " ++ (snd cl) ++ " : fresh load")
                                              loadCode ct cmap cl
               Just (CodeLoadFailure _) -> do debugM ((fst cl) ++ " : " ++ (snd cl) ++ " : previous failure; try load") 
                                              loadCode ct cmap cl
               _                        -> do debugM ((fst cl) ++ " : " ++ (snd cl) ++ " : checking reload") 
                                              checkReloadCode ct cmap (fromJust' "CodeStore: retrieveCode2" c) cl
    liftIO $ putMVar mv cmap'
    -- We _definitely_ have a code entry now, though it may have a MakeFailure
    let c' = lookup cl cmap'
    case c' of
        Nothing                             -> do debugM (fst cl ++ " : Not found in CodeStore") 
                                                  return  CodeLoadMissing
        Just CodeLoadMissing                -> do debugM (fst cl ++ " : Not found in CodeStore") 
                                                  return  CodeLoadMissing
        Just (CodeLoadFailure e)            -> do debugM (fst cl ++ " : CodeLoadFailure " ) 
                                                  return (CodeLoadFailure e)
        Just clc@(CodeLoadController   _ _) -> do debugM (fst cl ++ " : CodeLoadController " ) 
                                                  return clc  
        Just clv@(CodeLoadView         _ _) -> do debugM (fst cl ++ " : CodeLoadView" ) 
                                                  return clv
        Just clc@(CodeLoadComponentController   _ _) -> do debugM (fst cl ++ " : CodeLoadComponentController " ) 
                                                           return clc  
        Just clv@(CodeLoadComponentView         _ _) -> do debugM (fst cl ++ " : CodeLoadComponentView" ) 
                                                           return clv
        
-- | Checks to see if the file exists and if the file is newer than the loaded function.  If the 
-- code needs to be reloaded, then 'loadCode' will be called.