{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
module Debian.Deb where

import Control.Monad

import Debian.Control.Common
import System.Directory (canonicalizePath, withCurrentDirectory)
import System.Exit (ExitCode(..))
import System.Process (readProcessWithExitCode)
import System.IO.Temp (withSystemTempDirectory)

fields :: (ControlFunctions a) => FilePath -> IO (Control' a)
fields :: forall a. ControlFunctions a => FilePath -> IO (Control' a)
fields FilePath
debFP =
    FilePath -> (FilePath -> IO (Control' a)) -> IO (Control' a)
forall (m :: * -> *) a.
(MonadIO m, MonadMask m) =>
FilePath -> (FilePath -> m a) -> m a
withSystemTempDirectory (FilePath
"fields.XXXXXX") ((FilePath -> IO (Control' a)) -> IO (Control' a))
-> (FilePath -> IO (Control' a)) -> IO (Control' a)
forall a b. (a -> b) -> a -> b
$ \FilePath
tmpdir ->
      do debFP <- FilePath -> IO FilePath
canonicalizePath FilePath
debFP
         withCurrentDirectory tmpdir $
           do (res, out, err) <- readProcessWithExitCode "ar" ["x",debFP,"control.tar.gz"] ""
              when (res /= ExitSuccess) (error $ "Dpkg.fields: " ++ show out ++ "\n" ++ show err ++ "\n" ++ show res)
              (res, out, err) <- readProcessWithExitCode "tar" ["xzf", "control.tar.gz", "./control"] ""
              when (res /= ExitSuccess) (error $ "Dpkg.fields: " ++ show out ++ "\n" ++ show err ++ "\n" ++ show res)
              c <- parseControlFromFile "control"
              case c of
                Left ParseError
e -> FilePath -> IO (Control' a)
forall a. HasCallStack => FilePath -> a
error (ParseError -> FilePath
forall a. Show a => a -> FilePath
show ParseError
e)
                (Right Control' a
c) -> Control' a -> IO (Control' a)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Control' a
c -- I don't think we need seq because parsec will force everything from the file