#!/bin/sh

# gdb/mswin/regenerate
# author: Geoffrey Noer, noer@cygnus.com

# This script regenerates some files in gdb/mswin for WinGDB releases
# (mostly in the prebuilt subdirectory).  Normally this should be run in
# gdb/mswin but you can also provide a -m mswin-location and it
# will do its work there.

# It appears to work on at least the following triples:
# sparc-sun-sunos4.1, i486-linux, and mips-sgi-irix5.3

# Things left to add at some point:
#  -- change quarter, version numbers in gui.rc

USAGE="regenerate [-m mswin-location] [-h]"

# ---------------------------- Subroutines --------------------------------

# do a compare, if the file changed, copy over a new one

update()
{
  if [ -f $FOO ]; then
     cmp $FOO $BAR >/dev/null 2>1
     if [ $? != 0 ]; then
       echo "updating $BAR"
       cp $FOO $BAR
     fi
  else
     echo Error: unix build file $FOO not found!!!
  fi
  return
}

# do a unix configure in /tmp

unix_config()
{
   BUILD="/tmp/wingdb-config"

   if [ -d "$BUILD" ]; then
     echo "ERROR: $BUILD already exists!  Exiting..."
     exit 1
   fi

   mkdir $BUILD
   cd $BUILD

   echo Configuring a unix tree in $BUILD.  This takes a while...
   $MSWIN/../../configure --target=sh-hms >/dev/null 2>1
   return
}

# fixes the version numbers in a prebuilt/<target>/version.c file

fix_version()
{
  echo "    $TARGET"
  cp $MSWIN/prebuilt/$TARGET/version.c $MSWIN/prebuilt/$TARGET/version.c-bak
  grep -v version "$MSWIN/prebuilt/$TARGET/version.c-bak" > $MSWIN/prebuilt/$TARGET/version.c
  echo "char *version = \"$VERS\";" >> $MSWIN/prebuilt/$TARGET/version.c
  rm -f $MSWIN/prebuilt/$TARGET/version.c-bak
  return
}

# ------------------------------ Setup -------------------------------

DIR=`pwd`

# parse command line args

while [ $# -gt 0 ]; do
  case "$1" in
    -h|-he*|--he*)
	echo $USAGE ; exit 0
	;;
    -m*|--m*)
	MSWIN=$2
	;;
  esac
  shift
done

# sanity check MSWIN var

if [ $MSWIN ]; then
  if [ ! -d $MSWIN ]; then
    echo "Error: $MSWIN directory does not exist!"
    exit 1
  fi
  cd $MSWIN
fi

MSWIN=`pwd`   # we don't want a relative pathname

# check for mswin/prebuilt directory

if [ ! -d "$MSWIN/prebuilt" ]; then
  echo "Error: $MSWIN/prebuilt does not exist!"
  exit 1
fi

# do a unix configure so the right files can be generated

unix_config

# --------------------- Regenerate gdb/mswin ----------------------------

# Make; copy files over if they've changed:

# bfd.h

cd $BUILD/bfd
FOO="bfd.h"
BAR="$MSWIN/prebuilt/bfd.h"
make $FOO >/dev/null 2>1
update

# libiberty config files

cd $BUILD/libiberty
FOO="config.h"
BAR="$MSWIN/prebuilt/libiberty/config.h"
make $FOO >/dev/null 2>1
update

FOO="alloca-conf.h"
BAR="$MSWIN/prebuilt/libiberty/alloca-conf.h"
update

# gdb exp.tab files

cd $BUILD/gdb
FOO="c-exp.tab.c"
BAR="$MSWIN/prebuilt/gdb/cexptab.c"
make $FOO >/dev/null 2>1
update

FOO="f-exp.tab.c"
BAR="$MSWIN/prebuilt/gdb/fexptab.c"
make $FOO >/dev/null 2>1
update

FOO="m2-exp.tab.c"
BAR="$MSWIN/prebuilt/gdb/m2exptab.c"
make $FOO >/dev/null 2>1
update

# Hitachi simulator code

cd $BUILD/sim/sh
make >/dev/null 2>1

FOO="code.c"
BAR="$MSWIN/prebuilt/sh/code.c"
update

FOO="table.c"
BAR="$MSWIN/prebuilt/sh/table.c"
update

# mips/elf32-target.h

sed -e s/NN/32/g < $MSWIN/../../bfd/elfxx-target.h > $MSWIN/prebuilt/mips/elf32-target.h-tmp
FOO="$MSWIN/prebuilt/mips/elf32-target.h-tmp"
BAR="$MSWIN/prebuilt/mips/elf32-target.h"
update
rm -f $MSWIN/prebuilt/mips/elf32-target.h-tmp


# <target>/version.c files

VERS=`grep "VERSION\ =\ " $MSWIN/../Makefile.in | awk -F' ' '{print $3}'`

if [ -z "$VERS" ]; then
  VERS=`grep "VERSION=" $MSWIN/../Makefile.in | awk -F'=' '{print $2}'`
fi

if [ -z "$VERS" ]; then
   echo Warning: could not figure out version number 
   echo Leaving prebuilt/<target>/version.c files alone 
else
   echo Updating GDB version in version.c to $VERS for
   for TARGET in a29k i386 h8300 m68k mips sh sparclite; do
      fix_version
   done
fi


# and we're done...

echo Deleting $BUILD

rm -rf $BUILD

echo Done updating $MSWIN

exit 0
