#!/bin/bash

set -v
set -e

srcdir=/home/vlada/AUR/mingw-w64-gdb/mingw-w64-gdb/vk

pkgname=mingw-w64-gdb-vk
pkgver=10.1
pkgrel=1
pkgdesc="The GNU Debugger (mingw-w64) with tui"
arch=(any)
url="http://www.gnu.org/software/gdb/"
license=('LGPL')
depends=('mingw-w64-dlfcn' 'mingw-w64-expat' 'mingw-w64-zlib' 'mingw-w64-readline')
makedepends=('mingw-w64-configure' 'texinfo')
options=('staticlibs' '!buildflags' '!strip')
source=("http://ftp.gnu.org/gnu/gdb/gdb-${pkgver}.tar.xz")
sha256sums=('f82f1eceeec14a3afa2de8d9b0d3c91d5a3820e23e0a01bbb70ef9f0276b62c0')

_architectures="i686-w64-mingw32 x86_64-w64-mingw32"

source mingw-env i686-w64-mingw32

cd "$srcdir"

for _arch in ${_architectures}; do
  if [[ ! -L "$srcdir/curses.h" ]] ; then
    ln -s /usr/${_arch}/include/pdcurses/curses.h   $srcdir/curses.h
    ln -s /usr/${_arch}/include/pdcurses/curspriv.h $srcdir/curspriv.h
    ln -s /usr/${_arch}/include/pdcurses/panel.h    $srcdir/panel.h
  fi
  CFLAGS="-I$srcdir"
  CXXFLAGS="-I$srcdir"
  if [[ ${_arch} == "i686-w64-mingw32" ]]; then
    CFLAGS="$CFLAGS -DCHTYPE_32 -UCHTYPE_64"
    CXXFLAGS="$CXXFLAGS -DCHTYPE_32 -UCHTYPE_64"
  else
    CFLAGS="$CFLAGS -UCHTYPE_32"
    CXXFLAGS="$CXXFLAGS -UCHTYPE_32"
  fi
  mkdir -p build-${_arch} && pushd build-${_arch}
  ../../src/gdb-$pkgver/configure \
    --disable-option-checking \
    --build="x86_64-pc-linux-gnu" \
    --host=${_arch} \
    --target=${_arch} \
    --includedir=/usr/${_arch}/include \
    --enable-shared \
    --enable-static \
    --srcdir="../../src/gdb-$pkgver" \
    --prefix="$srcdir/install-${_arch}" \
    --enable-lto \
    --disable-source-highlight \
    --with-curses \
    --enable-tui \
    --with-system-readline \
    --with-expat .. \
    CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS" \
  > $srcdir/${_arch}-configure-out.log 2>&1
  make -j4 > $srcdir/${_arch}-make-out.log 2>&1
  make install > $srcdir/${_arch}-make-install-out.log 2>&1
  popd
done

exit 0
