summaryrefslogtreecommitdiff
path: root/Main.hs
blob: f0db040f3c60ca7332357e2266e35a908baa3b1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module Main where
import System.Exit
import System.Plugins

ghcargs :: [String]
ghcargs = ["-XPatternGuards"] 

-- | Dynamic launching function
main :: IO ()
main = do
    putStrLn "hsbot starting..."
    m <- makeAll "Hsbot.hs" ghcargs
    (modul', imain) <- case m of
        MakeSuccess _ _ -> do
            ldstat <- load_ "Hsbot/Main.o" [".","Hsbot","Hsbot/Plugins"] "imain"
            case ldstat of
                LoadSuccess v m' -> return (v,m')
                LoadFailure e -> do
                    putStrLn "Couldn't load Hsbot.Main.imain:"
                    mapM_ putStrLn e
                    exitWith $ ExitFailure 127
        MakeFailure e -> do
            putStrLn "FATAL: Couldn't compile Hsbot.hs:"
            mapM_ putStrLn e
            exitWith $ ExitFailure 127
    putStrLn "Compiled & Loaded Hsbot.Main.imain..."
    imain modul' reboot

-- | Dynamic rebooting function
reboot :: Module -> a -> IO ()
reboot modul' state = do
    mkstat <- makeAll "Hsbot.hs" ghcargs
    case mkstat of
        MakeSuccess _ _ -> do
            unloadAll modul'
            ldstat <- load_ "Hsbot/Main.o" [".","Hsbot","Hsbot/Plugins"] "imain'"
            case ldstat of
                LoadSuccess modul'' imain' -> do
                    putStrLn "REBOOT: Successful recompilation & reloading, rebooting..."
                    imain' modul'' reboot state
                LoadFailure e -> fatality e
        MakeFailure e -> fatality e
    where
        fatality errs = do
            putStrLn $ "REBOOT: FATAL: Couldn't reboot thread, err:"
            mapM_ putStrLn errs