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

srcdir="$1"

version="`grep version_string "$srcdir/version.c" | sed -e 's/[^"]*"//;s/".*//'`"

# Check whether we really need to rebuild tree.inc. Rebuilding tree.inc
# itself doesn't take long, but it forces recompilation of gpidump.pas,
# so it's a good idea to prevent it when possible.
if [ -r "$2" ] && [ "$2" -nt "$srcdir/version.c" ] && [ "$2" -nt "$srcdir/tree.def" ]; then
  if [ x"`grep 'GCC_VERSION ' "$2" | sed -e "s/[^']*'//;s/['].*//"`" = x"$version" ]; then
    exit 0
  else
    echo "*** Regenerating $2 because the GCC version seems to have changed"
  fi
fi

sub()
{
if [ x"$version" = x ]; then
  return 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\.0' > /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\.0' > /dev/null; then
    echo '{$define GCC_2_95_3}'
  fi
  if echo $version | grep '2\.9[6-9]' > /dev/null || echo $version | grep '3\.0' > /dev/null; then
    echo '{$define EGCS97}'
  fi
fi
echo '{$endif}'
echo ""
sed -ne '/^DEFTREECODE/{s,/\*.*\*/,,;p;}' "$srcdir/tree.def" || return 1
}

sub > "$2" || (rm -f "$2"; false)
