[Unit]
Description=WordPress importer

# Dependency ordering
After=mysql@%i.service
Before=wordpress@%i.service
Before=backup@%i.timer

# Dependency binding
BindsTo=wordpress@%i.service

[Service]
Type=oneshot
RemainAfterExit=yes
Environment=DOMAIN=%i
ExecStartPre=/data/indiehosters/scripts/backup-init.sh
ExecStartPre=/data/indiehosters/scripts/import.sh
ExecStart=/bin/bash -euxc ' \
  cp /data/domains/%i/TLS/%i.pem /data/runtime/haproxy/approved-certs/%i.pem; \
  wordpress_folder=/data/domains/%i/wordpress; \
  if [ ! -d $wordpress_folder/wp-content ]; then \
    mkdir -p $wordpress_folder; \
    cd $wordpress_folder; \
    tar xvzf /data/indiehosters/blueprints/wordpress.tgz; \
    touch .htaccess; \
  fi; \
  cat /data/domains/%i/mysql/.env | sed s/MYSQL_PASS/DB_PASS/ > $wordpress_folder/.env'

[Install]
WantedBy=wordpress@%i.service
