# Maintainer: cygSimple <cygsimple@users.sourceforge.net>

# Cross build requires specifying --host, --build and sometimes --target.
# The --build is always $CHOST as specified in /etc/makepkg.conf
# The --host will be $CHOST if --target is also specified otherwise $THOST
# The --target is specified if the binary runs as a cross tool
if [ -z "${THOST}" ]; then
  THOST=${CHOST}
fi

# The pkgbase is the controller of the templated actions
pkgbase=pacman

# The pkgver comes from the upstream source distribution.
pkgver=5.1.1

# The pkgrel is the count of number of issues of the same pkgver.
pkgrel=1

# The pkgdesc here comes from an upstream description of the package source.
pkgdesc="A library-based package manager with dependency support"

# cygSimple is only providing x86_64
arch=('x86_64')

# The license comes from the upstream package source.
license=('GPL')

# The URL describing the package.
url=https://www.archlinux.org/packages/core/x86_64/pacman/

# The `eval'uated parts below require srcdir and pkgdir be set; but PKGBUILD
# is sourced before these are set so we must do it explicitly here.
srcdir=`pwd`/src
pkgdir=`pwd`/pkg

# Identify the package type to split the packages into.
pkgtypes=(
	'exe'
	'lib-static'
	#'doc-info'
	'doc-man'
	#'doc-html'
	#'doc-text'
	#'locale'
	'data'
	#'license'
	#'extra'
)

# We always provide an all meta package so that PACKAGE-all delivers
# every component of PACKAGE.
pkgtypes+=('all')

# If we are cross building then we cannot `make check`
# instead we create a -testing.pkg.tar with a -testing.install
# to execute the testing on the target host.
# pacman -U PACKAGE-testing.pkg.tar.xz will execute the `make check`
# after installing the /usr/src/testing/PACKAGE-PKGVER-PKGREL.
if test ! $CHOST = $THOST; then
  pkgtypes+=('testing')
  touch ${pkgbase}-testing.install
  cat <<'  EOD'>${pkgbase}-testing.install.in
post_install() {
  cd /usr/src/testing/@PKGBASE@-@PKGVER@-@PKGREL@/@PKGBLDDIR@
  make check
}

post_upgrade() {
  post_install
}

post_remove() {
  rm -rf /usr/src/testing/@PKGBASE@-@PKGVER@-@PKGREL@
}
  EOD
fi

# Then pkgname becomes an array of the concantenation of the pkgbase and the
# individual pkgtypes.
pkgname=()
for T in "${pkgtypes[@]}"; do
  pkgname+=("${pkgbase}-${T}")
  case ${T} in
    doc-info)
      touch ${pkgbase}-doc-info.install
      cat <<'      EOD'>${pkgbase}-doc-info.install.in
infodir=usr/share/info
filelist=@PKGBASE@.info.gz

post_install() {
  [ -x usr/bin/install-info ] || return 0
  for file in ${filelist}; do
    usr/bin/install-info $infodir/$file $infodir/dir 2> /dev/null
  done
}

post_upgrade() {
  post_install $1
}

pre_remove() {
  [ -x usr/bin/install-info ] || return 0
  for file in ${filelist}; do
    usr/bin/install-info --delete $infodir/$file $infodir/dir 2> /dev/null
  done
}
      EOD
      ;;
    *) ;;
  esac
done

# The extra binaries should be declared here.
pkgexealsoprovides=(
	#@PKGEXEALSOPROVIDES@
)

# The extra libraries should be declared here.
pkglibalsoprovides=(
	#@PKGLIBALSOPROVIDES@
)

