#!/bin/bash

export TARGET

set -euo pipefail

# Update and install packages
apt-get update
# shellcheck disable=SC2086
DEBIAN_FRONTEND=noninteractive \
apt-get install -y \
  imagemagick \
  file \
  sudo \
  net-tools \
  iputils-ping \
  iproute2 \
  curl \
  git \
  git-lfs \
  jq \
  dos2unix \
  mysql-client \
  tzdata \
  rsync \
  nano \
  unzip \
  zstd \
  lbzip2 \
  nfs-common \
  libpcap0.8 \
  libnuma1 \
  libcap2-bin \
  jattach \
  ${EXTRA_DEB_PACKAGES}

# Clean up APT when done
apt-get clean

if [ "$TARGETARCH" = riscv64 ]; then
  KNOCKD_REPO_ORG=Opvolger/knock
fi
# Download and install patched knockd
knockdUrl="https://github.com/${KNOCKD_REPO_ORG}/releases/download/${KNOCKD_VERSION}/knock-${KNOCKD_VERSION}-$TARGET.tar.gz"
echo "Downloading knockd from $knockdUrl"
curl -fsSL -o /tmp/knock.tar.gz "$knockdUrl"
tar -xf /tmp/knock.tar.gz -C /usr/local/ && rm /tmp/knock.tar.gz
ln -s /usr/local/sbin/knockd /usr/sbin/knockd
setcap cap_net_raw=ep /usr/local/sbin/knockd
find /usr/lib -name 'libpcap.so.0.8' -execdir cp '{}' libpcap.so.1 \;

# Set git credentials globally
cat <<EOF >> /etc/gitconfig
[user]
	name = Minecraft Server on Docker
	email = server@example.com
EOF
git lfs install
