# vim: sw=4:
# A wider view of where we are
SubDir AllP4 p4-python ;

# Import P4 rules
SubRules AllP4 p4 : P4 ;

# Import packaging rules
SubRules AllP4 packaging : PACKAGING ;

# Back to where we are
SubDir AllP4 p4-python ;

# We need pic builds of P4API on Linux x86_64
if $(OS) = LINUX && $(OSPLAT:L) = x86_64
{
    TYPE = pic ;
}

# Default to standard builds
PKG_TYPE ?= standard ;

# Pick a python, any python
PYTHON ?= python ;

# Locate our API build
P4BINTOKENS = p4-bin bin.$(OS:L)$(OSVER:EL)$(OSPLAT:EL) $(TYPE) ;
P4BIN = [ FSubDirPath AllP4 $(P4BINTOKENS) ] ;
SEARCH on p4api.tgz = $(P4BIN) ;
ALL_LOCATE_TARGET = $(P4BIN) ;

rule P4Python
{
    local _vtokens = $(RELEASE[1-2]) $(PATCHLEVEL) ;
    local _version = $(_vtokens:J=.) ;

    # p4python					- Overall target
    # p4python.tgz				- Source distribution

    local _target = $(<) ;
    local _sources = [ FGristFiles $(>) ] ;
    local _tgz = $(_target:S=.tgz) ;

    Depends all : $(_target) ;
    NotFile $(_target) ;

    Depends $(_target) : $(_tgz) ;
    Depends $(_tgz) : $(_sources) ;
    Depends $(_tgz) : p4api.tgz ;

    SEARCH  on $(_sources)	= $(SEARCH_SOURCE) ;
    LOCATE  on $(_tgz) 	   	= $(EXEC) ;

    Clean clean : $(_tgz) ;

    NAME    on $(_tgz)	   	= $(_target) ;
    VERSION on $(_tgz)		= $(_version) ;
    DIST_NAME	on $(_tgz)	= $(_target)-$(_version).tar.gz ;

    local _s ;
    for _s in $(_sources)
    {
	if $(_s:BS) = setup.py
	{
	    # This is the location we build from. Should be the current
	    # directory, but may not be if this is part of a larger build.
	    TOPDIR on $(_tgz) = $(SEARCH_SOURCE) ;
	}
    }
    P4PythonSourceDist $(_tgz) : p4api.tgz ;
}


actions together P4PythonSourceDist
{
    set -e
    buildtop=`pwd`
    cd $(TOPDIR)
    rm -rf __p4api__
    mkdir __p4api__
    tar zxvf $(>) -C __p4api__ --strip-components=1
    $(PYTHON) setup.py sdist --apidir __p4api__
    rm -rf __p4api__
    cd $buildtop
    mv $(TOPDIR)/dist/$(DIST_NAME) $(<)
}