# The source files both virtual and physical
source=(
	https://sources.archlinux.org/other/pacman/${pkgbase}-${pkgver}.tar.gz{,.sig}
	makepkg.conf
	pacman.conf
	0000-configure.ac.patch
	0000-scripts-libmakepkg-tidy-staticlibs.sh.in.patch
	0000-scripts-libmakepkg-tidy-strip.sh.in.patch
	0000-scripts-libmakepkg-util-compress.sh.in.patch
	0000-scripts-makepkg.sh.in.patch
	0000-scripts-pacman-key.sh.in.patch
	0000-src-pacman-callback.c.patch
	0000-src-pacman-check.c.patch
	0000-src-pacman-pacman.c.patch
	0000-test-pacman-ldconfig.stub.patch
	0000-test-pacman-Makefile.am.patch
	0000-test-pacman-pactest.py.patch
	0000-test-pacman-pmtest.py.patch
	0000-test-pacman-tests-ldconfig001.py.patch
	0000-test-pacman-tests-ldconfig002.py.patch
	0000-test-pacman-tests-ldconfig003.py.patch
	0000-test-pacman-tests-TESTS.patch
	0001-lib-libalpm-util.c.patch
	0006-scripts-makepkg.sh.in.patch
	0007-lib-libalpm-conflict.c.patch
	0010-src-pacman-Makefile-am.patch
	0010-src-util-Makefile-am.patch
)

# The sha256sums array is in the same order as the source array, use 'SKIP'
# if the source file doesn't or shouldn't have one.
sha256sums=(
	'be04b9162d62d2567e21402dcbabb5bedfdb03909fa5ec6e8568e02ab325bd8d'  #pacman-5.1.1.tar.gz
	'35abc7fcc189595a2dab4028a9a9688b8e522f54efd5d8d35a9971ebf0168c63'  #pacman-5.1.1.tar.gz.sig
	'3c2a189528a988c349b6c4371969864f63b84895ddc4751509d218ed7a70e9c5'  #makepkg.conf
	'4451b5813f08d01bb61a1b1bfcfe0c801b9442c6620f96bb30874932a33f8389'  #pacman.conf
	'1a4a2627b2e049e2992070831feef0e672ffc714a090a551d183a61485539a4c'  #0000-configure.ac.patch
	'da0bf469718f6730e5218829957d5ab1abe23db1cd5e46619f93785a8d56a103'  #0000-scripts-libmakepkg-tidy-staticlibs.sh.in.patch
	'b8a3241de7a4a55e1812edd9f8fcea7fcad277fa23400fb7486519359bdb9974'  #0000-scripts-libmakepkg-tidy-strip.sh.in.patch
	'5ad1a26e56551e38c60d6ef61f05c5147a7da5430ae50570a1b0779ba437e3f9'  #0000-scripts-libmakepkg-util-compress.sh.in.patch
	'3c78d59f93d5413ebe076c6cdc8934fdbe75bde934c9d55a139b20beab2788b8'  #0000-scripts-makepkg.sh.in.patch
	'c3267eed729f5af9de370357055998a07f9db922ab984c78c78272d66c7ee05a'  #0000-scripts-pacman-key.sh.in.patch
	'e5def5f9aa41c658d2865ff5676ac9e9a977c5f27479e3f1d0d2d3fb45fd1ae3'  #0000-src-pacman-callback.c.patch
	'd82f5b00bdf97be5506862292715c002a76ff7b85f72d509d4b072cb085073f2'  #0000-src-pacman-check.c.patch
	'7c078910ea75e0a427a46550a8e70dd453b74eb95199f9e173e05f932b518922'  #0000-src-pacman-pacman.c.patch
	'04eb756089ff3116ad79ca33c6a47d6ffe64c521b740438c3ce410c70dc447eb'  #0000-test-pacman-ldconfig.stub.patch
	'c5dee08399eb38a81fde8373b0d5a8dcf7f94b72d216b6f75bb252f4a760e414'  #0000-test-pacman-Makefile.am.patch
	'c670b13f3d76f11c24e63db1c8a17bf66959913853954d7674f262df83bf9155'  #0000-test-pacman-pactest.py.patch
	'c99d001ab599215a33439805758b5b6becae123fc91886aa4e286ee0cfdabdec'  #0000-test-pacman-pmtest.py.patch
	'f770e8e32d3b9551af4e9c6e0111f21952769949685f0ca098277a4d48160570'  #0000-test-pacman-tests-ldconfig001.py.patch
	'2c0a6f78c5a7b58dd292999b764e1bae2dc8286bacf4652766aff253ff3f769e'  #0000-test-pacman-tests-ldconfig002.py.patch
	'43a68644224b094158ffc6a555bb0ed73648c1990148c26293c68b081afb078e'  #0000-test-pacman-tests-ldconfig003.py.patch
	'd5533d7fceb3a10681e71300983b66459d7d47e0bd0c1e6ab07ada52e970b43a'  #0000-test-pacman-tests-TESTS.patch
	'24ea2c8dca37847e04894ebfd05d1cf5df49dc0c8089f5581c99caa19b77a7ef'  #0001-lib-libalpm-util.c.patch
	'7da22309e03eecb522eabdb701189b25e3408ff56f57f0b81ebd7799e5339e07'  #0006-scripts-makepkg.sh.in.patch
	'77333deb24789e93c50d56d48399c0e1d67137f8ba6822ef335ca169f79da08a'  #0007-lib-libalpm-conflict.c.patch
	'5eac462d5f7a7fd595307ad98d1620f72fb6f66a6a706656e46f7784154d352b'  #0010-src-pacman-Makefile-am.patch
	'e689ee4286252c7681eecb023713a030deb5a6beda2b45c99bdb6d010fac38db'  #0010-src-util-Makefile-am.patch
)

