# Maintainer (AUR): lantw44 (at) gmail (dot) com
# Maintainer `echo V29qdGVrIEtvc2lvciA8a3dvanR1c0Bwcm90b25tYWlsLmNvbT4K | base64 -d`
# Modified from https://git.parabola.nu/abslibre.git/tree/pcr/guix/PKGBUILD?id=86a93aa1710795e0ef58dfc8b1aa7714a26253f9

# In order to verify the PGP signature of the source archive, you may need to
# use this command to download the needed public key:
#   gpg --keyserver pool.sks-keyservers.net \
#     --recv-keys 0x3CE464558A84FDC69DB40CFB090B11993D9AEBB5

pkgname=guix
pkgver=0.15.0
pkgrel=1
pkgdesc="A purely functional package manager for the GNU system"
arch=(i686 x86_64)
url="https://www.gnu.org/software/guix/"
license=('GPL3')
options=('!strip')
install="${pkgname}.install"

makedepends=(
  'guile-json'
  'guile-ssh>=0.10.2'
  'help2man')
depends=(
  'guile>=2.0.13'
  'libgcrypt'
  'make'
  'gnutls'
  'guile-sqlite3>=0.1.0'
  'guile-git-lib'
  'zlib'
  'gcc')
optdepends=(
  'bash-completion: to enable bash programmable completion'
  'guile-json: to import packages from cpan, gem, pypi'
  'guile-ssh: to offload builds to other machines')
source=(
  "https://alpha.gnu.org/gnu/${pkgname}/${pkgname}-${pkgver}.tar.gz"{,.sig}
  guix-daemon.initd
  guix-publish.initd
)
sha512sums=(
  '5e85ed03cde979d625df11d02d4f2ac7128bd944d11a763754e10a8e980f1eaeb721053fa12547bbc4289b33b3c7818bf992c7bfe79233f3a9555c96b8d52d7f'
  'SKIP'
  '87fadf21beb43f882badbc101e3f45c769bb752bdf4cf99804d226fa8cd35f58522e8a7f9d4e6ea19c64749e400cda4e593916f1bac22fcc6c72db3533e07d4d'
  'fadc7dfd64c16c3f8318a83d2bcab2d5009dc0d8fd9731186d0c3acaf8c807611d8eaee0808cdcfa5405e2dece6043961fb35b4488329f8b2ad9503cebbe06ec'
)

validpgpkeys=('3CE464558A84FDC69DB40CFB090B11993D9AEBB5') # Ludovic Courtès https://savannah.gnu.org/users/civodul

build() {
  local FISH_COMPLETION_DIR
  local BASH_COMPLETION_DIR
  local ZSH_COMPLETION_DIR
  
  if ! FISH_COMPLETION_DIR=$(pkg-config --variable=completionsdir fish)
    then FISH_COMPLETION_DIR=/usr/share/fish/completions
  fi
  if ! BASH_COMPLETION_DIR=$(pkg-config --variable=completionsdir bash-completion)
    then BASH_COMPLETION_DIR=/usr/share/bash-completion/completions
  fi
  if ! ZSH_COMPLETION_DIR=$(pkg-config --variable=completionsdir zsh)
    then ZSH_COMPLETION_DIR=/usr/share/zsh/site-functions
  fi

  cd "${srcdir}/${pkgname}-${pkgver}"
  ./bootstrap
  ./configure --prefix=/usr --sbindir=/usr/bin --sysconfdir=/etc \
              --libexecdir="/usr/lib/${pkgname}" --localstatedir=/var \
              --with-fish-completion-dir="${FISH_COMPLETION_DIR}" \
              --with-bash-completion-dir="${BASH_COMPLETION_DIR}" \
              --with-zsh-completion-dir="${ZSH_COMPLETION_DIR}" \
              --disable-rpath
  make
}

check() {
  # FIXME: There are too many failed tests and upstream developers haven't
  # made any response. All tests are temporarily skipped for now.
  # https://debbugs.gnu.org/32098
  
  return 0

  cd "${srcdir}/${pkgname}-${pkgver}"

  # Check whether the current working directory is too long
  local cwd_str="$(pwd)"
  local cwd_len="${#cwd_str}"
  if [ "${cwd_len}" -gt 46 ]; then
    error "${cwd_str} is too long."
    error "The working directory cannot be longer than 46 bytes."
    false
  fi

  # Make sure we have a valid shell accepting -c option
  SHELL=/bin/sh make check
}

package() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  make DESTDIR="${pkgdir}" install

  # Remove unused service files
  rm -r "${pkgdir}/usr/lib/"{upstart,systemd}

  # Remove unused selinux files
  rm -r "${pkgdir}/usr/share/selinux"

  install -Dm755 ../guix-daemon.initd "${pkgdir}/etc/init.d/guix-daemon"
  install -Dm755 ../guix-publish.initd "${pkgdir}/etc/init.d/guix-publish"
 
  # The default makepkg strip option cannot be used here because binaries
  # installed in /usr/share must not be stripped.
  # To keep user-defined 'strip' and 'debug' options useful, we still
  # depend on 'tidy_strip' function provided by makepkg to do the stripping
  # work. To make the function useful, we have to temporarily remove the
  # '!strip' option from 'options' array. However, assignments to 'options'
  # cause mksrcinfo to insert wrong lines to .SRCINFO, so they have to be
  # put in eval.
  eval 'options=()'
  cd "${pkgdir}/usr/bin"
  tidy_strip
  cd "${pkgdir}/usr/lib/guix"
  tidy_strip
  eval 'options=("!strip")'
}