.gitignore
include_rules

ifndef SCULPT_VERSION
error CONFIG_SCULPT_VERSION is undefined
endif

ifndef PUBLIC_USER
error CONFIG_PUBLIC_USER is undefined
endif

SHORT_SUM = b2sum -l24

: $(DEPOT_DIR)/<raw-archives> $(DEPOT_DIR)/<bin-archives> |> \
	for f in %<raw-archives> %<bin-archives>; do \
		awk -F '/' '{print "s|local/"$2"/"$3"/current|@(PUBLIC_USER)/"$2"/"$3"/"$4"|"}' $f >> %o; done; \
|> $(VERSIONS_SED_FILE)
	# Generate a listing of all raw archives

: $(DEPOT_DIR)/<raw-archives> |> \
	touch %o; \
	for f in %<raw-archives>; do cat $f >> %o; done; \
|> $(RAW_LISTING)
	# Generate a listing of all raw archives

: $(DEPOT_DIR)/<bin-archives> |> \
	for f in %<bin-archives>; do tail -n1 $f >> %o; done; \
|> $(BIN_LISTING)
	# Generate a listing of all binary archives

: $(DEPOT_DIR)/<packages> |> ^ generate package version sed script^ \
	for d in `dirname %<packages> | tr ' ' '\n' | sort -u`; do \
		echo $d/`cat $d/.ARCHIVES $d/runtime | $(SHORT_SUM)` | awk -F '/' \
			'{print "s|local/pkg/"$3"/current|@(PUBLIC_USER)/pkg/"$3"/"substr($5,0,6)"|"}' >> %o; \
	done \
|> $(PKG_SED_FILE)
	# Generate sed commands for replacing package versions

: $(DEPOT_DIR)/<raw-archives> $(DEPOT_DIR)/<bin-archives> $(PKG_SED_FILE) |> \
	^ generate public makefile^ \
	echo PUBLIC_USER=@(PUBLIC_USER) >> %o; \
	echo PUBLIC_ARCH=@(TUP_ARCH) >> %o; \
	echo "all: public/@(PUBLIC_USER)/index/@(SCULPT_VERSION).xz.sig" >> %o; \
	echo %%.xz.sig: %%.xz >> %o; \
	echo "	@echo sign \$<" >> %o; \
    echo "	@gpg --detach-sign --digest-algo SHA256 --no-tty --local-user @(PUBLIC_KEY) < \$< > tmp" >> %o; \
	echo "	@mv tmp \$@"  >> %o; \
	for f in %<raw-archives>; do \
		awk -F '/' -f errata/raw.mk.awk $f >> %o; done; \
	for f in %<bin-archives>; do \
		awk -F '/' -f errata/bin.mk.awk $f >> %o; done; \
	awk -F '/' -f errata/pkg.mk.awk $(PKG_SED_FILE) >> %o; \
|> Makefile
	# Generate the makefile for creating signed package tarballs

: index.xml | $(PKG_SED_FILE) |> ^ generate public index^ \
	xmllint %f | sed \
		-e 's|_/pkg/\([^"]*\)|local/pkg/\1/current|' \
		-f $(PKG_SED_FILE) \
		| xz > %o; \
|> public/@(PUBLIC_USER)/index/@(SCULPT_VERSION).xz

AWK_RUNTIME_PATH = awk -v RS=' ' -F '/' \
	'/runtime/ {print "	<pkg path=\"local/pkg/"$3"/current\"/>"}'

: $(DEPOT_DIR)/<packages> |> ^ generate local index^ \
	echo '<index>' > %o; \
	echo %<packages> | $(AWK_RUNTIME_PATH) | sort >> %o; \
	echo '</index>' >> %o |> $(DEPOT_DIR)/index/@(SCULPT_VERSION)
	# Generate the depot index file