# The validpgpkeys for the upstream source packages if there are any.
# You need these keys in your keystore.
validpgpkeys=(
	'0xf99ffe0feae999bd'
)

# The pkgmake is used to identify the make program to use
# Not everyone likes GNU make
pkgmake=/usr/bin/make

# The pkgdestdir identifies deposition directory for creating packages.
pkgdestdir="${srcdir}/dest"

# The pkgmakeinstall is used to identify the method to use for installing
pkgmakeinstall="${pkgmake} DESTDIR=${pkgdestdir} install"

# The pkgmakecheck is used to identify the method for testing the package.
pkgmakecheck="${pkgmake} check"

# The pkgsrcdir identifies the source directory of the package.
pkgsrcdir="${srcdir}/${pkgbase}-${pkgver}"

# The pkgblddir identifies the build directory for the source.
pkgblddir="${srcdir}/${pkgbase}-${pkgver}"

# The pkgconfigure is used to identify the name of the configure script.
# Not everyone uses autoconf tools.
pkgconfigure="${pkgsrcdir}/configure"

# The pkgconfigoptions is used to add configure options that aren't standard.
# When specifying --target --host must be $CHOST.
pkgconfigoptions=(
	'--prefix=/usr'
	"--host=${THOST}"
	#"--host=${CHOST}"
	"--build=${CHOST}"
	#"--target=${THOST}"
	'--sysconfdir=/etc'
	'--sharedstatedir=/var'
	'--localstatedir=/var'
	'--disable-shared'
	'--enable-static'
	'--disable-git-version'
	'--with-scriptlet-shell=/usr/bin/bash'
	'--without-ldconfig'
	'--with-pkg-ext=.pkg.tar.xz'
)

# The make process depends on packages listed here.
makedepends=(
	'openssl-lib>=1.1.1'
	'bzip2-lib>=1.0.6'
	'zlib-lib>=1.2.11'
	'libarchive-lib>=3.3.2'
	'libcrypt-lib>=2.1'
)

# The unit testing process depends on packages listed here.
checkdepends=(
	#@CHECKDEPENDS@
)

# ####################### #
#      Helper macros      #
# ####################### #

##
 # Apply patch with a message giving the name of the patch file
 #
 # @param mixed
 #   - integer identifying the -p number (defaults to 1) or
 #   - array of the names of the patch files
 # @param optional but required if Param 1 is an integer
 #   - array of the names of the patch files
 # @return the exit status of the patch operation
 #
 # @example
 #  apply_patch_with_msg some_file.patch
 #  apply_patch_with_msg 3 some_file.patch some_file2.patch
 #
 # @note
 #  The patch file must reside in ${srcdir}.
 ##
