#!/usr/bin/env runhaskell import Data.Char.CEDICT.GenerateCode import System.IO.UTF8 import System.IO hiding ( appendFile , getContents , getLine , print , putStr , putStrLn , readFile , readLn , writeFile , hGetContents , hGetLine , hPutStr , hPutStrLn ) import Prelude hiding ( appendFile , getContents , getLine , print , putStr , putStrLn , readFile , readLn , writeFile ) import Distribution.Simple import Distribution.PackageDescription import Control.Monad import System.FilePath import System.Directory import System.Time import System.Locale hooks = defaultUserHooks { preBuild = optionalGenC } main = defaultMainWithHooks hooks optionalGenC _ _ = do n <- needed when n genC return emptyHookedBuildInfo where headerPath = combine "c" "data.h" dictPath = combine "d" "utf8-cedict" needed = do f' <- doesFileExist headerPath f'' <- if f' then tCompare else return True when (not f'') $ putStrLn $ "Found existing header file at " ++ headerPath return f'' genC = do hPutStrLn stderr "Generating CEDICT header file -- may take a few minutes." t <- bench $ readAndWrite dictPath headerPath hPutStrLn stderr $ " " ++ show t ++ " seconds" tCompare = do headerTime <- getModificationTime headerPath dictTime <- getModificationTime dictPath return $ headerTime < dictTime bench op = do ts <- getClockTime op tf <- getClockTime return $ tdSec $ diffClockTimes tf ts