import Distribution.Simple
import Distribution.Simple.LocalBuildInfo
import Distribution.PackageDescription
import System.Cmd
import System.Directory
import GHC.IO.Exception

main = defaultMainWithHooks (simpleUserHooks { postBuild = buildLibrary
                                             , postClean = cleanLibrary })
  where
    buildLibrary _ _ pkg info = do
        putStrLn "Compiling standard library modules..."
        cwd <- getCurrentDirectory
        setCurrentDirectory $ cwd ++ "/Std"
        code <- system "make"
        case code of
            ExitFailure _ -> fail "Build failed."
            ExitSuccess -> putStrLn "Build succeeded."

    cleanLibrary _ _ pkg info = do
        putStrLn "Cleaning standard library modules..."
        cwd <- getCurrentDirectory
        setCurrentDirectory $ cwd ++ "/Std"
        code <- system "make clean"
        case code of
            ExitFailure _ -> fail "Build failed."
            ExitSuccess -> putStrLn "Build succeeded."
