#!/bin/bash

# Copyright 2025  Heinz Wiesinger, Amsterdam, The Netherlands
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cd $(dirname $0) ; CWD=$(pwd)

PKGNAM=containers-common
VERSION=${VERSION:-20260112}
BUILD=${BUILD:-1}

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$(uname -m)" in
    i?86) ARCH=i586 ;;
    arm*) readelf /usr/bin/file -A | egrep -q "Tag_CPU.*[4,5]" && ARCH=arm || ARCH=armv7hl ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
    *) ARCH=$(uname -m) ;;
  esac
  export ARCH
fi

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PKGNAM-$VERSION-$ARCH-$BUILD.txz"
  exit 0
fi

if [ "$ARCH" = "i586" ]; then
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "s390" ]; then
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  LIBDIRSUFFIX="64"
elif [ "$ARCH" = "armv7hl" ]; then
  LIBDIRSUFFIX=""
else
  LIBDIRSUFFIX=""
fi

TMP=${TMP:-/tmp}
PKG=$TMP/package-$PKGNAM

rm -rf $PKG
mkdir -p $TMP $PKG

cd $PKG

mkdir -p $PKG/etc/containers/containers.conf.d
mkdir -p $PKG/etc/containers/oci/hooks.d
mkdir -p $PKG/etc/containers/registries.conf.d
mkdir -p $PKG/etc/containers/registries.d
mkdir -p $PKG/usr/share/containers/oci/hooks.d
mkdir -p $PKG/usr/lib/tmpfiles.d/

# Custom config
install -m 644 $CWD/tmpfiles.conf $PKG/usr/lib/tmpfiles.d/$PKGNAM.conf
install -m 644 $CWD/mounts.conf $PKG/etc/containers/

# From containers/common
install -m 644 $CWD/containers.conf $PKG/etc/containers/
install -m 644 $CWD/containers.conf $PKG/usr/share/containers/

zcat $CWD/containers.conf.patch.gz | patch -p0 --verbose -d $PKG/etc/containers/ || exit 1
zcat $CWD/containers.conf.patch.gz | patch -p0 --verbose -d $PKG/usr/share/containers/ || exit 1

sed -i "s|lib/podman|lib$LIBDIRSUFFIX/podman|g" $PKG/etc/containers/containers.conf
sed -i "s|lib/podman|lib$LIBDIRSUFFIX/podman|g" $PKG/usr/share/containers/containers.conf

install -m 644 $CWD/seccomp.json $PKG/etc/containers/
install -m 644 $CWD/seccomp.json $PKG/usr/share/containers/

# From containers/image
install -m 644 $CWD/registries.conf $PKG/etc/containers/

# From containers/shortnames
install -m 644 $CWD/shortnames.conf $PKG/etc/containers/registries.conf.d/00-shortnames.conf

# From containers/skopeo
install -m 644 $CWD/default-policy.json $PKG/etc/containers/policy.json
install -m 644 $CWD/default.yaml $PKG/etc/containers/registries.d/

# From containers/storage
install -m 644 $CWD/storage.conf $PKG/etc/containers/
install -m 644 $CWD/storage.conf $PKG/usr/share/containers/

mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $TMP/$PKGNAM-$VERSION-$ARCH-$BUILD.txz
