#!/bin/sh -e

# Idea borrowed from RedHat's kernel package

if [ -n "$1" ]; then
	if [ ! -d "$1" ]; then
		echo "$1" does not exist, or is not a directory
		exit 1
	fi
	cd $1
else
	cd /usr/include
fi

if [ ! -d asm-s390 -o ! -d asm-s390x ] ; then
	echo E: asm-s390 and asm-s390x must exist, or you will have problems
	exit 1
fi

rm -rf asm
mkdir asm

for h in `( ls asm-s390; ls asm-s390x ) | grep '\.h$' | sort -u`; do
	name=`echo $h | tr a-z. A-Z_`
	# common header
	cat > asm/$h << EOF
/* All asm/ files are generated and point to the corresponding
 * file in asm-s390 or asm-s390x. To regenerate, run "generate-asm"
 */

#ifndef __S390STUB__${name}__
#define __S390STUB__${name}__

EOF

	# common for s390 and s390x
	if [ -f asm-s390/$h -a -f asm-s390x/$h ]; then
		cat >> asm/$h <<EOF
#ifdef __s390x__
#include <asm-s390x/$h>
#else
#include <asm-s390/$h>
#endif
EOF

	# s390 only
	elif [ -f asm-s390/$h ]; then
		cat >> asm/$h <<EOF
#ifndef __s390x__
#include <asm-s390/$h>
#endif
EOF
	# s390x only
	else
		cat >> asm/$h <<EOF
#ifdef __s390x__
#include <asm-s390x/$h>
#endif
EOF
	fi

	# common footer
	cat >> asm/$h <<EOF

#endif /* !__S390STUB__${name}__ */
EOF

done
