# Maintainer: Antoine Martin (ayakael) <dev@ayakael.net>
# Contributor: Antoine Martin (ayakael) <dev@ayakael.net>

pkgname=mastodon
_pkgname=$pkgname
pkgver=4.2.10
_gittag=v$pkgver
pkgrel=1
pkgdesc="Self-hosted social media and network server based on ActivityPub and OStatus"
arch="x86_64"
url="https://github.com/mastodon/mastodon"
license="AGPL-3.0-only"
depends="
	$pkgname-assets=$pkgver-r$pkgrel
	ffmpeg
	file
	gcompat
	imagemagick
	nodejs
	npm
	protobuf
	py3-elasticsearch
	redis
	ruby
	ruby-bundler
	yarn
	"
makedepends="
	gnu-libiconv-dev
	icu-dev
	libffi-dev
	libidn-dev
	libxml2-dev
	libxslt-dev
	openssl-dev
	postgresql-dev
	protobuf-dev
	ruby-dev
	yaml-dev
	zlib-dev
	"
install="
	$pkgname.pre-install
	$pkgname.post-upgrade
	$pkgname.post-install
	"
source="
	mastodon-$_gittag.tar.gz::https://github.com/mastodon/mastodon/archive/$_gittag.tar.gz
	mastodon.initd
	mastodon.web.initd
	mastodon.sidekiq.initd
	mastodon.streaming.initd
	mastodon.logrotate
	bin-wrapper.in
	"
subpackages="$pkgname-openrc $pkgname-assets::noarch"
options="!check" # No test suite

_prefix="usr/lib/bundles/$_pkgname"

export BUNDLE_DEPLOYMENT=true
export BUNDLE_FORCE_RUBY_PLATFORM=true
export BUNDLE_FROZEN=true
export BUNDLE_JOBS=${JOBS:-2}

prepare() {
	default_prepare

	# Allow use of any bundler
	sed -i -e '/BUNDLED/,+1d' Gemfile.lock

	# Allow use of higher Node versions
	sed -i 's/"node": .*"/"node": ">=14.15"/' package.json

	mkdir -p "$srcdir"/gem-cache
}

build() {
	local bundle_without='exclude development'

	msg "Installing Ruby gems..."
	bundle config --local build.nokogiri --use-system-libraries \
		--with-xml2-include=/usr/include/libxml2 \
		--with-xslt-include=/usr/include/libxslt
	bundle config --local build.ffi --enable-system-libffi
	bundle config --local build.idn --enable-system-libidn
	bundle config --local path "vendor/bundle"
	bundle config --local set deployment 'false'
	bundle config --local set without "$bundle_without"

	bundle install --no-cache -j"$(getconf _NPROCESSORS_ONLN)"

	msg "Installing npm modules..."
	yarn install --production --frozen-lockfile

	(
		msg "Compiling assets..."
		export NODE_ENV=production
		export RAILS_ENV=production
		export NODE_OPTIONS="--openssl-legacy-provider"

		OTP_SECRET=precompile_placeholder SECRET_KEY_BASE=precompile_placeholder bundle exec rails assets:precompile
	)

	msg "Cleaning assets gems..."
	bundle config --local without "$bundle_without"
	bundle clean

	# Create executables in bin/*.
	# See also https://github.com/bundler/bundler/issues/6149.
	bundle binstubs --force bundler puma sidekiq
}

