#!/bin/sh

# Create tree.inc, needed by GPIDump, for your GCC version.
# (Called mk-t-inc rather than make-tree-inc for "8+3" reasons.)
#
# Copyright (C) 2002-2006 Free Software Foundation, Inc.
# Author: Frank Heckenbach <frank@pascal.gnu.de>
#
# 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.
#
# 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; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA. }

# This script requires bash. Since bash cannot be assumed to be in
# /bin, /usr/bin or any other certain place, we cannot use it in the
# first line. So we use /bin/sh, which can be assumed to exist. Then
# we check if it's actually bash, and if not, try to re-run the
# script with bash.
if [ x"$BASH" = x ]; then
  if [ x"$RERUN_BASH_TRIED" != x ]; then
    echo "`basename "$0"`: cannot run, \`bash' is either not bash or a very old version" >&2
    exit 1
  else
    RERUN_BASH_TRIED=1; export RERUN_BASH_TRIED
    exec bash "$0" "$@"
    echo "`basename "$0"`: cannot run bash" >&2
    exit 1
  fi
fi

if [ $# -ne 2 ]; then
  echo "Usage: `basename $0` gcc-dir dest-filename" >&2
  exit 1
fi

GCCDIR="$1"

version="`grep version_string "$GCCDIR/version.c" | sed -e 's/[^"]*"//;s/".*//'`"
if [ x"$version" = x ]; then
  exit 1
fi

{

cat << EOF
{ Generated automatically by $0 for GCC version $version. }
{ DO NOT CHANGE THIS FILE MANUALLY! }

{\$ifndef GCC_VERSION_SET}
{\$define GCC_VERSION_SET}

{\$define GCC_VERSION '$version'}
EOF

# Use result of GPC's config-lang.in
sed -ne 's,#define\(.*\),{$define\1},p' ../../gcc-version.h

sed -ne '/Start GPI info/,/End GPI info/{s,/\*.*\*/,,g;s/0x/$/g;s/#/{$/g;s/[^\]$/&}/;s/\\$//;p;}' "$GCCDIR/p/module.c"
echo '{$endif}'
echo ""

{
  cat "$GCCDIR/tree.def"
  echo 'DEFTREECODE (LAST_AND_UNUSED_C_TREE_CODE, "unused", '"'x', 0)"
  sed -e '/#ifdef EGCS/,${/#ifdef EGCS/d;/#else/,/#endif/d}'  "$GCCDIR/p/p-tree.def"
} | sed -ne ':loop;/\\$/{N;b loop};/^DEFTREECODE/{s,/\*.*\*/,,;s/["'"'"']x["'"'"'].*/'"'x', 0)/;p;};/^#/p"
} > "$2" || { rm -f "$2"; false; }
