#!/bin/sh -e
# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../linux-header/generate-asm
# Copyright (C) 2006 The T2 SDE Project
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

# Idea borrowed from RedHat's kernel package
# Borrowed from Debian GNU/Linux for sparc64
# Extended for x86-64 and T2 -ReneR

# Usage:

dir="$1"
name1="asm-$2"
name2="asm-$3"
define="$4"

if [ ! -d "$dir" ] ; then
	echo E: $dir is not a directory.
	exit 1
fi

cd $dir

if [ ! -d "$name1" -o ! -d "$name2" ] ; then
	echo E: $name1 and $name2 must exist, or you will have problems.
	exit 1
fi

if [ ! "$define" ] ; then
	echo E: No define specified - this will not work.
	exit 1
fi

rm -rf asm
mkdir asm

for h in `( ls $name1; ls $name2 ) | 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 $name1 or $name2.
 */

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

EOF

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

	# $name1 only
	elif [ -f $name1/$h ]; then
		cat >> asm/$h <<EOF
#ifndef $define
#include <$name1/$h>
#endif
EOF
	# $name2 only
	else
		cat >> asm/$h <<EOF
#ifdef $define
#include <$name2/$h>
#endif
EOF
	fi

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

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

done