# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=rebase
pkgver=4.5.0
pkgrel=1
pkgdesc="The Cygwin rebase distribution contains four utilities, rebase, rebaseall, peflags, and peflagsall"
arch=('i686' 'x86_64')
license=('custom')
url="https://www.cygwin.com/"
depends=('dash')
makedepends=('coreutils' 'git' 'grep' 'gzip' 'sed')
# options=('debug' '!strip')
source=(${pkgname}-${pkgver}::git://sourceware.org/git/cygwin-apps/rebase.git#tag=${pkgname}-${pkgver//./-}
        '001-rebase-4.4.1-msys2.patch'
        '002-rebaseall-add-python-exts.patch'
        '003-allow-non-database-mode-when-__CYGWIN__-__MSYS__.patch'
        '004-msys2-usr.patch'
        'autorebase.bat'
        'rebase.hook')
sha256sums=('SKIP'
            '2ca14be8dad62d97e3d3931bf656686086d497e4b7a271ee294db6a1b3e26dfb'
            'b06d561d82e7c32573082cf0207ca80275bab241c61debf2d3d8cc10cf79e31b'
            '4b789d2f6ae7de9afd778cce5823600ee0f53590b3e70e133dcafbb583d6cbbb'
            '35484af07e1df8b00821428b774744537106f99592d849ad7db26b41f570fa8c'
            '8e4099a29107a1d03031b198c3d142bbc31a40ff19298d6e099d9bcffd31b1b0'
            'a50a8bfdb28c47c1742f400287a8a7ba65f9893dee74c109aad517d46afed2a1')

prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  patch -p1 -i ${srcdir}/001-rebase-4.4.1-msys2.patch
  patch -p1 -i ${srcdir}/002-rebaseall-add-python-exts.patch
  patch -p1 -i ${srcdir}/003-allow-non-database-mode-when-__CYGWIN__-__MSYS__.patch
  patch -p1 -i ${srcdir}/004-msys2-usr.patch

  if check_option "strip" "n"; then
    sed -i "s/ -s//g" Makefile.in
  fi
  autoreconf -fi
}

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  ./configure --prefix=/usr \
    --sysconfdir=/etc \
    --with-posix-shell=/usr/bin/dash \
    CXXFLAGS="${CXXFLAGS} -static -static-libgcc -static-libstdc++" \
    CFLAGS="${CFLAGS} -static -static-libgcc"

  make
}

package() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  make DESTDIR=${pkgdir} install
  cp -f ${srcdir}/autorebase.bat ${pkgdir}
  if [ "${CARCH}" == "i686" ]; then
    mkdir -p ${pkgdir}/usr/share/libalpm/hooks
    cp -f ${srcdir}/rebase.hook ${pkgdir}/usr/share/libalpm/hooks
  fi
}
