# Maintainer: David Runge <dvzrv@archlinux.org>

pkgname=containers-common
pkgver=0.58.2
pkgrel=1
epoch=1
_image_pkgver=5.30.0
_podman_pkgver=5.0.1
_shortnames_pkgver=2023.02.20
_skopeo_pkgver=1.15.0
_storage_pkgver=1.53.0
pkgdesc="Configuration files and manpages for containers"
arch=(any)
url="https://github.com/containers"
license=(Apache-2.0)
depends=('container-network-stack>1')
makedepends=(go-md2man)
checkdepends=(ripgrep)
backup=(
  etc/containers/containers.conf
  etc/containers/mounts.conf
  etc/containers/policy.json
  etc/containers/registries.conf
  etc/containers/seccomp.json
  etc/containers/storage.conf
)
# configuration override directories need to exist
options=(emptydirs)
source=(
  common-$pkgver.tar.gz::https://github.com/containers/common/archive/v$pkgver.tar.gz
  image-$_image_pkgver.tar.gz::https://github.com/containers/image/archive/v$_image_pkgver.tar.gz
  podman-$_podman_pkgver.tar.gz::https://github.com/containers/podman/archive/v$_podman_pkgver.tar.gz
  skopeo-$_skopeo_pkgver.tar.gz::https://github.com/containers/skopeo/archive/v$_skopeo_pkgver.tar.gz
  storage-$_storage_pkgver.tar.gz::https://github.com/containers/storage/archive/v$_storage_pkgver.tar.gz
  shortnames-$_shortnames_pkgver.tar.gz::https://github.com/containers/shortnames/archive/refs/tags/v$_shortnames_pkgver.tar.gz
  mounts.conf
  tmpfiles.conf
  $pkgname-0.48.0-init_path.patch
)
sha512sums=('1a56b03fcf8b466ab1d523e23736c8b0fe2604d05f0a5d9c3023e1d917ad87a6ac2c912c7d240de4498b184dc252146897102e916be466c324aec1115e308558'
            '89cd0d482bb5d70037ff51413b7abca01d36bc7f39e9dc130c78462e501dae32193137a367d81532afd216a7432e127728147e6e839d5d99eeb6b6680f31d4dc'
            '4ab64beea9b23839caa35bf8ec0f097ed16b4d2f448ce16039bb55672ac8b1d8d8cadf86c21eb32dc2c1d30dabb5b57a141823aa5fc3f14f09741f76cffa6023'
            '71fb94fd2cf9937bf2dd969c8b1b74f5b66eb2ac2bd9358e8c8983c3fd6548311ca274ed2f8c3f088c3c9d6032962075da1890449dcd25dcfd49882acbfa20de'
            'ea4a1d1899208eb8861e36beba206724b1f55cfd6007bc5a90c3a6a5e02835b4a2985814dc9363c31dcdc81a3fb331b29f51f5523628edb8d9c64c465d6dcaa3'
            '856dbbeb2acda276e9605bd1ecec0f8d65952c597ee2af61dd8909d7d3c04e5ef06c40b69ec4a98f79e623c536850f614c1b0af3a19637e300e7d3a285933193'
            '11fa515bbb0686d2b49c4fd2ab35348cb19f9c6780d6eb951a33b07ed7b7c72a676627f36e8c74e1a2d15e306d4537178f0e127fd3490f6131d078e56b46d5e1'
            '2b187c119db95cda439f36509545fd0f45530c69d9139823387f9aa68ea2e9c4b3dee8ee21a517daa73a88ac63cc694e0e170061bfc1503425c21868b2ccf7dd'
            '4a6526d01f192f0eb4dcbd28c019a2b0db6dc2128af644e8e89992e5dcfa45a02c739b06ee01e22606b5cb847213c002f8ab5f87a576846ac73f73eed9b2b469')
b2sums=('9b8ed27053138202b897229024fdf91625bf81aef9cc36c40fc10095bcb307ef700b65b8e5c7e8c36f4b8023f5de81678d8aee2e7a5b5f01b6e8f1bb934b30c6'
        '5952ab703b6df793f326dd4fc82f50c5f672e2b9d142e0bc706014092420e3563599d9f1c1f65f743e1a8144b91f92b2326d7bf4c3024b5efb466d777a8ad613'
        '433949d70e7984a278d5231fe4a480d0c758a67e6df30f587f25da727440ba944cf38bb979ab6b6375f0ba65038c6b4c5a8ca73c6bc73df57b92089c5f6c072e'
        'b6804561ec96ce7f987ccdaf5a89aa7f5fd0ed2da1307467b4c1857386508d88e3fd28f066f49b136962e232b89594484610fc4f6b494be6a98b64908900865b'
        '939ecca8a948165c42453d5461429d46249d73582869793969f9d4ae52d0a9fec25e6c39cd13190dad0730d9a17de7af1f237b3cf5434fd30f442c78e57ee7b3'
        '165a4c51bd284078990c94059145bb6b4bd5de4d8595eb2ccd2acc6fe8561e7edc300b7edadc684675ee281d7ada1c3a6d5aa0117c640ec22c2d2ef7e46486b5'
        '2f4b0af3271103362a898e7fcc3ec05f06755902ad664ac3107bb8debb8b2ac0d50de311d5fc651279a817a56e3ff05864a7e77c0d8fc628ff7411bfb98c9b69'
        '1cd6884f06269c3e6cbdfa542bdf8e178574062ec11252defc48187b60a98d0193353cc8a12ba186d338ce7da6b879a1e9dc249d2f40c28fe997c433c91e8e0e'
        '89e95f468785f6ca1309b0de37921702bd4eb6fb191afc0d93454bec7b7096a1b84e19408b5a0abcdfd89ce2ebd228879cbc42a0d409425fb41caab6a8f049f5')

