post_install() {
	echo "##########################################"
	echo "### Mastodon Installation Instructions ###"
	echo "##########################################"
	echo
	echo "### To setup Mastodon, enable and start PostgreSQL and Redis ###"
	echo "systemctl enable --now postgresql redis"
	echo
	echo "### Create the Mastodon PostgreSQL user and grant it the ability to create databases with ###"
	echo "sudo -u postgres createuser -d mastodon"
	echo
	echo "### Then, run the following commands ###"
	echo "cd /var/lib/mastodon"
	echo "sudo chown mastodon:mastodon -R ."
	echo "sudo -u mastodon corepack enable --install-directory . yarn"
	echo "sudo -u mastodon RAILS_ENV=production NODE_OPTIONS=--openssl-legacy-provider PATH=./:$PATH bundle exec rails mastodon:setup"
	echo
	echo "Afterwards, check if your nginx is properly setup. Please orient yourself on this file: https://github.com/mastodon/mastodon/blob/main/dist/nginx.conf"
	echo "Other reverse proxies are _not_ supported by upstream. Use them on your own and expect them to not work or fail after updates."
	echo
	echo "And if you want working search, it is highly recommended to also install OpenSearch and set up that package before continuing here."
	echo
	echo "### Finally, enable and start all the required services ###"
	echo "systemctl enable --now mastodon.target"
}

post_upgrade() {
	echo "#####################################"
	echo "### Mastodon Upgrade Instructions ###"
	echo "#####################################"
	echo
	echo "### IMPORTANT: choose the correct procedure depending on your previous Mastodon version ###"
	echo
	echo "########################################################"
	echo "### Case 1: upgrade from Mastodon 4.6.0 to 4.6.*      ###"
	echo "### No database migration is required                 ###"
	echo "########################################################"
	echo
	echo "cd /var/lib/mastodon; find -path ./public/system -prune -o -exec chown -h mastodon:mastodon {} +"
	echo "sudo -u mastodon RAILS_ENV=production bundle exec rails assets:precompile"
	echo "systemctl daemon-reload; systemctl restart mastodon-web mastodon-sidekiq mastodon-streaming"
	echo
	echo
	echo "########################################################"
	echo "### Case 2: upgrade from Mastodon 4.5.x or lower      ###"
	echo "### Database migrations are required                  ###"
	echo "########################################################"
	echo
	echo "### IMPORTANT: before upgrading to Mastodon 4.6.x, make sure you have a PostgreSQL backup ###"
	echo "### Example, adapt the path if needed: ###"
	echo "sudo -u postgres pg_dump -Fc mastodon_production > /root/mastodon_production_before_upgrade.dump"
	echo
	echo "### Go into the Mastodon directory ###"
	echo "cd /var/lib/mastodon"
	echo
	echo "### Stop Mastodon before maintenance ###"
	echo "sudo systemctl stop mastodon-web mastodon-sidekiq mastodon-streaming"
	echo
	echo "### Check that the services are stopped. Expected result: inactive ###"
	echo "systemctl is-active mastodon-web mastodon-sidekiq mastodon-streaming || true"
	echo
	echo "### Fix ownership, but do not touch uploaded media in ./public/system ###"
	echo "sudo find -path ./public/system -prune -o -exec chown -h mastodon:mastodon {} +"
	echo
	echo "### Precompile the assets ###"
	echo "sudo -u mastodon RAILS_ENV=production bundle exec rails assets:precompile"
	echo
	echo "### Run the pre-deployment database migration before restarting services ###"
	echo "sudo -u mastodon SKIP_POST_DEPLOYMENT_MIGRATIONS=true RAILS_ENV=production bundle exec rails db:migrate"
	echo
	echo "### Reload systemd and start Mastodon services ###"
	echo "sudo systemctl daemon-reload"
	echo "sudo systemctl start mastodon-web mastodon-sidekiq mastodon-streaming"
	echo
	echo "### Check that the services are running. Expected result: active ###"
	echo "systemctl is-active mastodon-web mastodon-sidekiq mastodon-streaming"
	echo
	echo "### Run the post-deployment database migration after services are running ###"
	echo "sudo -u mastodon RAILS_ENV=production bundle exec rails db:migrate"
	echo
	echo "### Optional: check the full status if something looks wrong ###"
	echo "sudo systemctl status mastodon-web mastodon-sidekiq mastodon-streaming"
}
