getPhonyInstalledPackageInfo :: PackageName -> FilePath
                             -> IO (Maybe InstalledPackageInfo)
getPhonyInstalledPackageInfo pkgname pathsModule = do
  content <- readFile pathsModule
  case extractVersion content of
    Nothing      -> return Nothing
    Just version -> return (Just pkginfo)
      where
        pkgid   = PackageIdentifier pkgname version
        pkginfo = emptyInstalledPackageInfo { sourcePackageId = pkgid }
  where
    -- search through the Paths_pkgname.hs file, looking for a line like:
    --
    -- > version = Version {versionBranch = [2,0], versionTags = []}
    --
    -- and parse it using 'Read'. Yes we are that evil.
    --
    extractVersion content =
      case [ version
           | ("version":"=":rest) <- map words (lines content)
           , (version, []) <- reads (concat rest) ] of
        [version] -> Just version
        _         -> Nothing

-- Older installed package info files did not have the installedPackageId
-- field, so if it is missing then we fill it as the source package ID.
getPhonyInstalledPackageInfo :: PackageName -> FilePath
                             -> IO (Maybe InstalledPackageInfo)
getPhonyInstalledPackageInfo pkgname pathsModule = do
  content <- readFile pathsModule
  case extractVersion content of
    Nothing      -> return Nothing
    Just version -> return (Just pkginfo)
      where
        pkgid   = PackageIdentifier pkgname version
        pkginfo = emptyInstalledPackageInfo { sourcePackageId = pkgid }
  where
    -- search through the Paths_pkgname.hs file, looking for a line like:
    --
    -- > version = Version {versionBranch = [2,0], versionTags = []}
    --
    -- and parse it using 'Read'. Yes we are that evil.
    --
    extractVersion content =
      case [ version
           | ("version":"=":rest) <- map words (lines content)
           , (version, []) <- reads (concat rest) ] of
        [version] -> Just version
        _         -> Nothing

-- Older installed package info files did not have the installedPackageId
-- field, so if it is missing then we fill it as the source package ID.
getPhonyInstalledPackageInfo pkgname pathsModule = do
  content <- readFile pathsModule
  case extractVersion content of
    Nothing      -> return Nothing
    Just version -> return (Just pkginfo)
      where
        pkgid   = PackageIdentifier pkgname version
        pkginfo = emptyInstalledPackageInfo { sourcePackageId = pkgid }
  where
    -- search through the Paths_pkgname.hs file, looking for a line like:
    --
    -- > version = Version {versionBranch = [2,0], versionTags = []}
    --
    -- and parse it using 'Read'. Yes we are that evil.
    --
    extractVersion content =
      case [ version
           | ("version":"=":rest) <- map words (lines content)
           , (version, []) <- reads (concat rest) ] of
        [version] -> Just version
        _         -> Nothing

-- Older installed package info files did not have the installedPackageId
-- field, so if it is missing then we fill it as the source package ID.