#!/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

# Mostly copied from GPC's config-lang.in
if echo $version | grep '^2\.9' > /dev/null || echo $version | grep '^3\.[1-9]' > /dev/null || echo $version | grep egcs > /dev/null; then
  echo '{$define EGCS}'
  if echo $version | grep '^2\.95\.[3-9]' > /dev/null || echo $version | grep '^3\.[1-9]' > /dev/null; then
    echo '{$define GCC_2_95_3}'
  fi
  if echo $version | grep '^2\.9[6-9]' > /dev/null || echo $version | grep '^3\.[1-9]' > /dev/null; then
    echo '{$define EGCS97}'
    if echo $version | grep '^3\.[3-9]' > /dev/null; then
      echo '{$define GCC_3_3}'
      if echo $version | grep '^3\.[4-9]' > /dev/null; then
        echo '{$define GCC_3_4}'
      fi
    fi
  fi
fi
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 '/#else/,/#endif/d' "$GCCDIR/p/p-tree.def"
} | sed -ne '/^DEFTREECODE/{s,/\*.*\*/,,;s/["'"'"']x["'"'"'].*/'"'x', 0)/;p;}"

} > "$2" || { rm -f "$2"; false; }
