{- This is the Main module which is used by the buddha
   declarative debugger for Haskell. Don't modify this file unless
   you are sure about what you are doing 

   Author: Bernie Pope
   buddha website: www.cs.mu.oz.au/~bjpop/buddha
-}

module Main where

import DD 
   ( dd 
   , dataDirectory 
   ) 
import Buddha as B 
   ( initialCallCount )
import qualified Main_B 
   ( main )
import IOPrims_B 
   ( runIO )
import Prelude
import Posix 
import Control.Concurrent 
import Control.Exception 
import Data.IORef 
   ( writeIORef )

-- subsituted in by the Makefile
dataDir = "/usr/local/share/buddha"

main :: Prelude.IO ()
main
   = do writeIORef dataDirectory dataDir 
        catchCtrlC 
        counter <- B.initialCallCount
        case runIO counter (Main_B.main counter) of
           Left e   -> notifyException e
           Right () -> return ()
        dd

-- turn sigINT signals into exceptions that can be caught in the debugger
catchCtrlC :: Prelude.IO Handler 
catchCtrlC 
   = do main_thread <- myThreadId
        installHandler sigINT (Catch (hupHandler main_thread)) Nothing
        where
        hupHandler :: ThreadId -> Prelude.IO ()
        hupHandler main_thread
           = throwTo main_thread (ErrorCall "Interrupt Signal")

-- exception thrown and not caught in the debuggee 
notifyException :: Exception -> IO ()
notifyException e
   = do putStrLn "\n\nbuddha: uncaught exception in the debuggee:"
        putChar '\t'
        print e
