# Contributor: Lukas Jirkovsky <l.jirkovsky@gmail.com>
# Contributor: Allan McRae <allan@archlinux.org>
# Contributor: Stefan Husmann <stefan-husmann@t-online.de>

pkgname=('gdb-git' 'gdb-common-git')
pkgver=126882.6d1be0b90e8
pkgrel=2
pkgdesc="The GNU Debugger from git"
arch=(armv7h aarch64 i686 x86_64)
url="http://www.gnu.org/software/gdb/"
license=('GPL3')
makedepends=(git texinfo python guile ncurses expat xz
             mpfr source-highlight readline libelf help2man xxhash boost)
provides=('gdb-common' 'gdb')
conflicts=('gdb-common' 'gdb')
backup=('etc/gdb/gdbinit')
options=('!libtool' '!lto')
source=('gdb::git+https://sourceware.org/git/binutils-gdb.git')
sha256sums=('SKIP')

pkgver() {
  cd "$srcdir/gdb"
  echo $(git rev-list --count master).$(git rev-parse --short master)
}

prepare() {
  cd gdb

  # fixes build, copied from the gdb PKGBUILD
  # hack! - libiberty configure tests for header files using "$CPP $CPPFLAGS"
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure
}

build() {
  cd gdb

  mkdir -p build
  cd build

  ../configure \
    --prefix=/usr \
    --disable-nls \
    --disable-werror \
    --enable-source-highlight \
    --enable-tui \
    --with-system-readline \
    --with-python=/usr/bin/python \
    --with-system-gdbinit=/etc/gdb/gdbinit
  make
}

package_gdb-common-git() {
  provides=('gdb-common')
  conflicts=('gdb-common')
  depends=('python' 'guile')
  cd gdb/build

  make -C gdb/data-directory DESTDIR="$pkgdir" install
}

package_gdb-git() {
  depends=(libreadline.so libncursesw.so python
           libexpat.so xz mpfr gmp source-highlight libxxhash.so libelf
           "gdb-common-git=$pkgver")
  provides=('gdb')
  conflicts=('gdb')
  backup=(etc/gdb/gdbinit)

  guilever=$(guile-config --version 2>&1 | sed -n -e 's/.*version *\([0-9]\.[0-9]\).*/\1/p')
  [[ $guilever =~ ^([0-9]*)\.([0-9]*) ]]
  maj_ver=${BASH_REMATCH[1]}
  min_ver=${BASH_REMATCH[2]}
  ((next_min=min_ver + 1))
  depends+=("guile>=${guilever}" "guile<${maj_ver}.${next_min}")

  cd gdb/build

  make -C gdb DESTDIR="$pkgdir" install
  make -C gdbserver DESTDIR="$pkgdir" install

  # install "custom" system gdbinit
  install -dm 755 "$pkgdir/etc/gdb"
  touch "$pkgdir/etc/gdb/gdbinit"

  # comes from gdb-common
  rm -r "$pkgdir/usr/share/gdb/"
}