apply_patch_with_msg() {
  local plvl
  if [[ $1 == +([0-9]) ]]; then
    plvl=$1
    shift
  else
    plvl=1
  fi
  for F in "$@"
  do
    msg2 "Applying patch file $F"
    patch -Nbp${plvl} -i "${srcdir}/${F}"
  done
}

##
 # Delete a file, link or directory if it exists
 #
 # @param An array of files, links and/or directories
 # @return the exit status of the last rm operation or unchanged exit status
 ##
del_file_exists() {
  local _f
  for _f in "$@"
  do
    if [ -l $_f ] || [ -f $_f ]; then
      rm -f $_f
    fi
    if [ -d $_f ]; then
      rm -rf $_f
    fi
  done
}

##
 # Does function exist?
 #
 # @param A string identifying the name of a function
 # @return boolean true or false
 #
 # @example
 #  fn_exists fn_exists && this || that
 #
 #  if fn_exhsts fn_exists; then
 #    this
 #  else
 #    that
 #  fi
 ##
fn_exists() {
  test x$(type -t $1) = xfunction
}

##
 # Does NEEDLE exists in the values of HAYSTACK
 #
 # @param needle - the item to search for
 # @param haystack - the array containing items
 # @return boolean true or false
 #
 # @example
 #  in_array 'needle' "${haystack[@]}" && this || that
 #
 #  if in_array 'needle' "${haystac[@]}"; then
 #    this
 #  else
 #    that
 #  fi
 ##
in_array() {
  local needle haystack
  printf -v needle -- ',,%q' "$1"
  shift
  printf -v haystack -- ',,%q' "$@"
  [[ "${haystack},," =~ "${needle},," ]]
}

# ####################### #
#         Hooks           #
# ####################### #
# prefix pre_ or post_ to #
#  any of the standard    #
#  functions (prepare,    #
#  build, check) to add   #
#  operations for each    #
#  step.                  #
# Create auto_conf() if   #
#  some configuration     #
#  script needs to exe-   #
#  cute to create the     #
#  configure scripts.     #
#  E.G. autoreconf -vfi   #
#                         #
# @see gettext/PKGBUILD   #
#  for examples of use.   #
# ####################### #

# Change or remove auto_conf as necessary
# autoreconf is the more common option so included for convenience
# @see gettext/PKGBUILD:auto_conf() for another example
auto_conf() {
  autoreconf -vfi
}

# ####################### #
#     Required macros     #
# ####################### #
prepare() {
  fn_exists pre_prepare && pre_prepare || true
  # Patches are always listed in the source array.  This scheme will force the
  # patch to be at level 1 deep and the file name to end in .patch
  cd ${pkgbase}-${pkgver}
  for F in ${source[@]}; do
    if test "${F##*.}" = 'patch'; then
      apply_patch_with_msg $F
    fi
  done
  fn_exists auto_conf && auto_conf || true
  fn_exists post_prepare && post_prepare || true
}

build() {
  fn_exists pre_build && pre_build || true
  cd ${pkgbase}-${pkgver}
  $pkgconfigure \
	${pkgconfigoptions[@]}
  ${pkgmake}
  ${pkgmakeinstall}
  fn_exists post_build && post_build || true
}

check() {
  fn_exists pre_check && pre_check || true
  if test "${THOST}" = "${CHOST}"; then
    cd ${pkgbase}-${pkgver}
    ${pkgmakecheck}
  else
    msg2 "NOTE: The make check function is disabled when cross compiling."
  fi
  fn_exists post_check && post_check || true
}

# ####################### #
#      Package macros     #
# ####################### #

# The pdir variable is used because of the timing of the eval.
pdir=${pkgdir}/${pkgbase}

in_array 'all' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-all() {
  pkgdesc='${pkgbase^^} all package'
  depends=()
  for I in ${pkgtypes[@]}; do
    case \$I in
	    testing|all) ;;
	    *) depends+=(${pkgbase}-\${I}) ;;
    esac
  done
}"

