{-# LINE 1 "Debian/Dpkg/DB.hsc" #-}
{-
{-# LINE 2 "Debian/Dpkg/DB.hsc" #-}
 DB.hsc: Haskell bindings to libdpkg
   Copyright (C) 2011 Clint Adams

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
-}

{-# LANGUAGE CPP, ForeignFunctionInterface #-}


{-# LINE 23 "Debian/Dpkg/DB.hsc" #-}

module Debian.Dpkg.DB (
    msdbInit
  , setDbDir
  , pkgDbFind
  , pkgList
  , getConfigVersion
) where
import Foreign.Ptr (Ptr,FunPtr,plusPtr)
import Foreign.Ptr (wordPtrToPtr,castPtrToFunPtr)
import Foreign.Storable
import Foreign.C.Types
import Foreign.C.String (CString,CStringLen,CWString,CWStringLen)
import Foreign.Marshal.Alloc (alloca)
import Foreign.Marshal.Array (peekArray,pokeArray)
import Data.Int
import Data.Word

{-# LINE 32 "Debian/Dpkg/DB.hsc" #-}

import Foreign.Ptr (nullPtr)
import Foreign.C.String (withCString, peekCString)
import Foreign.Marshal.Utils (with)
import Control.Monad (liftM, join)
import Control.Monad.Loops (unfoldrM)
import Debian.Dpkg.Types


{-# LINE 41 "Debian/Dpkg/DB.hsc" #-}

foreign import ccall unsafe "modstatdb_open" c'modstatdb_open
  :: CInt -> IO ()
foreign import ccall unsafe "&modstatdb_open" p'modstatdb_open
  :: FunPtr (CInt -> IO ())

{-# LINE 43 "Debian/Dpkg/DB.hsc" #-}
foreign import ccall unsafe "push_error_context" c'push_error_context
  :: IO ()
foreign import ccall unsafe "&push_error_context" p'push_error_context
  :: FunPtr (IO ())

{-# LINE 44 "Debian/Dpkg/DB.hsc" #-}
foreign import ccall unsafe "dpkg_set_progname" c'dpkg_set_progname
  :: CString -> IO ()
foreign import ccall unsafe "&dpkg_set_progname" p'dpkg_set_progname
  :: FunPtr (CString -> IO ())

{-# LINE 45 "Debian/Dpkg/DB.hsc" #-}
foreign import ccall unsafe "dpkg_db_set_dir" c'dpkg_db_set_dir
  :: CString -> IO ()
foreign import ccall unsafe "&dpkg_db_set_dir" p'dpkg_db_set_dir
  :: FunPtr (CString -> IO ())

{-# LINE 46 "Debian/Dpkg/DB.hsc" #-}

foreign import ccall unsafe "pkg_db_iter_new" c'pkg_db_iter_new
  :: IO (Ptr C'pkgiterator)
foreign import ccall unsafe "&pkg_db_iter_new" p'pkg_db_iter_new
  :: FunPtr (IO (Ptr C'pkgiterator))

{-# LINE 48 "Debian/Dpkg/DB.hsc" #-}
foreign import ccall unsafe "pkg_db_iter_next" c'pkg_db_iter_next
  :: Ptr C'pkgiterator -> IO (Ptr C'pkginfo)
foreign import ccall unsafe "&pkg_db_iter_next" p'pkg_db_iter_next
  :: FunPtr (Ptr C'pkgiterator -> IO (Ptr C'pkginfo))

{-# LINE 49 "Debian/Dpkg/DB.hsc" #-}

msdbInit :: IO ()
msdbInit = do
	c'push_error_context
	c'modstatdb_open 0
	withCString "haskell-dpkg" c'dpkg_set_progname

setDbDir :: String -> IO ()
setDbDir x = withCString x  c'dpkg_db_set_dir

pkgDbIterNext :: Ptr C'pkgiterator -> IO (Maybe (Ptr C'pkginfo, Ptr C'pkgiterator))
pkgDbIterNext i = do
         pptr <- c'pkg_db_iter_next i
         if pptr == nullPtr
             then
                return Nothing
             else
                return $ Just (pptr, i)

pkgpList :: IO [Ptr C'pkginfo]
pkgpList = c'pkg_db_iter_new >>= unfoldrM (pkgDbIterNext)

pkgList :: IO [C'pkginfo]
pkgList = pkgpList >>= mapM peek

foreign import ccall unsafe "pkg_db_find" c'pkg_db_find
  :: CString -> IO (Ptr C'pkginfo)
foreign import ccall unsafe "&pkg_db_find" p'pkg_db_find
  :: FunPtr (CString -> IO (Ptr C'pkginfo))

{-# LINE 75 "Debian/Dpkg/DB.hsc" #-}

pkgDbFind :: String -> IO C'pkginfo
pkgDbFind p = withCString p c'pkg_db_find >>= peek

getConfigVersion :: C'pkginfo -> IO String
getConfigVersion p = do
		v <- peekCString (c'versionrevision'version vr)
		r <- peekCString (c'versionrevision'revision vr)
		return $ nonZeroEpoch e ++ v ++ nonNativeRevision r
	where
		nonZeroEpoch e = if e == 0 then "" else (show e) ++ ":"
		nonNativeRevision r = if r == "" then r else "-" ++ r
		e = fromIntegral (c'versionrevision'epoch vr)
		vr = c'pkginfo'configversion p