prepare() {
  sed -r 's/(GOMD2MAN = ).*/\1 go-md2man/' -i storage-$_storage_pkgver/docs/Makefile
  patch -Np1 -d common-$pkgver -i ../$pkgname-0.48.0-init_path.patch
  mkdir -vp build/{man1,man5}
}

build() {

  (
    printf "Creating common man pages...\n"
    cd common-$pkgver
    for _man_page in docs/*.md pkg/hooks/docs/*.md; do
      go-md2man -in $_man_page -out ../build/man5/$(basename ${_man_page//.md})
    done
  )

  (
    printf "Creating image man pages...\n"
    cd image-$_image_pkgver
    mkdir -vp man5
    mv -v docs/*.5.md man5/
    for _man_page in docs/*.md; do
      go-md2man -in $_man_page -out ../build/man1/$(basename ${_man_page//.md}).1
    done
    for _man_page in man5/*.md; do
      go-md2man -in $_man_page -out ../build/man5/$(basename ${_man_page//.md})
    done
  )

  (
    printf "Creating storage documentation...\n"
    cd storage-$_storage_pkgver
    make -C docs
  )
}

check() {
  local _podman_common_ver
  _podman_common_ver="$(rg "github.com/containers/common" podman-$_podman_pkgver/go.mod | cut -d ' ' -f2 | sed 's/v//')"
  _podman_image_ver="$(rg "github.com/containers/image" podman-$_podman_pkgver/go.mod | cut -d ' ' -f2 | sed 's/v//')"
  _podman_storage_ver="$(rg "github.com/containers/storage" podman-$_podman_pkgver/go.mod | cut -d ' ' -f2 | sed 's/v//')"

  printf "Common version in podman: %s\n" $_podman_common_ver
  printf "Image version in podman: %s\n" $_podman_image_ver
  printf "Storage version in podman: %s\n" $_podman_storage_ver

  # NOTE: we are comparing the major.minor version of containers/common required by podman with the one of containers/common that we are trying to build
  # https://github.com/containers/common/issues/923
  if (( $(vercmp "${_podman_common_ver%.*}" "${pkgver%.*}") != 0 )); then
    printf "The podman package in version %s requires containers/common in version %s but we are trying to build version %s\n" $_podman_pkgver ${_podman_common_ver%.*} ${pkgver%.*}
    exit 1
  fi
  if (( $(vercmp "${_podman_image_ver%.*}" "${_image_pkgver%.*}") != 0 )); then
    printf "The podman package in version %s requires containers/image in version %s but we are trying to use version %s\n" $_podman_pkgver ${_podman_image_ver%.*} ${_image_pkgver%.*}
    exit 1
  fi
  if (( $(vercmp "${_podman_storage_ver%.*}" "${_storage_pkgver%.*}") != 0 )); then
    printf "The podman package in version %s requires containers/storage in version %s but we are trying to use version %s\n" $_podman_pkgver ${_podman_storage_ver%.*} ${_storage_pkgver%.*}
    exit 1
  fi
}

package() {
  # directories
  install -vdm 755 "$pkgdir/etc/containers/containers.conf.d/"
  install -vdm 755 "$pkgdir/etc/containers/oci/hooks.d/"
  install -vdm 755 "$pkgdir/etc/containers/registries.conf.d/"
  install -vdm 755 "$pkgdir/usr/share/containers/oci/hooks.d/"
  install -vDm 644 tmpfiles.conf "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"

  # configs
  install -vDm 644 mounts.conf -t "$pkgdir/etc/containers/"

  (
    cd common-$pkgver
    # configs
    install -vDm 644 pkg/config/containers.conf -t "$pkgdir/etc/containers/"
    install -vDm 644 pkg/config/containers.conf -t "$pkgdir/usr/share/containers/"
    install -vDm 644 pkg/seccomp/seccomp.json -t "$pkgdir/etc/containers/"
    install -vDm 644 pkg/seccomp/seccomp.json -t "$pkgdir/usr/share/containers/"
  )
  (
    cd image-$_image_pkgver
    # configs
    install -vDm 644 registries.conf -t "$pkgdir/etc/containers/"
  )
  (
    cd shortnames-$_shortnames_pkgver
    install -vDm 644 shortnames.conf "$pkgdir/etc/containers/registries.conf.d/00-shortnames.conf"
  )
  (
    cd skopeo-$_skopeo_pkgver
    # configs
    install -vDm 644 default-policy.json "$pkgdir/etc/containers/policy.json"
    install -vDm 644 default.yaml -t "$pkgdir/etc/containers/registries.d/"
  )
  (
    cd storage-$_storage_pkgver
    # configs
    install -vDm 644 storage.conf -t "$pkgdir/etc/containers/"
    install -vDm 644 storage.conf -t "$pkgdir/usr/share/containers/"
    # man pages
    install -vDm 644 docs/*.1 -t "$pkgdir/usr/share/man/man1/"
    install -vDm 644 docs/*.5 -t "$pkgdir/usr/share/man/man5/"
  )
  # man pages
  install -vDm 644 build/man1/*.1 "$pkgdir/usr/share/man/man1/"
  install -vDm 644 build/man5/*.5 "$pkgdir/usr/share/man/man5/"
}
