#!/bin/sh
#
# substitute various values for ||STRINGS|| 
#

set -e

if [ $# != 8 ] ; then 
  echo "usage: $0 host target release-date bundle bindir srcdir inputfile outputfile"
  exit 1
fi

HOSTstr=$1
TARGETstr=$2
RELNO=$3
BUNDLE=$4
BINDIR=$5
SRCDIR=$6
INFILE=$7
OUTFILE=$8

if [ -f ${SRCDIR}/release-info ] ; then
  . ${SRCDIR}/release-info
fi

if [ -f ${SRCDIR}/gcc/version.c ] ; then
  GCCvn=`awk '{printf "%s", substr ($4, 2, length ($4) - 3); }' ${SRCDIR}/gcc/version.c`
else
  GCCvn='N.A.'
fi

if [ $BUNDLE = "comp-tools" ] ; then
  TEXBUNDLE=comptools
  if [ ! $TARGETstr = $HOSTstr ] ; then
    BUNDLE=cross-tools
  fi
  else
    if [ "$BUNDLE" = "deja-gnu" ]; then
      TEXBUNDLE=dejagnu
      else
        TEXBUNDLE=$BUNDLE
    fi
fi

TAPEdflt=QIC-24
TEX_RELEASE_TAG=`echo ${RELEASE_TAG} | sed 's/-/--/g'`
INSTALLdir=`echo ${ROOTING} | sed 's,\(/[a-zA-Z0-9]+\),,`

case $HOSTstr in 
  sparc-sun-solaris2*)
        NICK=solaris2
	HOST="Sun--4 running Solaris 2"
	DEVdflt="/dev/rmt/0n"
	OS_STRING="Solaris 2"
	;;
  sparc-sun-sunos4.1.*)
	NICK=sun4
	HOST="Sun--4"
	DEVdflt=/dev/nrst8
	OS_STRING="SunOS 4.1.x"
  	;;
  m68k-sun-sunos4.1.1)
	NICK=sun3
	HOST="Sun-3"
	DEVdflt=/dev/nrst8
	OS_STRING="SunOS 4.1.1"
	;;
  mips-dec-ultrix)
	NICK=decstation
	HOST="DECstation"
	DEVdflt=/dev/nrtm0
	OS_STRING="ULTRIX 4.2"
	;;
  mips-sgi-irix4)
	NICK=iris
	HOST="Iris"
	DEVdflt=/dev/mt/tps0d0nrns
	OS_STRING="Irix 4"
	;;
  rs6000-ibm-aix)
	NICK=rs6000
	HOST="RS/6000"
	DEVdflt=/dev/rmt0.1
	OS_STRING="AIX 3.2"
	;;
  m68k-hp-hpux)
	NICK=hp300
	HOST="HP 9000/300"
	DEVdflt=/dev/rmt/0mn
	TAPEdflt=DAT
	OS_STRING="HP/UX 8"
	;;
  hppa1.1-hp-hpux)
	NICK=hppa
	HOST="HP 9000/700"
	DEVdflt=/dev/rmt/0mn
	TAPEdflt=DAT
	OS_STRING="HP/UX 8"
	;;
  i386-sco3.2v4)
	NICK=SCO
	HOST="PC running SCO"
	DEVdflt=/dev/nrct0
	OS_STRING="SCO Unix 3.2v4"
	;;
  i386-sysv4.2)
	NICK=UnixWare
	HOST="PC running UnixWare"
	DEVdflt=/dev/rmt/c0s0n
	OS_STRING="UnixWare (System V Release 4.2)"
	;;
  mips-mips-riscos5*)
	NICK=mips
	HOST="MIPS Magnum"
	DEVdflt=/dev/rmt/Q24n-0
	OS_STRING="RISC/os 5"
	;;
  *)
	echo "WARNING"
 	echo "WARNING"
	echo "NO VALID HOST FOUND"
	echo "WARNING"
 	echo "WARNING"
	exit 1
	;;
esac

TARGET=$TARGETstr

bindu=`du ${DUFLAGS--s} $BINDIR | awk '{print $1}'`
BINSIZE=`expr \( $bindu + 500 \) / 1000`
srcdu=`du ${DUFLAGS--s} $SRCDIR | awk '{print $1}'`
SRCSIZE=`expr \( $srcdu + 500 \) / 1000`

TOTALDISK=`expr $BINSIZE + $SRCSIZE`

set -x

sed 	-e "s,\|\|RELNO\|\|,$RELNO,g" \
    	-e "s,\|\|GCCvn\|\|,$GCCvn,g" \
	-e "s,\|\|HOSTstr\|\|,$HOSTstr,g" \
	-e "s,\|\|HOST\|\|,$HOST,g" \
	-e "s,\|\|TARGETstr\|\|,$TARGETstr,g" \
	-e "s,\|\|TARGET\|\|,$TARGET,g" \
	-e "s,\|\|BUNDLE\|\|,$BUNDLE,g" \
	-e "s,\|\|TEXBUNDLE\|\|,$TEXBUNDLE,g" \
	-e "s,\|\|DEVdflt\|\|,$DEVdflt,g" \
	-e "s,\|\|TAPEdflt\|\|,$TAPEdflt,g" \
	-e "s,\|\|INSTALLdir\|\|,$INSTALLdir,g" \
	-e "s,\|\|BD\|\|,$BINSIZE,g" \
	-e "s,\|\|SD\|\|,$SRCSIZE,g" \
	-e "s,\|\|DF\|\|,$TOTALDISK,g" \
	-e "s,\|\|ROOTING\|\|,$ROOTING,g" \
	-e "s,\|\|RELEASE_DIR\|\|,$RELEASE_DIR,g" \
	-e "s,\|\|RELEASE_TAG\|\|,$RELEASE_TAG,g" \
	-e "s,\|\|TEX_RELEASE_TAG\|\|,$TEX_RELEASE_TAG,g" \
	-e "s,\|\|OS_STRING\|\|,$OS_STRING,g" \
	-e "s,^@clear HOST${NICK},@set HOST${NICK}," \
	< $INFILE > $OUTFILE
