# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: David Runge <dvzrv@archlinux.org>

pkgname=containers-common
pkgver=0.59.2
pkgrel=1
epoch=1
_image_pkgver=5.31.1
_podman_pkgver=5.1.2
_shortnames_pkgver=2023.02.20
_skopeo_pkgver=1.15.1
_storage_pkgver=1.54.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=('2eb28236a1171be8f344bd97567879ebe8b6901d8cf30310a77722046cb6486e5409e3b7bc157573b7b432a181def742d1678e8e1f67ea887163c8b13fc74c92'
            '175e0a2c7fd6c0b733df86907676f905c9c3fd26c4a905f02bb4ca6b62b1dcd0f45072055fb9161c959186ffeca3b4425489a0d334e30465586dcac8ecb28a4c'
            'adfd4dc5a1f7e1887ca5ffc0c6ec4593f051566010c3cc7050294fd50d2cab3d0257ff6ea4cc1abd2ba8b12adcfe0310446ac66bfc2280d9a9597294ecbcd474'
            '686d17da4a84d9029d688cfca08724576564ce2a05aed9380bf55059c244c90cffa3b21c2dbe84b19b746773e98d5f26e5ceaca58f7be26b656125977685c663'
            'e3535ab378bcb7b852d1ebf431e94696f53da469278f618c07dde2d3c8c6b5b3a4c71d65a9c14d1c1b4fa1e41a0550029468ef9ba1f24c1d3294a5aeec55b5fd'
            '856dbbeb2acda276e9605bd1ecec0f8d65952c597ee2af61dd8909d7d3c04e5ef06c40b69ec4a98f79e623c536850f614c1b0af3a19637e300e7d3a285933193'
            '11fa515bbb0686d2b49c4fd2ab35348cb19f9c6780d6eb951a33b07ed7b7c72a676627f36e8c74e1a2d15e306d4537178f0e127fd3490f6131d078e56b46d5e1'
            '2b187c119db95cda439f36509545fd0f45530c69d9139823387f9aa68ea2e9c4b3dee8ee21a517daa73a88ac63cc694e0e170061bfc1503425c21868b2ccf7dd'
            '4a6526d01f192f0eb4dcbd28c019a2b0db6dc2128af644e8e89992e5dcfa45a02c739b06ee01e22606b5cb847213c002f8ab5f87a576846ac73f73eed9b2b469')
b2sums=('7e313af1f5de32024f4d3631933d55c04ab738a91882a866e59b6e50cb6444f2142fc046eda5db98187ffe24d33711b891a078bf880fa14fbbe2b043a21ff9e4'
        '2d0e8dfcebf622d3841bc0e6326b45038486180d709457c254c5362646994c2e010230b6517b499011c6d33da004857c285e1836bcb5514f72f7255715bbaa43'
        '85647182109eb782e4ce89ff4dd017781b812558b08f70e0ac9e1a7449ab2cb6b9971383c3cfd9a8220827501b68353560cefc0ce6c4700c78fa75d74f619a8f'
        'aa688197fe0d803b6cb1f4e7c5c998a917e7d588fa41b9e6ede6d60fd1ef12f209e2f119bc29143bf92ae3349c1168e33f79cb36d6c7f5a1a22bcbf3f4e51792'
        '30f3e4287e9db46fca81ae6c08d3016ec4be6f38a4981d310f06cbc72e4125b458446fe49675f53232efdf0bec58e9cd18aa90424677e1b726338f1fb62b7172'
        '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/"
}
