# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

pkgbase=bash
pkgname=('bash' 'bash-devel')
_basever=5.1
_patchlevel=008 #prepare for some patches
pkgver=${_basever}.${_patchlevel}
pkgrel=1
pkgdesc="The GNU Bourne Again shell"
arch=('i686' 'x86_64')
license=('GPL')
url="https://www.gnu.org/software/bash/bash.html"
validpgpkeys=('7C0135FB088AAF6C66C650B9BB5869F064EA74AB') # Chet Ramey
makedepends=('gettext-devel' 'libreadline-devel>=7.0' 'ncurses-devel')
source=(https://ftp.gnu.org/gnu/bash/bash-${_basever}.tar.gz{,.sig}
        0001-bash-4.4-cygwin.patch
        0002-bash-4.3-msysize.patch
        0005-bash-4.3-msys2-fix-lineendings.patch
        0006-bash-4.3-add-pwd-W-option.patch
        0007-fix-static-build.patch)
if [ $_patchlevel -gt 000 ]; then
  for (( p=1; p<=$((10#${_patchlevel})); p++ )); do
    source=(${source[@]} https://ftp.gnu.org/gnu/bash/bash-${_basever}-patches/bash${_basever//./}-$(printf "%03d" $p){,.sig})
  done
fi
noextract=(${pkgbase}-${_basever}.tar.gz)
validpgpkeys=('7C0135FB088AAF6C66C650B9BB5869F064EA74AB') # Chet Ramey

prepare() {
  [[ -d ${pkgbase}-${_basever} ]] && rm -rf ${pkgbase}-${_basever}
  tar -xzvf ${srcdir}/${pkgbase}-${_basever}.tar.gz || true
  cd ${srcdir}/${pkgname}-${_basever}

  # Remove patch-created file.
  [[ -f cross-build/msys32.cache ]] && rm -rf cross-build/msys32.cache

  for (( p=1; p<=$((10#${_patchlevel})); p++ )); do
    msg "applying patch bash${_basever//./}-$(printf "%03d" $p)"
    patch -p0 -i $srcdir/bash${_basever//./}-$(printf "%03d" $p)
  done

  patch -p1 -i ${srcdir}/0001-bash-4.4-cygwin.patch
  patch -p1 -i ${srcdir}/0002-bash-4.3-msysize.patch
  patch -p1 -i ${srcdir}/0005-bash-4.3-msys2-fix-lineendings.patch
  patch -p1 -i ${srcdir}/0006-bash-4.3-add-pwd-W-option.patch
  patch -p1 -i ${srcdir}/0007-fix-static-build.patch

  autoconf
}

build() {
  cd ${srcdir}/${pkgname}-$_basever

  ./configure --build=${CHOST} \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --enable-static-link \
    --enable-readline \
    --without-libintl-prefix \
    --without-libiconv-prefix \
    --with-installed-readline \
    --without-bash-malloc \
    --with-curses \
    bash_cv_dev_stdin=present \
    bash_cv_dev_fd=standard \
    bash_cv_termcap_lib=libncurses

  # Prepopulate the release level.
  expr ${pkgrel} > .build || :

  # CFLAGS+=" -D_STATIC_BUILD"
  make CPPFLAGS=-DWORDEXP_OPTION \
       HISTORY_LDFLAGS= \
       READLINE_LDFLAGS= \
       LOCAL_LDFLAGS='-Wl,--export-all,--out-implib,lib$(@:.exe=.dll.a)'
}

check() {
  cd ${srcdir}/${pkgname}-$_basever
  make check
}

package_bash() {
  provides=('sh')
  #install=bash.install

  cd ${srcdir}/${pkgname}-$_basever
  make DESTDIR=${pkgdir} install

  cp -fp ${pkgdir}/usr/bin/{ba,}sh.exe

  cd ${pkgdir}/usr/share/man/man1 > /dev/null
  echo '.so man1/bash.1' > sh.1
  echo '.so man1/bash_builtins.1.gz' > alias.1

  gzip alias.1
  for f in bg bind break builtin caller case cd command compgen complete \
    continue declare dirs disown do done elif else enable esac eval exec \
    exit export fc fg fi for function getopts hash help history if in jobs \
    let local logout popd pushd read readonly return select set shift shopt \
    source suspend then time times trap type typeset ulimit umask unalias \
    unset until wait while [ ; do
    cp -fp alias.1.gz $f.1.gz
  done
}

package_bash-devel() {
 pkgdesc="Bash headers and libraries"
 groups=('development')
 options=('staticlibs')
 backup=()

 mkdir -p ${pkgdir}/usr/{include,lib}
 cd ${srcdir}/bash-$_basever
 cp -f libbash.dll.a ${pkgdir}/usr/lib/

 mkdir -p  ${pkgdir}/usr/include/bash
 for f in [^y]*.h builtins/*.h include/*.h lib/{glob,tilde}/*.h
 do
   /usr/bin/install ${f} ${pkgdir}/usr/include/bash
 done
}

sha256sums=('cc012bc860406dcf42f64431bcd3d2fa7560c02915a601aba9cd597a39329baa'
            'SKIP'
            'af29ed2eb7bce53ec09ea691c0054d8e3b4ac726a797a919951a5cba93e772ef'
            'e1c76669ee5002b1ec06401224b94ece9a27fc722a6eb85ffaba272aad598001'
            'c866eeb043090c13ab2ce6777ca6a24b31929fb58c4a6a221bca541fcc5d9949'
            '9ef0a4686385a36c7a094ea8f5e9e973ee427c868952b2778f2f659347f74b32'
            '82578a38798bfcfd85707c08e4f7b22c3fa7acc7a01d083faf86eb923511d60e'
            'ebb07b3dbadd98598f078125d0ae0d699295978a5cdaef6282fe19adef45b5fa'
            'SKIP'
            '15ea6121a801e48e658ceee712ea9b88d4ded022046a6147550790caf04f5dbe'
            'SKIP'
            '22f2cc262f056b22966281babf4b0a2f84cb7dd2223422e5dcd013c3dcbab6b1'
            'SKIP'
            '9aaeb65664ef0d28c0067e47ba5652b518298b3b92d33327d84b98b28d873c86'
            'SKIP'
            'cccbb5e9e6763915d232d29c713007a62b06e65126e3dd2d1128a0dc5ef46da5'
            'SKIP'
            '75e17d937de862615c6375def40a7574462210dce88cf741f660e2cc29473d14'
            'SKIP'
            'acfcb8c7e9f73457c0fb12324afb613785e0c9cef3315c9bbab4be702f40393a'
            'SKIP'
            'f22cf3c51a28f084a25aef28950e8777489072628f972b12643b4534a17ed2d1'
            'SKIP')
