#!/bin/sh

#       build-bash-builtins.sh
#       
#       Copyright 2010 Anshul Narula aka sunny <sunny_delhi18@yahoo.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

#Maintainer: Anshul Narula aka sunny <sunny_delhi18@yahoo.com>

shopt -s extglob

softname=bash-builtins
softversion=4.1.2
march=i486
mtune=i686
build=64.2
cwd=$(pwd)
Upstream_source=ftp://ftp.gnu.org/gnu/bash/bash-4.1.tar.gz

src=$cwd/bash-4.1.tar.gz

if [[ ! -f $src ]] ; then
  wget -c "$Upstream_source" || exit 1
fi

: ${TMP:=/tmp}
pkg=$TMP/pkg-bash-builtins
CFLAGS="-O2 -march=$march -mtune=$mtune"

cd "$TMP"
rm -rf bash-4.1
tar -xvf "$src" || exit 1
cd bash-4.1 || exit 1
chown -R root:root .
find . -perm 664 | xargs chmod 644
find . -perm 775 | xargs chmod 755

CFLAGS="$CFLAGS" ./configure
make -j4 || make || exit 1

cd examples/loadables || exit 2
make -j4 || make || exit 2

#our make install
mkdir -p "$pkg/usr/share/bash-builtins" 
for i in ./!(*.h|*.c|*.o|README|Makefile*|*/); do
  cp "$i" "$pkg/usr/share/bash-builtins" || exit 3
done
  
(
 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
)

mkdir -p "$pkg/usr/doc/$softname-$softversion"
cp README "$pkg/usr/doc/$softname-$softversion"

mkdir -p "$pkg/install"
cat <<'DESC' > "$pkg/install/slack-desc"
bash-builtins: bash loadable builtins
bash-builtins:
bash-builtins: This package contains loadable builtins provided as examples along
bash-builtins: with the bash source code.Examples given are reimplementations of
bash-builtins: standard commands whose execution time is dominated by process startup
bash-builtins: time with few exceptions.
bash-builtins:
bash-builtins:
bash-builtins:
bash-builtins:
bash-builtins:
DESC

mkdir -p "$pkg/usr/src/$softname-$softversion"
cp "$cwd/build-$softname.sh" "$pkg/usr/src/$softname-$softversion"

# Build the package:
cd "$pkg"
makepkg -l y -c n "$TMP/$softname-$softversion-$march-$build.txz

