-import Data.Char
-
-import Compat.RawSystem ( rawSystem )
-import Compat.Directory ( findExecutable )
-
-main = do
- args <- getArgs
- case args of
- ('-':'f' : ghc) : args -> do
- doIt (dropWhile isSpace ghc) args
- args -> do
- mb_ghc <- findExecutable "ghc"
- case mb_ghc of
- Nothing -> dieProg ("cannot find ghc")
- Just ghc -> doIt ghc args
+import System.FilePath
+import System.IO
+
+#if defined(mingw32_HOST_OS)
+import Control.Monad
+import Foreign
+import Foreign.C.String
+#endif
+
+main :: IO ()
+main = do
+ args <- getArgs
+ case parseRunGhcFlags args of
+ (Help, _) -> printUsage
+ (ShowVersion, _) -> printVersion
+ (RunGhcFlags (Just ghc), args') -> doIt ghc args'
+ (RunGhcFlags Nothing, args') -> do
+ mbPath <- getExecPath
+ case mbPath of
+ Nothing -> dieProg ("cannot find ghc")
+ Just path ->
+ let ghc = takeDirectory (normalise path) </> "ghc"
+ in doIt ghc args'
+
+data RunGhcFlags = RunGhcFlags (Maybe FilePath) -- GHC location
+ | Help -- Print help text
+ | ShowVersion -- Print version info
+
+instance Monoid RunGhcFlags where
+ mempty = RunGhcFlags Nothing
+ Help `mappend` _ = Help
+ _ `mappend` Help = Help
+ ShowVersion `mappend` _ = ShowVersion
+ _ `mappend` ShowVersion = ShowVersion
+ RunGhcFlags _ `mappend` right@(RunGhcFlags (Just _)) = right
+ left@(RunGhcFlags _) `mappend` RunGhcFlags Nothing = left
+
+parseRunGhcFlags :: [String] -> (RunGhcFlags, [String])
+parseRunGhcFlags = f mempty
+ where f flags ("-f" : ghc : args)
+ = f (flags `mappend` RunGhcFlags (Just ghc)) args
+ f flags (('-' : 'f' : ghc) : args)
+ = f (flags `mappend` RunGhcFlags (Just ghc)) args
+ f flags ("--help" : args) = f (flags `mappend` Help) args
+ f flags ("--version" : args) = f (flags `mappend` ShowVersion) args
+ -- If you need the first GHC flag to be a -f flag then
+ -- you can pass -- first
+ f flags ("--" : args) = (flags, args)
+ f flags args = (flags, args)
+
+printVersion :: IO ()
+printVersion = do
+ putStrLn ("runghc " ++ VERSION)