#! /usr/bin/env runhaskell

import Control.Monad ( unless )
import Distribution.Simple
import System.Cmd ( system )
import System.FilePath
import System.Posix.Files ( createSymbolicLink, fileExist )


main = defaultMainWithHooks (simpleUserHooks 
   { runTests = testRunner
   , postBuild = customPostBuild
   } )
   where
      -- Run the entire unit test suite
      testRunner _ _ _ _ = do
         system $ "runhaskell -isrc -itestsuite testsuite/runtests.hs"
         return ()

      -- Create symlink to the binary after build for developer 
      -- convenience
      customPostBuild _ _ _ _ = do
         let dest = "pwstore"

         exists <- fileExist dest
         unless exists $ do
            let src = "dist" </> "build" </> dest </> dest
            createSymbolicLink src dest
