#!/bin/sh
#
# usage: mkchwrappers mapFile directory
#
# This script generates the C++ standard header wrappers for a system that already has
# a set of proprietary implementation headers.  The proprietary headers are mapped to
# the standard headers through the required map file.
#
# The map file must be a text file containing lines, one per wrapped header, of the form
# <gcc-wrapped-header> <list-of-native-headers>
# where gcc-wrapped-header are enumerated below and list-of-native-headers are the 
# proprietary implementation headers (separated by whitespace if there is more than one)
# to which the gcc-wrapped-header is mapped.
#
# Copyright (C) 2001 Free Software Foundation, Inc.
#
# 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; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

MKDIR="mkdir -p"

#
# Parse the command-line arguments and do some quick sanity checks.
#
errMsg=""
if [ $# -ne 3 ]; then
	mapFile=$1
	destDir=$2
else
	errMsg="Missing required parameters"
fi

if [ -n "$errMsg" ]; then
	echo $errMsg >&2
	echo "usage: $0 mapFile destDir" >&2
	exit 1
fi

if [ ! -r $mapFile ]; then
	echo "$mapFile is not a readable file." >&2
	exit 2
fi
if [ ! -d $destDir ]; then
	$MKDIR $destDir
fi

#
# All of our required standard header wrappers must be mapped.  Note that
# it is possible to map them to nothing (that is, create a stub header),
# but consider the effect that would have on builds and standards compliance.
#
headerList="std_cassert.h std_cctype.h std_cerrno.h std_cfloat.h \
            std_climits.h std_clocale.h std_cmath.h std_csetjmp.h \
			std_csignal.h stdarg.h stddef.h stdio.h stdlib.h \
			std_cstring.h std_ctime.h std_cwchar.h std_cwctype.h"

#
# Generate the C++ wrappers.  There are no guard macros on these wrappers
# because certain compliant headers require that there be none, so it's
# really up to the wrapped header to supply one.
#
for h in $headerList; do
  mapping=`grep $h $mapFile`
  if [ $? -ne 0 ]; then
    echo "$mapFile is missing a mapping for header $h" >&2
	exit 4
  fi
  echo $mapping | while read ourHeader theirHeaders; do
    cat >$destDir/$ourHeader <<EOF
// -*- C++ -*- header wrapper.

// Copyright (C) 2001 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library 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; either version 2, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING.  If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

// As a special exception, you may use this file as part of a free software
// library without restriction.  Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License.  This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.

// Note: this file was automatically generated by the "mkchwrappers" tool.
// Edit at your own considerable risk.

#pragma GCC system_header
EOF
	#
	# There could be a 1:N mapping of our headers into their headers.
	#
    for header in $theirHeaders; do
      echo "#include <$header>" >>$destDir/$ourHeader
    done
  done
done

exit 0