package() {
	local destdir="$pkgdir"/$_prefix
	local datadir="$pkgdir/var/lib/gitlab"
	# directory creation
	install -dm 755 \
		"$destdir" \
		"$datadir" \
		"$pkgdir"/etc/init.d


	# Install application files.
	rmdir "$destdir"
	cp -a "$builddir" "$destdir"

	install -m755 -t "$destdir"/bin/ \
		bin/bundle \
		bin/rails \
		bin/rake \
		bin/sidekiq \
		bin/sidekiqmon \
		bin/tootctl \
		bin/puma

	cd "$destdir"/vendor/bundle/ruby/*/

	# Remove tests, documentations and other useless files.
	find gems/ \( -name 'doc' \
	           -o -name 'spec' \
	           -o -name 'test' \) \
		-type d -maxdepth 2 -exec rm -fr "{}" +
	find gems/ \( -name 'README*' \
	           -o -name 'CHANGELOG*' \
	           -o -name 'CONTRIBUT*' \
	           -o -name '*LICENSE*' \
	           -o -name 'Rakefile' \
	           -o -name '.*' \) \
		-type f -delete

	# Remove assets, they are already compiled.
	rm -r gems/doorkeeper-*/app/assets
	rm -r gems/pghero-*/app/assets

	# Remove build logs and cache.
	rm -rf build_info/ cache/
	find extensions/ \( -name gem_make.out -o -name mkmf.log \) -delete

	cat > "$datadir"/.profile <<-EOF
		export RAILS_ENV=production
		export NODE_ENV=production
		export EXECJS_RUNTIME=Disabled
	EOF

	# Install wrapper scripts to /usr/bin.
	local name; for name in rake rails tootctl; do
		sed "s/__COMMAND__/$name/g" "$srcdir"/bin-wrapper.in \
			> "$builddir"/mastodon-$name
		install -m755 -D "$builddir"/mastodon-$name "$pkgdir"/usr/bin/mastodon-$name
	done

	# Put the config file in /etc and link to it
	touch "$pkgdir"/etc/mastodon.conf
	ln -s /etc/mastodon.conf "$destdir"/.env.production
	ln -s /usr/bin/node "$destdir"/node

	for file in $_pkgname $_pkgname.sidekiq $_pkgname.web $_pkgname.streaming; do
		install -m755 -D "$srcdir"/$file.initd "$pkgdir"/etc/init.d/$file
	done

	# Removing all prebuilt artifacts
	rm -R "$destdir"/node_modules/*/prebuilds 2>&1 || true

	install -m644 -D "$srcdir"/$_pkgname.logrotate \
		"$pkgdir"/etc/logrotate.d/$_pkgname
}

assets() {
	depends=""

	amove $_prefix/public/assets
}

sha512sums="
1fe5417136bc020a83b83eaccef7f1f46c13fc8318681f12ba556b1b6b03e25ef7b6335c28f4e6722101e97b63020cbd0d3fbacdaf9b3b5a4b73c3cf3e230813  mastodon-v4.2.10.tar.gz
d49fea9451c97ccefe5e35b68e4274aeb427f9d1e910b89c1f6c810489c3bec1ccff72952fdaef95abf944b8aff0da84a52347540d36ff1fba5ccc19e1d935c6  mastodon.initd
eefe12a31268245f802222c0001dac884e03adb0d301e53a1512a3cd204836ca03ad083908cd14d146cf0dce99e3a4366570efd0e40a9a490ccd381d4c63c32f  mastodon.web.initd
8fc9249c01693bb02b8d1a6177288d5d3549addde8c03eb35cc7a32dde669171872ebc2b5deb8019dc7a12970098f1af707171fa41129be31b04e1dc1651a777  mastodon.sidekiq.initd
03433a2f58600ca0d58e7c3713df2146ccdfc92033ccfe801dbd38bac39b66d6297f2b5ca02300caa36455b484eab2caa68c912c2f72150203bfa0e106c375fc  mastodon.streaming.initd
83b3bae5b6fdb4d0dbc1cbe546c62c0aa77397b97d1a5d5377af032466677de188065b556710c0d96576bbae89cc76800f1ffb8cd718155eb2784da818f27619  mastodon.logrotate
dfd0e43ac6c28387bd4aa57fd98ae41aeb5a098b6deb3e44b89f07818e2470773b025364afee7ef6fd0f664cb86bbbbe8796c9f222f5436c256a787282fbe3e1  bin-wrapper.in
"
