 {-# LANGUAGE ForeignFunctionInterface #-}
 module Network.LibRSync.Internal where
+
+import Data.ByteString
 
 import System.IO
 import GHC.IO.Handle

}
#endc

-- cSignature      :: FilePath -> IO (Either String CSignature)
-- cSignature p = cGenSig p >>= \r -> return $ case r of
--                                               RsDone -> Right $ getSig ps
--                                               _      -> Left "some error"
--                    where
--                      getSig :: SignaturePtr -> CSignature
--                      getSig = undefined



cGenSig :: FilePath -> Handle -> IO (Handle, Result)
cGenSig p h = handleToFd h >>= cGenSig' p >>= \r -> return (h,r)

{#fun unsafe genSig as cGenSig'
      { `String'
      , fromIntegral `Fd'
      } -> `Result' cIntToEnum
 #}



-- -- | Loading signatures
-- -- the c-function is:

cIntToEnum :: Enum a => CInt -> a
cIntToEnum = toEnum . fromIntegral

copyHandleToFd h = hDuplicate h >>= handleToFd

test = do
  h <- openBinaryFile "/tmp/signature" ReadWriteMode
  r <- hSignature "/Users/frank/tmp/httpd-error.log" h
  print r
  hClose h
