{-# 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 :: FilePath -> IO (Control' a)
fields debFP :: FilePath
debFP =
    FilePath -> (FilePath -> IO (Control' a)) -> IO (Control' a)
forall (m :: * -> *) a.
(MonadIO m, MonadMask m) =>
FilePath -> (FilePath -> m a) -> m a
withSystemTempDirectory ("fields.XXXXXX") ((FilePath -> IO (Control' a)) -> IO (Control' a))
-> (FilePath -> IO (Control' a)) -> IO (Control' a)
forall a b. (a -> b) -> a -> b
$ \tmpdir :: FilePath
tmpdir ->
      do FilePath
debFP <- FilePath -> IO FilePath
canonicalizePath FilePath
debFP
         FilePath -> IO (Control' a) -> IO (Control' a)
forall a. FilePath -> IO a -> IO a
withCurrentDirectory FilePath
tmpdir (IO (Control' a) -> IO (Control' a))
-> IO (Control' a) -> IO (Control' a)
forall a b. (a -> b) -> a -> b
$
           do (res :: ExitCode
res, out :: FilePath
out, err :: FilePath
err) <- FilePath
-> [FilePath] -> FilePath -> IO (ExitCode, FilePath, FilePath)
readProcessWithExitCode "ar" ["x",FilePath
debFP,"control.tar.gz"] ""
              Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ExitCode
res ExitCode -> ExitCode -> Bool
forall a. Eq a => a -> a -> Bool
/= ExitCode
ExitSuccess) (FilePath -> IO ()
forall a. HasCallStack => FilePath -> a
error (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ "Dpkg.fields: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath -> FilePath
forall a. Show a => a -> FilePath
show FilePath
out FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ "\n" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath -> FilePath
forall a. Show a => a -> FilePath
show FilePath
err FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ "\n" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ ExitCode -> FilePath
forall a. Show a => a -> FilePath
show ExitCode
res)
              (res :: ExitCode
res, out :: FilePath
out, err :: FilePath
err) <- FilePath
-> [FilePath] -> FilePath -> IO (ExitCode, FilePath, FilePath)
readProcessWithExitCode "tar" ["xzf", "control.tar.gz", "./control"] ""
              Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ExitCode
res ExitCode -> ExitCode -> Bool
forall a. Eq a => a -> a -> Bool
/= ExitCode
ExitSuccess) (FilePath -> IO ()
forall a. HasCallStack => FilePath -> a
error (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ "Dpkg.fields: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath -> FilePath
forall a. Show a => a -> FilePath
show FilePath
out FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ "\n" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath -> FilePath
forall a. Show a => a -> FilePath
show FilePath
err FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ "\n" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ ExitCode -> FilePath
forall a. Show a => a -> FilePath
show ExitCode
res)
              Either ParseError (Control' a)
c <- FilePath -> IO (Either ParseError (Control' a))
forall a.
ControlFunctions a =>
FilePath -> IO (Either ParseError (Control' a))
parseControlFromFile "control"
              case Either ParseError (Control' a)
c of
                Left e :: ParseError
e -> FilePath -> IO (Control' a)
forall a. HasCallStack => FilePath -> a
error (ParseError -> FilePath
forall a. Show a => a -> FilePath
show ParseError
e)
                (Right c :: Control' a
c) -> Control' a -> IO (Control' 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