#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-gdb

VERSION=${VERSION:-6.8}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
rm -rf $PKG
mkdir -p $PKG

cd $TMP
rm -rf gdb-$VERSION
tar xjvf $CWD/gdb-$VERSION.tar.bz2 || exit 1
cd gdb-$VERSION
chown -R root:root .
./configure \
  --prefix=/usr \
  --mandir=/usr/man \
  --infodir=/usr/info \
  --build=$ARCH-slackware-linux
( cd readline ; make )
make -j4 || exit 1
make install DESTDIR=$PKG

# None of this stuff has ever been included in this package:
rm -f $PKG/usr/lib/{libbfd*,libiberty*,libopcodes*}
rmdir $PKG/usr/lib 2> /dev/null
rm -f $PKG/usr/info/{annotate*,bfd*,configure*,standards*}
rm -rf $PKG/usr/include

# Use the -tui option if you want this.
# Including a whole extra copy of the gdb binary is obnoxious:
rm -f $PKG/usr/bin/gdbtui $PKG/usr/man/man1/gdbtui.1*

mkdir -p $PKG/usr/doc/gdb-$VERSION/gdb
cp -a COPYING* README $PKG/usr/doc/gdb-$VERSION
cd gdb
cp -a NEWS README TODO $PKG/usr/doc/gdb-$VERSION/gdb
cp -a gdbserver/README $PKG/usr/doc/gdb-$VERSION/README.gdbserver
find $PKG/usr/doc/gdb-$VERSION -type f -exec chmod 644 {} \;

# Strip binaries:
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-debug 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-debug 2> /dev/null
)

# Compress and link manpages, if any:
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
    for manpagedir in $(find . -type d -name "man*") ; do
      ( cd $manpagedir
        for eachpage in $( find . -type l -maxdepth 1) ; do
          ln -s $( readlink $eachpage ).gz $eachpage.gz
          rm $eachpage
        done
        gzip -9 *.?
      )
    done
  )
fi

# Compress info files, if any:
if [ -d $PKG/usr/info ]; then
  ( cd $PKG/usr/info
    rm -f dir
    gzip -9 *
  )
fi

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/gdb-$VERSION-$ARCH-$BUILD.tgz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/gdb-$VERSION
  rm -rf $PKG
fi