+#!/bin/sh
+set -e
+# Heavily inspired by <https://docs.pleroma.social/otp_en.html>
+
+if [ "$#" -ne 2 ]; then
+ echo 'Need domain name, mail_address as arguments.'
+ false
+fi
+domain="$1"
+mail="$2"
+
+# Install dependencies, set up firewall.
+config_tree_prefix="${HOME}/config/buster"
+./install_for_target.sh pleroma
+./copy_dirtree.sh "${config_tree_prefix}/etc_files" "" all "$@"
+nft -f /etc/nftables.conf
+
+# Set up letsencrypt certificate. TODO: Is it auto-renewed?
+certbot --nginx --agree-tos --redirect --no-eff-email -m "${mail}" -d "${domain}"
+
+# Prepare user and system info.
+adduser --system --shell /bin/false --home /opt/pleroma pleroma
+export FLAVOUR='amd64'
+
+# Download and unzip latest stable release, set up Pleroma dirs.
+su pleroma -s $SHELL -lc "
+curl 'https://git.pleroma.social/api/v4/projects/2/jobs/artifacts/stable/download?job=$FLAVOUR' -o /tmp/pleroma.zip
+unzip /tmp/pleroma.zip -d /tmp/
+"
+su pleroma -s $SHELL -lc "
+mv /tmp/release/* /opt/pleroma
+rmdir /tmp/release
+rm /tmp/pleroma.zip
+"
+mkdir -p /var/lib/pleroma/uploads
+chown -R pleroma /var/lib/pleroma
+mkdir -p /etc/pleroma
+chown -R pleroma /etc/pleroma
+
+# Configure and set up DB.
+su pleroma -s $SHELL -lc './bin/pleroma_ctl instance gen '\
+ '--output /etc/pleroma/config.exs '\
+ '--output-psql /tmp/setup_db.psql' \
+ "--domain ${domain}" \
+ '--instance-name plom-roma' \
+ "--admin-email ${mail}" \
+ "--notify-email ${mail}" \
+ '--dbhost localhost' \
+ '--dbname pleroma' \
+ '--dbuser pleroma' \
+ "--dbpass $(pwgen -s 100 1)" \
+ '--rum N' \
+ '--indexable N' \
+ '--uploads-dir /var/lib/pleroma/uploads' \
+ '--static-dir /var/lib/pleroma/static' \
+ '--listen-ip 127.0.0.1' \
+ '--listen-port 4000'
+su postgres -s $SHELL -lc "psql -f /tmp/setup_db.psql"
+su pleroma -s $SHELL -lc "./bin/pleroma_ctl migrate"
+
+# Prepare NGINX config for Pleroma.
+cp /opt/pleroma/installation/pleroma.nginx /etc/nginx/sites-available/pleroma.nginx
+sed -i "s/example\.tld/${domain}/g" /etc/nginx/sites-available/pleroma.nginx
+ln -s /etc/nginx/sites-available/pleroma.nginx /etc/nginx/sites-enabled/pleroma.nginx
+rm /etc/nginx/sites-enabled/default
+
+# Systemd integration.
+cp /opt/pleroma/installation/pleroma.service /etc/systemd/system/pleroma.service
+systemctl start pleroma
+systemctl enable pleroma
+
+# Only restart NGINX with Pleroma running.
+service nginx restart