module Test.SBench.Space.Single.RunExecutable
    ( run
    , runWith
    )
  where

import System.FilePath ( FilePath, addExtension, isRelative )
import System.Process ( system )

import Test.SBench.Options ( 
    TestOpts (..)
  , opts2string
  , ProfParam (..)
  , Breakdown (..)
  , Restriction (..)
  , ProfilingOptions
  , MemoryOptions
  , RuntimeOptions (..)
  , ThreadNum
  , ProgramArguments
  )

run :: TestOpts -> FilePath -> IO FilePath
run topts = runWith 
              (get progArgs) 
              (get threadNum)
              (get memOpts)
              (get profOpts)
  where get opts = opts $ rOpts topts 

runWith :: ProgramArguments -> Maybe ThreadNum -> MemoryOptions -> 
       ProfilingOptions -> FilePath -> IO FilePath
runWith pargs threads mopts popts exe = do
    let call = opts2string
                [ (if isRelative exe then "./" else "") ++ exe
                , opts2string pargs
                , "+RTS"
                , case threads of { Nothing -> ""; Just t -> "-N" ++ show t}
                , opts2string (map show mopts)
                , opts2string (map show popts)
                ]
    putStrLn $ "call the following program:\n" ++ call ++ "\n"
    system call
    return $ addExtension exe "hp"