ErrorTableRegexp = "/^\\s*#define\\s+\\w+\(\\s+\\(-?\\d+L\\)\)|\(initialize_\\w+_error_table\\(\\)\)\\s*$/" ;
ExtractErrorCodes = "perl -e 'while (<STDIN>) { if ($(ErrorTableRegexp)) { print; } }'" ;

Intermediates = "$(BUILT_PRODUCTS_DIR)/Kerberos5.intermediates" ;
IntermediateErrorTables = "$(Intermediates)/ErrorTables" ;
IntermediateIncludes = "$(Intermediates)/include" ;
IntermediateV5Includes = "$(IntermediateIncludes)/krb5" ;
IntermediateV4Includes = "$(IntermediateIncludes)/kerberosIV" ;
IntermediateGSSIncludes = "$(IntermediateIncludes)/gssapi" ;
IntermediateFrameworkHeaders = "$(Intermediates)/Kerberos" ;
IntermediateBuild = "$(Intermediates)/build" ;

AutoconfH = "$(IntermediateV5Includes)/autoconf.h" ;
OsconfH = "$(IntermediateV5Includes)/osconf.h" ;

# CatHeader <header> : <macro name> <header.hin> <error tables>
rule CatHeader
{
    NOTFILE "$(2[1])" ;
    DEPENDS "$(1)" : "$(2[2-])" "$(AutoconfH)" ;
    Clean.Remove clean : "$(1)" ;
}
actions CatHeader
{
    mkdir -p "$(1:D)"
    echo "/*"                                   > "$(1)"
    echo " * This file is auto generated."     >> "$(1)"
    echo " * Please do not edit it."           >> "$(1)"
    echo " */"                                 >> "$(1)"
    echo ""                                    >> "$(1)"
    echo "#ifndef $(2[1])"                     >> "$(1)"
    echo "#define $(2[1])"                     >> "$(1)"
    echo ""                                    >> "$(1)"
    echo "/* Environment dependent macros */"  >> "$(1)"
    grep SIZEOF           "$(AutoconfH)"       >> "$(1)"
    echo ""                                    >> "$(1)"
    for header in "$(2[3-])" ; do
        base=`basename "${header}"`
        echo ""                                >> "$(1)"
        echo "/* Error tables from ${base} */" >> "$(1)"
        cat "${header}" | $(ExtractErrorCodes) >> "$(1)"
    done
    echo ""                                    >> "$(1)"
    cat "$(2[2])"                              >> "$(1)"
    echo "#endif /* $(2[1]) */"                >> "$(1)"
}

rule MakeFrameworkHeader
{
    DEPENDS "$(1)" : "$(2)" ;
    Clean.Remove clean : "$(1)" ;
}
actions MakeFrameworkHeader
{
    mkdir -p "$(1:D)"
    $(SED) -e 's:<com_err.h>:<Kerberos/com_err.h>:' \
           -e 's:<kerberosIV/des.h>:<Kerberos/des.h>:' \
           -e 's:<kerberosIV/krb.h>:<Kerberos/krb.h>:' \
           -e 's:<kerberosIV/krb_err.h>:<Kerberos/krb_err.h>:' \
           -e 's:<profile.h>:<Kerberos/profile.h>:' \
           -e 's:<krb5.h>:<Kerberos/krb5.h>:' \
           -e 's:<gssapi/gssapi.h>:<Kerberos/gssapi.h>:' \
           -e 's:<gssapi/gssapi_krb5.h>:<Kerberos/gssapi_krb5.h>:' \
           -e 's:<gssapi/gssapi_generic.h>:<Kerberos/gssapi_generic.h>:' \
           "$(2)" > "$(1)"
}

rule CopyHeader
{
    DEPENDS "$(1)" : "$(2)" ;
    Clean.Remove clean : "$(1)" ;
}
actions CopyHeader
{
    mkdir -p "$(1:D)"
    cp -fRP "$(2)" "$(1)"
}


MakeFrameworkHeader "$(IntermediateFrameworkHeaders)/com_err.h" : "$(SRCROOT)/../../KerberosErrors/Headers/Kerberos/com_err.h" ;
CopyHeader          "$(IntermediateIncludes)/com_err.h"         : "$(SRCROOT)/../../KerberosErrors/Headers/Kerberos/com_err.h" ;

CopyHeader          "$(IntermediateV4Includes)/des.h"       : "$(SRCROOT)/../../Kerberos5/Sources/include/kerberosIV/des.h" ;
MakeFrameworkHeader "$(IntermediateFrameworkHeaders)/des.h" : "$(IntermediateV4Includes)/des.h" ;

