#!/bin/sh
CWD=`pwd`
VERSION=4.3.2
ARCH=alpha
TARGET=$ARCH-alphaslack-linux
BUILD=1
TMP=/tmp
PKG=$TMP/package-gcc

cd $TMP
tar xjf $CWD/gcc-core-$VERSION.tar.bz2
tar xjf $CWD/gcc-fortran-$VERSION.tar.bz2
tar xjf $CWD/gcc-g++-$VERSION.tar.bz2
tar xjf $CWD/gcc-objc-$VERSION.tar.bz2
# install docs
( cd gcc-$VERSION
  # Fix perms/owners
  chown -R root:root .
  find . -perm 777 -exec chmod 755 {} \;
  find . -perm 775 -exec chmod 755 {} \;
  find . -perm 754 -exec chmod 755 {} \;
  find . -perm 664 -exec chmod 644 {} \;
  mkdir -p $PKG/usr/doc/gcc-$VERSION
  cp -a \
    BUGS COPYING COPYING.LIB ChangeLog FAQ INSTALL MAINTAINERS README* *.html \
    $PKG/usr/doc/gcc-$VERSION
# build gcc
  ./configure \
  --prefix=/usr \
  --enable-shared \
  --enable-threads=posix \
  --enable-__cxa_atexit \
  --disable-checking \
  --with-gnu-ld \
  --verbose \
  --target=$TARGET \
  --build=$TARGET || exit 1

  # Start the build:
  make -j2 -s || exit 1
  # Set GCCCHECK=something to run the tests
  if [ ! -z $GCCCHECK ]; then
    make check || exit 1
  fi

  make install DESTDIR=$PKG || exit
  chmod 755 $PKG/usr/lib/libgcc_s.so.1
  # This is provided by binutils, so delete it here:
  rm -f $PKG/usr/lib/libiberty.a

  # Strip out unneeded stuff from the libraries and binaries:
  ( cd $PKG
    find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
    find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  )
  # Most people debug their own code (not the libraries), so we'll strip these.
  # It cuts the size down quite a bit.
  find $PKG -name "*.a" | xargs file | grep "archive" | cut -f 1 -d : | xargs strip -g

  # Fix stuff up:
  ( cd $PKG/usr/info ; rm dir ; gzip -9 * )
  ( cd $PKG/usr/man ; rm man1/g++.1 ; gzip -9 man{1,7}/* )
  ( cd $PKG
    mkdir -p lib
    cd lib
    ln -sf /usr/bin/cpp .
  )
  ( cd $PKG/usr/bin
    mv g++ g++-gcc-$VERSION
    mv gcc gcc-$VERSION
    mv gfortran gfortran-gcc-$VERSION
    ln -sf g++-gcc-$VERSION g++
    ln -sf gcc-$VERSION gcc
    ln -sf gfortran-gcc-$VERSION gfortran
    ln -sf g++ c++
    ln -sf gcc cc
    ln -sf gfortran f77
    ln -sf gcc-$VERSION ${TARGET}-gcc
    ln -sf gcc-$VERSION ${TARGET}-gcc-$VERSION
    ln -sf g++-gcc-$VERSION ${TARGET}-c++
    ln -sf g++-gcc-$VERSION ${TARGET}-g++
    ln -sf gfortran-gcc-$VERSION ${TARGET}-gfortran
    ln -sf gfortran-gcc-$VERSION ${TARGET}-gfortran-$VERSION
    chown root:bin * .
  )

  mkdir -p $PKG/install
  # Install the descriptions:
  ( cd $CWD
    cat slack-desc.gcc > $PKG/install/slack-desc
  )

# Filter all .la files (thanks much to Mark Post for the sed script):
( cd $TMP
  for file in `find . -type f -name "*.la"` ; do
    cat $file | sed -e 's%-L/gcc-[[:graph:]]* % %g' > $TMP/tmp-la-file
    cat $TMP/tmp-la-file > $file
  done
  rm $TMP/tmp-la-file
)

( cd $PKG
  makepkg -l y -c n $TMP/gcc-$VERSION-$ARCH-$BUILD.tgz )
) 2>&1 | tee $TMP/gcc.build.log