in_array 'exe' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-exe() {
  pkgdesc='${pkgbase^^} executable files'
  provides=('${pkgbase}=${pkgver}' ${pkgexealsoprovides[@]})
  groups=('executables')
  depends=(
	'${pkgbase}-data'
	'bash>=4.2.045'
	'gettext'
	'gnupg'
	'newlib-cygwin'
	'curl'
	'pacman-mirrorlist'
	'cygsimple-keyring'
	'which'
	'bzip2'
	'xz'
  )

  mkdir -p ${pdir}-exe/usr/share/bash-completion/completions
  cp -a ${pkgdestdir}/usr/bin ${pdir}-exe/usr/
  cp -a ${pkgdestdir}/etc ${pdir}-exe/
  install -m644 ${srcdir}/pacman.conf ${pdir}-exe/etc/pacman.conf
  install -m644 ${srcdir}/makepkg.conf ${pdir}-exe/etc/makepkg.conf
  install -dm755 ${pdir}-exe/usr/share/bash-completion/completions
  mv ${pdir}-exe/etc/bash_completion.d/pacman ${pdir}-exe/usr/share/bash-completion/completions/
  rmdir ${pdir}-exe/etc/bash_completion.d
  ln -s pacman '${pdir}-exe/usr/share/bash-completion/completions/makepkg'
  ln -s pacman '${pdir}-exe/usr/share/bash-completion/completions/pacman-key'
  cp -a ${pkgdestdir}/var ${pdir}-exe/
}"

in_array 'lib-static' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-lib-static() {
  pkgdesc='${pkgbase^^} headers and static libraries'
  provides=('${pkgbase}-lib=${pkgver}' '${pkgbase}-devel=${pkgver}' ${pkglibalsoprovides[@]})
  groups=('libraries' 'libstatic')

  mkdir -p ${pdir}-lib-static/usr
  cp -a ${pkgdestdir}/usr/{include,lib} ${pdir}-lib-static/usr/
  if [ -e ${pkgdestdir}/usr/share/aclocal ]; then
    mkdir -p ${pdir}-lib-static/usr/share
    cp -a ${pkgdestdir}/usr/share/aclocal ${pdir}-lib-static/usr/share/
  fi
}"

in_array 'doc-info' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-doc-info() {
  pkgdesc='${pkgbase^^} info documentation'
  groups=('documentation' 'doc-info' '${pkgbase}-doc')
  depends=('info-exe')
  options=('!strip')

  cat ${startdir}/${pkgbase}-doc-info.install.in | \
    sed -e 's/@PKGBASE@/${pkgbase}/' \
        -e 's/@PKGVER@/${pkgver}/' \
        -e 's/@PKGREL@/${pkgrel}/' \
        -e 's~@PKGBLDDIR@~`basename ${pkgblddir}`~' > \
	  ${startdir}/${pkgbase}-doc-info.install
  install=${pkgbase}-doc-info.install

  mkdir -p ${pdir}-doc-info/usr/share
  cp -a ${pkgdestdir}/usr/share/info ${pdir}-doc-info/usr/share/
}"

in_array 'doc-man' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-doc-man() {
  pkgdesc='${pkgbase^^} man documentation'
  groups=('documentation' 'doc-man' '${pkgbase}-doc')
  depends=('man-exe')
  options=('!strip')

  mkdir -p ${pdir}-doc-man/usr/share
  cp -a ${pkgdestdir}/usr/share/man ${pdir}-doc-man/usr/share/
}"

