-- Miscellaneous checks on package description
extraChecks :: GenericPackageDescription -> UploadMonad ()
extraChecks genPkgDesc = do
  let pkgDesc = flattenPackageDescription genPkgDesc
  -- various checks

  --FIXME: do the content checks. The dev version of Cabal generalises
  -- checkPackageContent to work in any monad, we just need to provide
  -- a record of ops that will do checks inside the tarball. We should
  -- gather a map of files and dirs and have these just to map lookups:
  --
  -- > checkTarballContents = CheckPackageContentOps {
  -- >   doesFileExist      = Set.member fileMap,
  -- >   doesDirectoryExist = Set.member dirsMap
  -- > }
  -- > fileChecks <- checkPackageContent checkTarballContents pkgDesc

  let pureChecks = checkPackage genPkgDesc (Just pkgDesc)
      checks = pureChecks -- ++ fileChecks
      isDistError (PackageDistSuspicious {}) = False -- warn without refusing
      isDistError _                          = True
      (errors, warnings) = partition isDistError checks
  mapM_ (throwError . explanation) errors
  mapM_ (warn . explanation) warnings

  -- Proprietary License check (only active in central-server branch)
  when (not allowAllRightsReserved && license pkgDesc == AllRightsReserved) $
    throwError $ "This server does not accept packages with 'license' "
              ++ "field set to AllRightsReserved."

  -- Check for an existing x-revision
  when (isJust (lookup "x-revision" (customFieldsPD pkgDesc))) $
    throwError $ "Newly uploaded packages must not specify the 'x-revision' "
              ++ "field in their .cabal file. This is only used for "
              ++ "post-release revisions."

  -- Check reasonableness of names of exposed modules
  let topLevel = case library pkgDesc of
                 Nothing -> []
                 Just l ->
                     nub $ map head $ filter (not . null) $ map components $ exposedModules l
      badTopLevel = topLevel \\ allocatedTopLevelNodes

  unless (null badTopLevel) $
          warn $ "Exposed modules use unallocated top-level names: " ++
                          unwords badTopLevel

-- Monad for uploading packages:
--      WriterT for warning messages
--      Either for fatal errors
extraChecks :: GenericPackageDescription -> UploadMonad ()
extraChecks genPkgDesc = do
  let pkgDesc = flattenPackageDescription genPkgDesc
  -- various checks

  --FIXME: do the content checks. The dev version of Cabal generalises
  -- checkPackageContent to work in any monad, we just need to provide
  -- a record of ops that will do checks inside the tarball. We should
  -- gather a map of files and dirs and have these just to map lookups:
  --
  -- > checkTarballContents = CheckPackageContentOps {
  -- >   doesFileExist      = Set.member fileMap,
  -- >   doesDirectoryExist = Set.member dirsMap
  -- > }
  -- > fileChecks <- checkPackageContent checkTarballContents pkgDesc

  let pureChecks = checkPackage genPkgDesc (Just pkgDesc)
      checks = pureChecks -- ++ fileChecks
      isDistError (PackageDistSuspicious {}) = False -- warn without refusing
      isDistError _                          = True
      (errors, warnings) = partition isDistError checks
  mapM_ (throwError . explanation) errors
  mapM_ (warn . explanation) warnings

  -- Proprietary License check (only active in central-server branch)
  when (not allowAllRightsReserved && license pkgDesc == AllRightsReserved) $
    throwError $ "This server does not accept packages with 'license' "
              ++ "field set to AllRightsReserved."

  -- Check for an existing x-revision
  when (isJust (lookup "x-revision" (customFieldsPD pkgDesc))) $
    throwError $ "Newly uploaded packages must not specify the 'x-revision' "
              ++ "field in their .cabal file. This is only used for "
              ++ "post-release revisions."

  -- Check reasonableness of names of exposed modules
  let topLevel = case library pkgDesc of
                 Nothing -> []
                 Just l ->
                     nub $ map head $ filter (not . null) $ map components $ exposedModules l
      badTopLevel = topLevel \\ allocatedTopLevelNodes

  unless (null badTopLevel) $
          warn $ "Exposed modules use unallocated top-level names: " ++
                          unwords badTopLevel

-- Monad for uploading packages:
--      WriterT for warning messages
--      Either for fatal errors
extraChecks genPkgDesc = do
  let pkgDesc = flattenPackageDescription genPkgDesc
  -- various checks

  --FIXME: do the content checks. The dev version of Cabal generalises
  -- checkPackageContent to work in any monad, we just need to provide
  -- a record of ops that will do checks inside the tarball. We should
  -- gather a map of files and dirs and have these just to map lookups:
  --
  -- > checkTarballContents = CheckPackageContentOps {
  -- >   doesFileExist      = Set.member fileMap,
  -- >   doesDirectoryExist = Set.member dirsMap
  -- > }
  -- > fileChecks <- checkPackageContent checkTarballContents pkgDesc

  let pureChecks = checkPackage genPkgDesc (Just pkgDesc)
      checks = pureChecks -- ++ fileChecks
      isDistError (PackageDistSuspicious {}) = False -- warn without refusing
      isDistError _                          = True
      (errors, warnings) = partition isDistError checks
  mapM_ (throwError . explanation) errors
  mapM_ (warn . explanation) warnings

  -- Proprietary License check (only active in central-server branch)
  when (not allowAllRightsReserved && license pkgDesc == AllRightsReserved) $
    throwError $ "This server does not accept packages with 'license' "
              ++ "field set to AllRightsReserved."

  -- Check for an existing x-revision
  when (isJust (lookup "x-revision" (customFieldsPD pkgDesc))) $
    throwError $ "Newly uploaded packages must not specify the 'x-revision' "
              ++ "field in their .cabal file. This is only used for "
              ++ "post-release revisions."

  -- Check reasonableness of names of exposed modules
  let topLevel = case library pkgDesc of
                 Nothing -> []
                 Just l ->
                     nub $ map head $ filter (not . null) $ map components $ exposedModules l
      badTopLevel = topLevel \\ allocatedTopLevelNodes

  unless (null badTopLevel) $
          warn $ "Exposed modules use unallocated top-level names: " ++
                          unwords badTopLevel

-- Monad for uploading packages:
--      WriterT for warning messages
--      Either for fatal errors