module Standard( standard, fast ) where
import System.FilePath
import Shellish
import Benchmark hiding ( darcs )
import Control.Monad( forM_, mapM_, forM, filterM, when )
import Control.Monad.Trans( liftIO )
import qualified Control.Monad.State as MS

check :: BenchmarkCmd ()
check darcs = do
  cd "repo"
  darcs [ "check", "--no-test" ]
  return ()

repair :: BenchmarkCmd ()
repair darcs = do
  cd "repo"
  darcs [ "repair" ]
  return ()

annotate :: BenchmarkCmd ()
annotate darcs = do
  cd "repo"
  whenM ((not . or) `fmap` mapM test_e files) $ fail "no files to annotate"
  sequence [ whenM (test_e f) $ (darcs [ "annotate", f ] >> return ())
             | f <- files ]
  return ()
    where files = [ "Setup.hs", "Setup.lhs" ]

get :: Int -> [String] -> BenchmarkCmd ()
get n param darcs = do
  forM [1..n] $ \x -> darcs $ "get" : param ++ ["repo", "get" ++ show x]
  return ()

pull :: Int -> BenchmarkCmd ()
pull n darcs = do
  cd "repo"
  rm_f "_darcs/patches/unrevert"
  darcs [ "unpull", "--last", show n, "--all" ]
  reset -- the benchmark starts here
  darcs [ "pull", "--all" ]
  return ()

-- Oh my eyes! Oh noes! Horrible!
darcs_wh :: [String] -> BenchmarkCmd ()
darcs_wh param darcs = do
  state <- MS.get
  newstate <- liftIO $ catch (MS.execStateT (darcs $ "whatsnew" : param) state)
                             (\_ -> return state)
  MS.put newstate

wh :: Int -> BenchmarkCmd ()
wh n darcs = do
  cd "repo"
  forM [1..n] $ \_ -> darcs_wh [] darcs
  return ()

wh_mod :: Int -> BenchmarkCmd ()
wh_mod n darcs = do
  cd "repo"
  files <- filterM test_f =<< ls "."
  when (null files) $ fail "no files to modify in repo root!"
  forM files $ \f -> mv f $ f <.> "__foo__"
  forM [1..n] $ \_ -> darcs_wh [] darcs
  forM files $ \f -> mv (f <.> "__foo__") f
  return ()

wh_l :: Int -> BenchmarkCmd ()
wh_l n darcs = do
  cd "repo"
  forM [1..n] $ \_ -> darcs_wh [ "--look-for-adds" ] darcs
  return ()

-- | n patches for each file
record_mod :: Int -> BenchmarkCmd ()
record_mod n darcs = do
 cd "repo"
 files <- filterM test_f =<< ls "."
 forM_ [1..n] $ \x -> do
   forM_ files $ \f -> liftIO (appendFile f (show n))
   darcs [ "record", "--all", "-m", show x, "--no-test"]
 darcs [ "obliterate", "--last=" ++ show n, "--all" ]
 return ()

fast :: [ Benchmark () ]
fast = [ Destructive "get (full)" $ get 1 []
       , Destructive "get (lazy, x10)" $ get 10 ["--lazy"]
       , Idempotent "pull 100" $ pull 100
       , Idempotent "annotate" annotate
       , Idempotent "wh x50" $ wh 50
       , Idempotent "wh mod x50" $ wh_mod 50
       , Idempotent "wh -l x20" $ wh_l 20 ]

standard :: [ Benchmark () ]
standard = fast ++
           [ Idempotent "check" check
           , Idempotent "repair" repair
           , Idempotent "pull 1000" $ pull 1000 ]


