#!/bin/sh
export TEXTDOMAINDIR='/usr/share/locale'
export TEXTDOMAIN='podsec'

. podsec-functions

mes=$(isSigstoreServer)
if [ -n "$mes" ]
then
  echo -ne $mes >&2
  echo "$(gettext 'Services cannot be started')."  >&2
  exit 1
fi

# Настройка sigStore
cd /etc/nginx/sites-enabled.d
sed -i \
  -e 's/server_name .*;/server_name sigstore.local;/' \
  -e 's|root .*|root /var/sigstore;|' \
  -e 's|# autoindex .*|autoindex on;|' \
  -e 's|# autoindex_localtime .*|autoindex_format json;|' \
  -e 's/listen \(.*\):80;.*/listen \1:81;/' \
  -e 's/listen.*127.0.0.1:81;/listen 0.0.0.0:81;/' ../sites-available.d/default.conf
ln -sf ../sites-available.d/default.conf .
systemctl enable --now nginx

# Настройка registry
podman volume create registry
sed -i -e 's|rootdirectory:.*|rootdirectory: /var/lib/containers/storage/volumes/registry/_data/|' -e 's/addr:.*/addr: :80/' /etc/docker-registry/config.yml
if systemctl | grep httpd2 | grep running >/dev/null 2>&1
then
  echo $(gettext 'Service httpd2 is running and conflicts with docker-registry')  >&2
  echo $(gettext 'Service httpd2 stopped')  >&2
  systemctl disable --now httpd2
fi
systemctl enable --now docker-registry

# Настройка trivy-server
systemctl enable --now trivy
