# Importada de Alpine aports por `hammer import-alpine` (Etapa G). PUNTO DE PARTIDA — pero
# YA trae los parches de musl de Alpine (lo que un import de nix pierde). Pendiente: el
# sha256 del tarball (el wrapper lo calcula), y adaptar build/install del shell de abuild.
name    = "forgejo"
version = "15.0.3"

[source]
tarball = "https://codeberg.org/forgejo/forgejo/archive/v15.0.3.tar.gz"
# FIXME sha256: el wrapper lo calcula (Alpine publica sha512). sha512 de Alpine:
# sha512 = "c1ebd90bc1c8ce9a0cbbf88451078691d7f272927a8208328f9ba26786200347ca0d9f25bf87a256d825c0c6a63a0a022082f21c2871c45ac1710394c9385f1b"
sha256  = "a73bb36caebf15925c00d46abce2adbd38b810a86610a0202a928c0a746e5742"

[build]
compiler = "zig-cc"
target   = "x86_64-linux-musl"
link     = "static"
flags    = []

[build.phases]
# de build() de Alpine (traducido; el lab provee $CBUILD/$CHOST — Etapa G Fase 3; revisá --shared para estático):
compile = '''
_abuild_phase() {
# XXX: LARGEFILE64
	export CGO_CFLAGS="$CFLAGS -O2 -D_LARGEFILE64_SOURCE"
	export TAGS="bindata sqlite sqlite_unlock_notify"
	export GITEA_VERSION="15.0.3"
	export EXTRA_GOFLAGS="$GOFLAGS"
	export CGO_LDFLAGS="$LDFLAGS"
	unset LDFLAGS
	## make FHS compliant
	local setting="forgejo.org/modules/setting"
	export LDFLAGS="$LDFLAGS -X $setting.CustomConf=/etc/forgejo/app.ini"
	export LDFLAGS="$LDFLAGS -X $setting.AppWorkPath=/var/lib/forgejo/"

	make -j1 build
}
_abuild_phase
'''
# de package() de Alpine (traducido $pkgdir→/out):
install = '''
_abuild_phase() {
for dir in forgejo forgejo/git forgejo/data forgejo/db forgejo/custom; do
		install -dm750 -o forgejo -g www-data \
			"/out"/var/lib/$dir
	done

	install -dm755 -o forgejo -g www-data "/out"/var/log/forgejo

	# TODO: rename when upstream does: https://codeberg.org/forgejo/forgejo/issues/5174
	install -Dm755 -g www-data gitea "/out"/usr/bin/forgejo

	install -Dm644 -o forgejo -g www-data "$srcdir"/forgejo.ini \
		"/out"/etc/forgejo/app.ini
	chown forgejo:www-data "/out"/etc/forgejo

	install -Dm755 "$srcdir"/forgejo.initd \
		"/out"/etc/init.d/forgejo
	install -Dm644 contrib/systemd/forgejo.service \
		-t "/out"/usr/lib/systemd/system/
}
_abuild_phase
'''

# depends de runtime de Alpine (NO build-deps): git, git-lfs, gnupg

[deps]
build = ["nodejs", "npm"]