CopyHeader          "$(IntermediateV4Includes)/krb.h"           : "$(SRCROOT)/../../Kerberos5/Sources/include/kerberosIV/krb.h" ;
CopyHeader          "$(IntermediateV4Includes)/krb_err.h"       : "$(IntermediateErrorTables)/krb_err.h" ;
MakeFrameworkHeader "$(IntermediateFrameworkHeaders)/krb.h"     : "$(IntermediateV4Includes)/krb.h" ;
MakeFrameworkHeader "$(IntermediateFrameworkHeaders)/krb_err.h" : "$(IntermediateV4Includes)/krb_err.h" ;

CatHeader           "$(IntermediateIncludes)/profile.h"         : "__KERBEROSPROFILE__"
                                                                  "$(SRCROOT)/../../Kerberos5/Sources/util/profile/profile.hin"
                                                                  "$(IntermediateErrorTables)/prof_err.h" ;
MakeFrameworkHeader "$(IntermediateFrameworkHeaders)/profile.h" : "$(IntermediateIncludes)/profile.h" ;

CatHeader           "$(IntermediateIncludes)/krb5.h"               : "__KERBEROS5__"
                                                                     "$(SRCROOT)/../../Kerberos5/Sources/include/krb5.hin"
                                                                     "$(IntermediateErrorTables)/asn1_err.h"
                                                                     "$(IntermediateErrorTables)/kdb5_err.h"
                                                                     "$(IntermediateErrorTables)/krb5_err.h"
                                                                     "$(IntermediateErrorTables)/krb524_err.h"
                                                                     "$(IntermediateErrorTables)/kv5m_err.h" ;
MakeFrameworkHeader "$(IntermediateFrameworkHeaders)/krb5.h"       : "$(IntermediateIncludes)/krb5.h" ;

CatHeader           "$(IntermediateGSSIncludes)/gssapi.h"              : "__GSSAPI__"
                                                                         "$(SRCROOT)/../../Kerberos5/Sources/lib/gssapi/generic/gssapi.hin"
                                                                         "$(IntermediateErrorTables)/gssapi_err_generic.h"
                                                                         "$(IntermediateErrorTables)/gssapi_err_krb5.h" ;
CopyHeader          "$(IntermediateGSSIncludes)/gssapi_generic.h"      : "$(SRCROOT)/../../Kerberos5/Sources/lib/gssapi/generic/gssapi_generic.h" ;
CopyHeader          "$(IntermediateGSSIncludes)/gssapi_krb5.h"         : "$(SRCROOT)/../../Kerberos5/Sources/lib/gssapi/krb5/gssapi_krb5.h" ;
MakeFrameworkHeader "$(IntermediateFrameworkHeaders)/gssapi.h"         : "$(IntermediateGSSIncludes)/gssapi.h" ;
MakeFrameworkHeader "$(IntermediateFrameworkHeaders)/gssapi_generic.h" : "$(IntermediateGSSIncludes)/gssapi_generic.h" ;
MakeFrameworkHeader "$(IntermediateFrameworkHeaders)/gssapi_krb5.h"    : "$(IntermediateGSSIncludes)/gssapi_krb5.h" ;

CopyHeader "$(AutoconfH)" : "$(IntermediateBuild)/include/krb5/autoconf.h" ;
CopyHeader "$(OsconfH)"   : "$(IntermediateBuild)/include/krb5/osconf.h" ;

DEPENDS all :	"$(IntermediateIncludes)/com_err.h"
                "$(IntermediateFrameworkHeaders)/com_err.h"

                "$(IntermediateV4Includes)/des.h"
                "$(IntermediateFrameworkHeaders)/des.h"
                
                "$(IntermediateV4Includes)/krb.h"
                "$(IntermediateV4Includes)/krb_err.h"
                "$(IntermediateFrameworkHeaders)/krb.h"
                "$(IntermediateFrameworkHeaders)/krb_err.h"
                
                "$(IntermediateIncludes)/profile.h"
                "$(IntermediateFrameworkHeaders)/profile.h"
                
                "$(IntermediateIncludes)/krb5.h"
                "$(IntermediateFrameworkHeaders)/krb5.h"
                
                "$(IntermediateGSSIncludes)/gssapi.h"
                "$(IntermediateGSSIncludes)/gssapi_generic.h"
                "$(IntermediateGSSIncludes)/gssapi_krb5.h" 
                "$(IntermediateFrameworkHeaders)/gssapi.h"
                "$(IntermediateFrameworkHeaders)/gssapi_generic.h"
                "$(IntermediateFrameworkHeaders)/gssapi_krb5.h"
                
                "$(AutoconfH)"
                "$(OsconfH)" ;

DEPENDS install : all ;
DEPENDS installhdrs : all ;
