note *
note * This build script is in an extended Make Abstract Machine (MAM)
note * language. Documentation is at: src/cmd/INIT/README-mamake.md
note *

setv MAMAKE_STRICT 3

setv INSTALLROOT ../../..
setv PACKAGE_ast_INCLUDE ${INSTALLROOT}/include/ast
setv CC cc
setv mam_cc_FLAGS ${mam_cc_TARGET} ${mam_cc_DLL} ${-debug-symbols?1?${mam_cc_DEBUG} -D_BLD_DEBUG?${mam_cc_OPTIMIZE}?}
setv CCFLAGS
setv CCLDFLAGS ${-strip-symbols?1?${mam_cc_LD_STRIP}??}
setv IFFEFLAGS
setv LDFLAGS

make .INIT dontcare virtual
	prev ${PACKAGE_ast_INCLUDE}/cmdlist.h
	exec - sed -e '/^CMDLIST(.*)$/!d' -e 's/CMDLIST(\(.*\))/\1/' -e '/^getconf$/d' \
	exec -		-e '/^ln$/d' -e '/^mv$/d' -e '/^md5sum$/d' -e '/^sum$/d' ${<}
	bind -lcmd
	bind -lutil dontcare
done .INIT

make install virtual
	note *
	note * AST header dependencies
	note * TODO: verify and centralise
	note *

	prev ${PACKAGE_ast_INCLUDE}/ast_time.h implicit
	make ${PACKAGE_ast_INCLUDE}/vmalloc.h implicit
		make ${PACKAGE_ast_INCLUDE}/ast_common.h implicit
			prev ${PACKAGE_ast_INCLUDE}/ast_map.h implicit
			make ${PACKAGE_ast_INCLUDE}/endian.h implicit
				make ${PACKAGE_ast_INCLUDE}/bytesex.h implicit
					note * TODO * circular dependency
					note # prev ${PACKAGE_ast_INCLUDE}/ast_common.h
				done
			done
		done
		make ${PACKAGE_ast_INCLUDE}/ast_std.h implicit
			make ${PACKAGE_ast_INCLUDE}/regex.h implicit
				prev ${PACKAGE_ast_INCLUDE}/ast_api.h implicit
				make ${PACKAGE_ast_INCLUDE}/ast_wchar.h implicit
					make ${PACKAGE_ast_INCLUDE}/wctype.h implicit
						make ${PACKAGE_ast_INCLUDE}/ast_wctype.h implicit
							prev ${PACKAGE_ast_INCLUDE}/endian.h
							make ${PACKAGE_ast_INCLUDE}/wchar.h implicit
								note * TODO * circular dependency
								note # prev ${PACKAGE_ast_INCLUDE}/ast_wchar.h
							done
						done
					done
					make ${PACKAGE_ast_INCLUDE}/stdio.h implicit
						make ${PACKAGE_ast_INCLUDE}/ast_stdio.h implicit
							prev ${PACKAGE_ast_INCLUDE}/sfio_s.h implicit
							note * TODO * circular dependency
							note # prev ${PACKAGE_ast_INCLUDE}/ast_std.h
						done
					done
					prev ${PACKAGE_ast_INCLUDE}/ast_common.h
				done
				prev ${PACKAGE_ast_INCLUDE}/ast_common.h
			done
			make ${PACKAGE_ast_INCLUDE}/getopt.h implicit
				prev ${PACKAGE_ast_INCLUDE}/ast_getopt.h implicit
			done
			prev ${PACKAGE_ast_INCLUDE}/ast_map.h
			prev ${PACKAGE_ast_INCLUDE}/ast_botch.h implicit
			prev ${PACKAGE_ast_INCLUDE}/ast_limits.h implicit
			make ${PACKAGE_ast_INCLUDE}/ast_fcntl.h implicit
				prev ${PACKAGE_ast_INCLUDE}/ast_fs.h implicit
			done
			prev ${PACKAGE_ast_INCLUDE}/ast_getopt.h
			make ${PACKAGE_ast_INCLUDE}/ast_sys.h implicit
				prev ${PACKAGE_ast_INCLUDE}/getopt.h
				prev ${PACKAGE_ast_INCLUDE}/endian.h
			done
			prev ${PACKAGE_ast_INCLUDE}/ast_lib.h implicit
			prev ${PACKAGE_ast_INCLUDE}/ast_common.h
		done
	done
	make ${PACKAGE_ast_INCLUDE}/proc.h implicit
		make ${PACKAGE_ast_INCLUDE}/ast.h implicit
			prev ${PACKAGE_ast_INCLUDE}/ast_api.h
			prev ${PACKAGE_ast_INCLUDE}/vmalloc.h
			make ${PACKAGE_ast_INCLUDE}/sfio.h implicit
				prev ${PACKAGE_ast_INCLUDE}/sfio_s.h
				prev ${PACKAGE_ast_INCLUDE}/ast_common.h
				prev ${PACKAGE_ast_INCLUDE}/ast_std.h
			done
			prev ${PACKAGE_ast_INCLUDE}/ast_std.h
		done
	done
	make ${PACKAGE_ast_INCLUDE}/error.h implicit
		make ${PACKAGE_ast_INCLUDE}/option.h implicit
			prev ${PACKAGE_ast_INCLUDE}/ast.h
		done
		prev ${PACKAGE_ast_INCLUDE}/ast.h
	done
	make ${PACKAGE_ast_INCLUDE}/cmd.h implicit
		make ${PACKAGE_ast_INCLUDE}/cmdext.h implicit
			prev ${PACKAGE_ast_INCLUDE}/shcmd.h implicit
		done
		prev ${PACKAGE_ast_INCLUDE}/shcmd.h
		make ${PACKAGE_ast_INCLUDE}/stk.h implicit
			prev ${PACKAGE_ast_INCLUDE}/sfio.h
		done
		prev ${PACKAGE_ast_INCLUDE}/error.h
		prev ${PACKAGE_ast_INCLUDE}/ast.h
	done

	note *
	note * Build pty
	note *

	make pty
		make pty.o
			make pty.c
				make FEATURE/pty implicit
					prev features/pty
					exec - iffe ${IFFEFLAGS} -v -c "${CC} ${mam_cc_FLAGS} ${CCFLAGS} ${LDFLAGS}" \
					exec -	ref ${mam_cc_L+-L${INSTALLROOT}/lib} -I${PACKAGE_ast_INCLUDE} \
					exec -	-I${INSTALLROOT}/include ${mam_libast} ${mam_libcmd} : run ${<}
				done
				prev ${PACKAGE_ast_INCLUDE}/ast_time.h
				prev ${PACKAGE_ast_INCLUDE}/vmalloc.h
				prev ${PACKAGE_ast_INCLUDE}/regex.h
				prev ${PACKAGE_ast_INCLUDE}/proc.h
				prev ${PACKAGE_ast_INCLUDE}/error.h
				prev ${PACKAGE_ast_INCLUDE}/cmd.h
			done pty.c
			exec - ${CC} ${mam_cc_FLAGS} ${CCFLAGS} -I. -I${PACKAGE_ast_INCLUDE} \
			exec -	-DERROR_CATALOG=\""builtin"\" -DCMD_STANDALONE=b_pty -c ${<}
		done pty.o
		exec - ${CC} ${CCLDFLAGS} ${mam_cc_FLAGS} ${CCFLAGS} ${mam_cc_NOSTRICTALIASING} ${LDFLAGS} ${mam_cc_L+-L.} \
		exec -	${mam_cc_L+-L${INSTALLROOT}/lib} -o pty pty.o ${mam_libutil} ${mam_libast} ${mam_libcmd}
	done pty

	note *
	note * Dynamically linked version, if supported
	note *

	make ${INSTALLROOT}/dyn/bin/pty dontcare
		prev pty
		prev ${INSTALLROOT}/lib/lib/cmd
		exec - export LDFLAGS='${LDFLAGS} ${CCLDFLAGS} ${mam_cc_LD_NOASNEEDED}'
		exec - dylink -e pty ${mam_libutil} $(cat ${INSTALLROOT}/lib/lib/cmd) pty.o
	done

	note *
	note * Pre-install
	note *

	make ${INSTALLROOT}/bin
		exec - mkdir -p ${@}
	done
	make ${INSTALLROOT}/bin/pty
		prev pty
		note * macOS throws code signature error if 'cp' overwrites Mach-O binary; must remove first
		exec - rm -f ${@} && cp ${<} ${@}
	done
done install

make test dontcare virtual
done test
