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

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

validpgpkeys=('7C0135FB088AAF6C66C650B9BB5869F064EA74AB') # Chet Ramey

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

  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.3-cygwin.patch
  patch -p1 -i $srcdir/0002-bash-4.3-msysize.patch
  patch -p1 -i $srcdir/0003-bash-4.3-readline.patch
  patch -p1 -i $srcdir/0004-bash-4.3-deprecations.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

  # http://hmarco.org/bugs/bash_4.3-setuid-bug.html (FS#40663)
  patch -p0 -i $srcdir/privmode-setuid-fail.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

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

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

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

package_bash() {
  depends=('msys2-runtime') # 'libintl' 'libiconv' 'libreadline>=6.3' 'ncurses' 'gcc-libs'
  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
}

md5sums=('81348932d5da294953e15d4814c74dd1'
         'SKIP'
         'e6091db6a795001bb7aa0dbd753fb0ec'
         '03696955885df2bae6ecc647157cf808'
         '99c6789826a493331c186d9387ce3442'
         'fc268eb1264c79b7d0c6f43f82d3d6be'
         '26734e34adf9bec225bf29dab4c2767f'
         'fd89ff75ed13a89fb35291129ab60f0f'
         'd4136a8b13a3bf03e09fc89788f11409'
         'a577d42e38249d298d6a8d4bf2823883'
         '1ab682b4e36afa4cf1b426aa7ac81c0d'
         'SKIP'
         '8fc22cf50ec85da00f6af3d66f7ddc1b'
         'SKIP'
         'a41728eca78858758e26b5dea64ae506'
         'SKIP'
         'bf8d53d227829d67235927689a03cc7a'
         'SKIP'
         'c0c00935c8b8ffff76e8ab77e7be7d15'
         'SKIP'
         '6f01e364cd092faa28dd7119f47ddb5f'
         'SKIP'
         'dcf471d222bcd83283d3094e6ceeb6f8'
         'SKIP'
         'f7553416646dc26c266454c78a916d36'
         'SKIP'
         '7e73d2151f4064b484a4ba2c4b09960e'
         'SKIP'
         'a275463d21735bb6d7161f9fbd320d8f'
         'SKIP'
         'c17103ee20420d77e46b224c8d3fceda'
         'SKIP'
         '3e2a057a19d02b3f92a3a09eacbc03ae'
         'SKIP'
         'fb377143a996d4ff087a2771bc8332f9'
         'SKIP'
         '1a1aaecc99a9d0cbc310e8e247dcc8b6'
         'SKIP'
         '4f04387458a3c1b4d460d199f49991a8'
         'SKIP'
         '90e759709720c4f877525bebc9d5dc06'
         'SKIP'
         '11e4046e1b86070f6adbb7ffc89641be'
         'SKIP'
         'cd5a9b46f5bea0dc0248c93c7dfac011'
         'SKIP'
         'cff4dc024d9d3456888aaaf8a36ca774'
         'SKIP'
         '167839c5f147347f4a03d88ab97ff787'
         'SKIP'
         '1d350671c48dec30b34d8b81f09cd79d'
         'SKIP'
         '11c349af66a55481a3215ef2520bec36'
         'SKIP'
         'b3cb0d80fd0c47728264405cbb3b23c7'
         'SKIP'
         'b5ea5600942acceb4b6f07313d2de74e'
         'SKIP'
         '193c06f578d38ffdbaebae9c51a7551f'
         'SKIP'
         '922578e2be7ed03729454e92ee8d3f3a'
         'SKIP'
         '8ff6948b16f2db5c29b1b9ae1085bbe7'
         'SKIP'
         'dd51fa67913b5dca45a702b672b3323f'
         'SKIP'
         '0729364c977ef4271e9f8dfafadacf67'
         'SKIP'
         'efb709fdb1368945513de23ccbfae053'
         'SKIP'
         '236df1ac1130a033ed0dbe2d2115f28f'
         'SKIP'
         '2360f7e79cfb28526f80021025ea5909'
         'SKIP'
         'b551c4ee7b8713759e4143499d0bbd48'
         'SKIP'
         'c9a56fbe0348e05a886dff97f2872b74'
         'SKIP'
         'e564e8ab44ed1ca3a4e315a9f6cabdc9'
         'SKIP'
         'b00ff66c41a7c0f06e191200981980b0'
         'SKIP'
         'be2a7b05f6ae560313f3c9d5f7127bda'
         'SKIP'
         '61e0522830b24fbe8c0d1b010f132470'
         'SKIP'
         'a4775487abe958536751c8ce53cdf6f9'
         'SKIP')
