#!/bin/bash
#
# This script takes as input the output of the following operations:
# from top of os/linux-2.4, after x86 build:
#     find include/linux -type f -print > inclin-x86.lst
#     find include/asm-i386 -type f -print > incasm-x86.lst
# from top of os/linux-2.4, after ppc build (attempt...):
#     find include/linux -type f -print > inclin-ppc.lst
#     find include/asm-ppc -type f -print > incasm-ppc.lst
# from top of os/linux-2.4, after mips build:
#     find include/linux -type f -print > inclin-mips.lst
#     find include/asm-mips -type f -print > incasm-mips.lst
# from top of os/linux-2.4:
#     find include/asm-generic -type f -print > incasm-gen.lst
#
# Note that is important to actually build os/linux-2.4 for each
# BOOTARCH, because some header files are generated files, and they
# do need to be exported.
#
# The output of this script is most of the bom for this ISM, the last few
# lines of the bom need to be added (just copy out of the existing bom).
#
# In other words, please don't check in the results of this script unless
# you know what you're doing...
#
# Also, please note, that the bom file has been split (expected to be
# temporary though) into bom and noexport.bom. The noexport.bom does not
# have to be available for the build to work (see the Makefile).
#

# line (file) in $arg
#
_in_() {
	cat inc$1.lst | sort | uniq
}

# line (file) in stdin or in $arg
#
_or_() {
	cat - inc$1.lst | sort | uniq
}

# line (file) in stdin and in $arg
#
_and_() {
	cat - inc$1.lst | sort | uniq -d
}

# line (file) not in stdin and in $arg
#
_Nand_() {
	cat - inc$1.lst | sort | uniq -u | \
		cat - inc$1.lst | sort | uniq -d
}

# Usage:
# cat stuff | _bom_ rpmarch
#
_bom_() {
	sed -e "s:.*:f 0644 root root \0 \0 package=hdr arch=$1 dest=sdk:"
}

_bomasm_() {
	sed -e "s:.*:f 0644 root root \0 \0 package=hdr arch=$1 dest=sdk:" \
	    -e "s:include/asm-[^/]*:include/asm:"
}


(
	_in_ lin-x86 | _and_ lin-ppc | _and_ lin-mips | _bom_ "all"
	_in_ lin-x86 | _Nand_ lin-ppc | _and_ lin-mips | _bom_ "ppc,mips"
	_in_ lin-ppc | _Nand_ lin-mips | _and_ lin-x86 | _bom_ "x86,mips"
	_in_ lin-mips | _Nand_ lin-x86 | _and_ lin-ppc | _bom_ "x86,ppc"
	_in_ lin-x86 | _or_ lin-mips | _Nand_ lin-ppc | _bom_ "ppc"
	_in_ lin-ppc | _or_ lin-x86 | _Nand_ lin-mips | _bom_ "mips"
	_in_ lin-mips | _or_ lin-ppc | _Nand_ lin-x86 | _bom_ "x86"
) | sort +4
_in_ asm-x86 | _bomasm_ "x86"
_in_ asm-ppc | _bomasm_ "ppc"
_in_ asm-mips | _bomasm_ "mips"
_in_ asm-gen | _bom_ "all"

