#
# Copyright (c) 2005 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.opensource.org/licenses/cpl.php.
#
# This program is distributed in the hope that it will be useful, but
# without any waranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.
#

from conary.build.packagerecipe import _AbstractPackageRecipe, _recipeHelper
from conary.build.recipe import RECIPE_TYPE_INFO

from conary.build import buildpackage
from conary.build import errors
from conary.build import usergroup
from conary.deps import deps

class UserGroupInfoRecipe(_AbstractPackageRecipe):
    # abstract base class
    _recipeType = RECIPE_TYPE_INFO
    ignore = 1

    def __init__(self, cfg, laReposCache, srcdirs, extraMacros={}):
        _AbstractPackageRecipe.__init__(self, cfg, laReposCache, srcdirs, extraMacros)
        self.destdirPolicy = []
        self.packagePolicy = []
        self.requires = []
        self.infofilename = None
        self.realfilename = None

    def getPackages(self):
        # we do not package up build logs for info-* packages
        self._autoCreatedFileCount -= 1
        comp = buildpackage.BuildComponent(
            'info-%s:%s' %(self.infoname, self.type), self)
        f = comp.addFile(self.infofilename, self.realfilename)
        f.tags.set("%s-info" %self.type)
        self.addProvides(f)
        self.addRequires(f)
        comp.provides.union(f.provides())
        comp.requires.union(f.requires())
        return [comp]

    def addProvides(self, f):
        pass

    def addRequires(self, f):
        if not self.requires:
            return
        depSet = deps.DependencySet()
        for info, type in self.requires:
            if type == 'user':
                depClass = deps.UserInfoDependencies
            else:
                depClass = deps.GroupInfoDependencies
            depSet.addDep(depClass, deps.Dependency(info, []))
        f.requires.set(depSet)

    def requiresUser(self, user):
        self.requires.append((user, 'user'))

    def requiresGroup(self, group):
        self.requires.append((group, 'group'))

    def __getattr__(self, name):
        if not name.startswith('_'):
	    if name in usergroup.__dict__:
		return _recipeHelper(self._build, self,
                                     usergroup.__dict__[name])
        if name in self.__dict__:
            return self.__dict__[name]
        raise AttributeError, name

class UserInfoRecipe(UserGroupInfoRecipe):
    type = 'user'
    # abstract base class
    ignore = 1

    def addProvides(self, f):
        depSet = deps.DependencySet()
        depSet.addDep(deps.UserInfoDependencies,
                      deps.Dependency(self.infoname, []))
        depSet.addDep(deps.GroupInfoDependencies,
                      deps.Dependency(self.groupname, []))
        f.provides.set(depSet)

class GroupInfoRecipe(UserGroupInfoRecipe):
    type = 'group'
    # abstract base class
    ignore = 1

    def addProvides(self, f):
        depSet = deps.DependencySet()
        depSet.addDep(deps.GroupInfoDependencies,
                      deps.Dependency(self.infoname, []))
        f.provides.set(depSet)

@@ -1021,16 +1021,53 @@
 acros=%7B%7D
+, %0A                 crossCompile=None
 ):%0A     
@@ -1146,16 +1146,30 @@
 raMacros
+, crossCompile
 )%0A      

fix info recipe __init__ signature to match its superclass