#!/bin/bash

# arm/build
# Pre-build setup script for ARMedslack Linux's gcc
# by Stuart Winter <mozes@slackware.com>
# Last modified: 03-Jan-05

source /usr/share/slackdev/buildkit.sh

# We need autoconf 2.64 for the moment:
#removepkg autoconf
#upgradepkg --install-new $PKGSTORE/../extra/autoconf264/autoconf*t?z || exit 1

# Package metadata:
export PKGNAM=gcc
export VERSION=${VERSION:-15.2.0}
export BUILD=${BUILD:-1}
slack_var_pkgseries

# If building in /patches for a Stable Release, we drop the packages into the
# '/patches' directory within the configured Slackware tree:
slack_findpkgstore_is_stablerelease && {
    export PKGSTORE=$PORTPATCHPKGS
    export PKGSERIES=/ # unset as it's not required for patches
    export BUILD=1_slack15.1
    mkdir -vpm755 $PKGSTORE/$PKGSERIES ;}

## ******************************************************************* ##
# For test packages - store in another location rather than overwriting
# the working copy in the main tree:
# export PKGSTORE=/tmp/
# mkdir -vpm755 $PKGSTORE/$PKGSERIES
## ******************************************************************* ##

# Set package name:
export SLACKPACKAGE=$PKGNAM-$VERSION-$PKGARCH-$BUILD.txz

# Ensure base ARM packages are installed first:
slackbasedeps

# Needs
{ slackcheckpkgdeps infozip  || installpkg $PKGSTORE/a/infozip-[0-9]*.t?z || exit 99; }
{ slackcheckpkgdeps gmp      || installpkg $PKGSTORE/l/gmp-[0-9]*.t?z     || exit 99; }
{ slackcheckpkgdeps mpfr     || installpkg $PKGSTORE/l/mpfr-[0-9]*.t?z    || exit 99; }
{ slackcheckpkgdeps gtk+2    || installpkg $PKGSTORE/l/gtk+2-[0-9]*.t?z    || exit 99; }
# Introduced in gcc-4.5:
{ slackcheckpkgdeps libmpc   || installpkg $PKGSTORE/l/libmpc-[0-9]*.t?z  || exit 99; }
{ slackcheckpkgdeps elfutils || installpkg $PKGSTORE/l/elfutils-[0-9]*.t?z  || exit 99; }
# configure: error: libXtst not found, required by java.awt.Robot
{ slackcheckpkgdeps harfbuzz || installpkg $PKGSTORE/l/harfbuzz-[0-9]*.t?z  || exit 99; }
{ slackcheckpkgdeps isl      || installpkg $PKGSTORE/l/isl-[0-9]*.t?z     || exit 99; }

# 21-Jan-2016:
# Remove LLVM.  I cannot get llvm to build when llvm was present during the building of the
# gcc package.  Removing llvm before building gcc makes llvm build again.
#removepkg llvm

# Ensure the kernel headers package is installed.  These should always be installed
# but it doesn't hurt (unless you're in the middle of building something else)
# to do this:
ORIGPATH=$PWD # store this location because we change to /usr/include below
#( cd /usr/include
#  rm -rf linux asm-arm asm-generic asm
#  removepkg kernel-headers
# Incase I ever forget and delete them from the main ARMedslack tree, I've copied a working Kernel
# headers from Linux 2.4.26.  In the past I have found 2.6 headers break building gcc.
#  installpkg ~/tgzstash/d/kernel-headers-*.t?z
#  installpkg $ORIGPATH/arm/kernel-headers-2.4.26-arm-1.t?z
#  installpkg $ORIGPATH/arm/kernel-headers-2.4.26-arm-1.t?z
#  ln -s asm asm-arm )

# Launch the package build script:
BUILDLOG=$SLKPORTARCH-$( basename $SLACKPACKAGE .t?z ).build.log
( ./$PKGNAM.SlackBuild ) >& /dev/stdout | tee $BUILDLOG

# Compress the build log:
bzip2 -9fvz $BUILDLOG

# Restore the current autoconf:
#removepkg autoconf

# Reinstall llvm:
#installpkg $PKGSTORE/d/llvm-[0-9]*.t?z

# Do we need to rebuild llvm?
# Retry next gcc build with llvm installed and see if it works (didn't used to!)
#if [ "$( gcc -v 2>&1 | grep "Reading specs" | rev| cut -d/ -f2 | rev )" != "$( clang -v 2>&1| grep -E "slackware-linux.*\/"| tail -n1 | rev | cut -d/ -f1 | rev )" ]; then
#   echo "*********************"
#   echo "llvm needs rebuilding"
#   echo "*********************"
#fi
