SUMMARY="A highly portable *NIX \"make\" command"
DESCRIPTION="smake is a \"make\" implementation developed and maintained since \
1985."
HOMEPAGE="https://s-make.sourceforge.io/"
COPYRIGHT="1985-2015 Joerg Schilling"
LICENSE="CDDL v1
	GNU GPL v2"
REVISION="2"
SOURCE_URI="https://downloads.sourceforge.net/s-make/smake-$portVersion.tar.bz2"
CHECKSUM_SHA256="27566aa731a400c791cd95361cc755288b44ff659fa879933d4ea35d052259d4"

ARCHITECTURES="all"

GLOBAL_WRITABLE_FILES="
	settings/smake/defaults.smk auto-merge
	"

PROVIDES="
	smake = $portVersion
	cmd:smake = $portVersion
	"
REQUIRES="
	haiku
	"

BUILD_REQUIRES="
	haiku_devel
	"
BUILD_PREREQUIRES="
	cmd:cmp
	cmd:gcc
	cmd:ld
	cmd:sed
	"

patchInsdir()
{
	# Usage: patchInsdir <oldDir> <newDir> <file> ...
	oldDir=$1
	newDir=$2
	shift 2

	sed -i "s,^\(INSDIR=\s*\)$oldDir,\1$newDir," "$@"
}

PATCH()
{
	# No need to build the profiled lib.
	sed -i -e "/^MK_FILES/ s/ libschily_p\.mk//" lib/Makefile

	# defaults.mks will be in system/settings/smake/ instead of system/lib/
	sed -i \
		-e "s|\"/usr/bert/lib\(/defaults\.smk\"\)|\"$sysconfDir/smake\1|" \
		-e "/searchfileinpath/ \
			s|\"lib\(/defaults\.smk\"\)|\"$relativeSettingsDir/smake\1|" \
		smake/make.c
	sed -i \
		-e "/-DDEFAULTS_PATH=/ s|/lib/|/$relativeSettingsDir/smake/|" \
		smake/Makefile
	patchInsdir lib $relativeSettingsDir/smake smake/Makefile.def

	patchInsdir include $relativeIncludeDir \
		conf/Makefile conf/*.mk inc/*.mk \
		include/schily/Makefile \
		include/schily/*.mk
}

BUILD()
{
	cd psmake
	./MAKE-all
	cd ..
	psmake/smake -r \
		INS_BASE="$prefix" INS_RBASE="$prefix" \
		LDPATH="" RUNPATH="" NO_SHL=no \
		DEFMANBASE=$relativeDocumentationDir
}

INSTALL()
{
	psmake/smake -r install DEFINSGRP=root \
		INS_BASE="$prefix" INS_RBASE="$prefix" \
		LDPATH="" RUNPATH="" NO_SHL=no \
		DEFMANBASE=$relativeDocumentationDir

	rm "$libDir"/libschily.a

	sed -i \
		-e "s|/opt/schily/lib/|`finddir B_SYSTEM_SETTINGS_DIRECTORY`/smake/|" \
		"$manDir"/man1/smake.1

	chmod u+w "$settingsDir"/smake/defaults.smk
}