in_array 'doc-html' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-doc-html() {
  pkgdesc='${pkgbase^^} html documentation'
  groups=('documentation' 'doc-html' '${pkgbase}-doc')
  options=('!strip')

  mkdir -p ${pdir}-doc-html/usr/share/doc/html
  cp -a ${pkgdestdir}/usr/share/doc/${pkgbase}/*.html ${pdir}-doc-html/usr/share/doc/hmtl/${pkgbase}/
}"

in_array 'doc-text' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-doc-text() {
  pkgdesc='${pkgbase^^} text documentation'
  groups=('documentation' 'doc-text' '${pkgbase}-doc')
  options=('!strip')

  mkdir -p ${pdir}-doc-text/usr/share/doc/text/${pkgbase}
  cp -a ${pkgdestdir}/usr/share/doc/${pkgbase}/* ${pdir}-doc-text/usr/share/doc/text/${pkgbase}/
  rm -f ${pdir}-doc-text/usr/share/doc/text/${pkgbase}/*.html
}"

in_array 'locale' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-locale() {
  pkgdesc='${pkgbase^^} locale message database files'
  groups=('locale')
  options=('!strip')

  mkdir -p ${pdir}-locale/usr/share
  cp -a ${pkgdestdir}/usr/share/locale ${pdir}-locale/usr/share/
}"

in_array 'data' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-data() {
  pkgdesc='${pkgbase^^} data files'
  groups=('data')
  options=('!strip')

  mkdir -p 						${pdir}-data/usr/share
  cp -a ${pkgdestdir}/usr/share/${pkgbase} 		${pdir}-data/usr/share/
  cp -a ${pkgdestdir}/usr/share/makepkg 		${pdir}-data/usr/share/
  cp -a ${pkgdestdir}/usr/share/makepkg-template 	${pdir}-data/usr/share/
  cp -a ${pkgdestdir}/usr/share/libalpm 		${pdir}-data/usr/share/
  cp -a ${pkgdestdir}/usr/share/zsh 			${pdir}-data/usr/share/
}"

in_array 'license' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-license() {
  pkgdesc '${pkgbase^^} custom license file'
  groups=('license')
  options=('!strip')

  mkdir -p ${pdir}-license/usr/share/licenses/${pkgbase}
  cp -f ${srcdir}/${pkgbase}-${pkgver}/LICENSE ${pdir}-license/usr/share/licenses/${pkgbase}/
}"

in_array 'extra' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-extra() {
  pkgdesc '${pkgbase^^} custom license file'
  groups=('extra')
  options=('!strip')

  # create what you need in ${pdir}-extra/
}"

in_array 'testing' "${pkgtypes[@]}" && \
eval "package_${pkgbase}-testing() {
  pkgdesc='${pkgbase^^} preconfigured and built files to be used for native host testing'
  groups=('testing')
  provides=('${pkgbase}-testing=${pkgver}')
  options=('staticlibs' '!strip' 'libtool')
  install='${pkgbase}-testing.install'

  cat ${startdir}/${pkgbase}-testing.install.in | \
    sed -e 's/@PKGBASE@/${pkgbase}/' \
        -e 's/@PKGVER@/${pkgver}/' \
        -e 's/@PKGREL@/${pkgrel}/' \
        -e 's~@PKGBLDDIR@~`basename ${pkgblddir}`~' > \
	  ${startdir}/${pkgbase}-testing.install
  mkdir -p ${pdir}-testing/usr/src/testing/${pkgbase}-${pkgver}-${pkgrel}
  cp -a ${pkgsrcdir} ${pdir}-testing/usr/src/testing/${pkgbase}-${pkgver}-${pkgrel}/
  if test ! '$pkgblddir' = '$pkgsrcdir'; then
    cp -a ${pkgblddir} ${pdir}-testing/usr/src/testing/${pkgbase}-${pkgver}-${pkgrel}/
  fi
  find ${pdir}-testing/usr/src/testing -type d -name autom4te.cache -prune -exec rm -rf {} \;
  for I in \`grep -rl ${srcdir}/ ${pdir}-testing/usr/src/testing/\`
  do
    sed -e 's~${srcdir}/~/usr/src/testing/${pkgbase}-${pkgver}-${pkgrel}/~g' \$I > \${I}.new
    mv -f \${I}.new \${I}
  done
}"

# Need to reset the exit status due to the use of in_array.
true
