#!/bin/bash

rm -f gcc.spec gcc-mainline.spec gcc-*.spec

if [ -f gcc-mainline.changes ] ; then
  mv gcc-mainline.changes gcc.changes
fi

# Default is to generate the normal gcc package
# unless a parameter is given.  In case that it is,
# that parameter will be used as suffix for the package name
# and as suffix for the install path (/opt/gcc-SUFFIX)

if [ $# -lt 1 ]; then
	GCC_FOR_OPT=no
	outfile=gcc.spec
else
	GCC_FOR_OPT=yes
	suffix=$1
	outfile=gcc-$1.spec
fi

if [ $GCC_FOR_OPT = yes ]; then
	sed -e 's%@gcc_suffix@%-'"$suffix"'%g' \
	    -e 's$@PREFIX_FOR_OPT@$%define _prefix /opt/gcc-'"$suffix"'$g' \
	    -e 's$@LIBDIR_FOR_OPT@$%define _libdir %{_prefix}/%{_lib}$g' \
	    -e 's%@GCC_FOR_OPT@%1%g' \
	    -e 's%^PreReq:.*%%g' \
	    gcc.spec.in > $outfile
	test -f gcc.changes && mv gcc.changes gcc-$suffix.changes
        rm -f cross*gcc-icecream-backend.spec cross*gcc-icecream-backend.changes cross-avr-gcc.changes cross-avr-gcc.spec
else
	sed -e 's%@gcc_suffix@%%g' \
	    -e 's$@PREFIX_FOR_OPT@$$g' \
	    -e 's$@LIBDIR_FOR_OPT@$$g' \
	    -e 's%@GCC_FOR_OPT@%0%g' \
	    gcc.spec.in > $outfile
fi
	
	
if [ $GCC_FOR_OPT != yes ]; then
# disabled: mips, sparc
  for arch in alpha hppa arm i386 x86_64 s390 s390x ppc ppc64 ia64; do
    { sed -n -e '1,/COMMON-BEGIN/p' cross.spec.in
      sed -n -e '/COMMON-BEGIN/,/COMMON-END/p' gcc.spec
      sed -n -e '/COMMON-END/,$p' cross.spec.in; } |
    sed -e "s#@cross_arch@#$arch#" > cross-$arch-gcc-icecream-backend.spec
    test -f gcc.changes && ln -f gcc.changes cross-$arch-gcc-icecream-backend.changes
  done

  ## non-icecream cross compilers.
  for arch in avr; do
    cross_arch=$arch sh micro-cross.spec.in > cross-$arch-gcc.spec
    test -f gcc.changes && ln -f gcc.changes cross-$arch-gcc.changes
  done
fi
