#!/usr/bin/env bash
set -Eeuo pipefail

INSTALL_ROOT=/opt/date-integration
DATA_ROOT=/srv/date-integration

install -d -m 0750 "$DATA_ROOT/mysql" "$DATA_ROOT/kafka" "$DATA_ROOT/pulsar"
chown -R 1000:1000 "$DATA_ROOT/kafka"

if [[ ! -e /swapfile ]]; then
  fallocate -l 2G /swapfile
  chmod 0600 /swapfile
  mkswap /swapfile
fi
swapon --show=NAME | grep -qx /swapfile || swapon /swapfile
grep -q '^/swapfile ' /etc/fstab || echo '/swapfile none swap sw 0 0' >> /etc/fstab

if systemctl list-unit-files date-integration.service >/dev/null 2>&1; then
  systemctl stop date-integration.service || true
fi

mysql_hash_file="$DATA_ROOT/.mysql-credentials-hash"
if [[ ! -s "$mysql_hash_file" ]] || [[ "$(<"$mysql_hash_file")" != '${mysql_credentials_hash}' ]]; then
  find "$DATA_ROOT/mysql" -mindepth 1 -delete
fi

pulsar_hash_file="$DATA_ROOT/.pulsar-credentials-hash"
if [[ ! -s "$pulsar_hash_file" ]] || [[ "$(<"$pulsar_hash_file")" != '${pulsar_credentials_hash}' ]]; then
  find "$DATA_ROOT/pulsar" -mindepth 1 -delete
fi

install -d -m 0700 "$INSTALL_ROOT"
echo '${compose_b64}' | base64 -d > "$INSTALL_ROOT/compose.yaml"
echo '${mysql_config_b64}' | base64 -d > "$INSTALL_ROOT/mysql.cnf"
echo '${mysql_init_b64}' | base64 -d > "$INSTALL_ROOT/mysql-init.sql"
echo '${secrets_env_b64}' | base64 -d > "$INSTALL_ROOT/secrets.env"
echo '${pulsar_secret_b64}' | base64 -d > "$INSTALL_ROOT/token-secret.key"
echo '${schema_registry_jaas_b64}' | base64 -d > "$INSTALL_ROOT/schema-registry.jaas"
echo '${schema_registry_users_b64}' | base64 -d > "$INSTALL_ROOT/schema-registry-users.properties"
echo '${service_unit_b64}' | base64 -d > /etc/systemd/system/date-integration.service
echo '${seed_script_b64}' | base64 -d > /usr/local/sbin/date-integration-seed

chmod 0600 \
  "$INSTALL_ROOT/compose.yaml" \
  "$INSTALL_ROOT/secrets.env" \
  "$INSTALL_ROOT/token-secret.key"
chown 999:999 "$INSTALL_ROOT/mysql.cnf" "$INSTALL_ROOT/mysql-init.sql"
chmod 0400 "$INSTALL_ROOT/mysql.cnf" "$INSTALL_ROOT/mysql-init.sql"
chown 1000:1000 "$INSTALL_ROOT/schema-registry.jaas" "$INSTALL_ROOT/schema-registry-users.properties"
chmod 0400 "$INSTALL_ROOT/schema-registry.jaas" "$INSTALL_ROOT/schema-registry-users.properties"
chmod 0750 /usr/local/sbin/date-integration-seed
chmod 0644 /etc/systemd/system/date-integration.service

systemctl daemon-reload
systemctl enable date-integration.service
systemctl start --no-block date-integration.service
for _ in $(seq 1 360); do
  if systemctl is-active --quiet date-integration.service; then
    break
  fi
  if systemctl is-failed --quiet date-integration.service; then
    systemctl status date-integration.service --no-pager
    exit 1
  fi
  sleep 5
done
if ! systemctl is-active --quiet date-integration.service; then
  systemctl status date-integration.service --no-pager
  echo "Timed out waiting for date-integration.service." >&2
  exit 1
fi

printf '%s' '${mysql_credentials_hash}' > "$mysql_hash_file"
printf '%s' '${pulsar_credentials_hash}' > "$pulsar_hash_file"
chmod 0600 "$mysql_hash_file" "$pulsar_hash_file"

echo DATE_INTEGRATION_CAPACITY_SNAPSHOT
free -m
docker stats --no-stream --format '{{.Name}} {{.MemUsage}}' || true
