# SPDX-License-Identifier: GPL-2.0-or-later
# zsh -- the Z shell, MIT-like: https://www.zsh.org/
#
# Installed as /bin/zsh, *beside* mksh, which keeps /bin/sh.
#
# That is deliberate and it is not about taste. schinit's no-jobs fallback
# execs /bin/sh. If zsh were /bin/sh and zsh failed to start, the fallback
# would be the same broken binary and the machine would have no shell at all.
# Two different programs means the recovery path is a different program.
# It is also what real systems do: a small POSIX /bin/sh for scripts, a large
# interactive shell for people. docs/history/kernel.md section 2 says "exactly
# one name per role" -- these are two roles.
#
# ---------------------------------------------------------------------------
# Licensing
#
# zsh's LICENCE is MIT-like, with one condition that has to be honoured rather
# than noted: a handful of shell functions are GPL, and the licence says so
# explicitly, adding that "none of the core functions are affected by this, so
# those files may simply be omitted."
#
# So they are omitted. GPL_FILES below is that list -- four files, deleted
# after extraction and verified absent before install. Every one completes a
# command SchOS does not have (darcs, qdbus, osc, zypper), which is why this
# costs nothing at all.

include ../mk/port.mk

ZSH_VERSION := 5.9
ZSH_URL     := https://sourceforge.net/projects/zsh/files/zsh/$(ZSH_VERSION)/zsh-$(ZSH_VERSION).tar.xz/download
ZSH_SHA256  := 9b8d1ecedd5b5e81fbf1918e876752a7dd948e05c1a0dba10ab863842d45acd5

TARBALL := $(DISTDIR)/zsh-$(ZSH_VERSION).tar.xz

# The GPL-licensed functions. Removed from the source tree, not merely skipped
# at install: a file that is not there cannot be shipped by a later edit to
# this Makefile.
#
# This list is four files, and it was audited rather than grepped. The obvious
# search -- `grep -rl GPL` -- returns three more, and all three are wrong:
#
#   _composer                 completes SPDX licence identifiers, so the word
#                             "licence" is data, not a licence
#   _luarocks                 has '--license=... (e.g. "MIT/X11" or "GNU GPL
#                             v3")' as a completion string, same thing
#   VCS_INFO_get_data_hg      says "Distributed under the same BSD-ish license
#                             as zsh itself" -- it matched on HGPLAIN, a
#                             Mercurial environment variable with GPL in the
#                             middle of it
#
# Deleting those three would cost nothing and would still be a false claim in
# a licensing file, which is the kind of thing that is believed later.
GPL_FILES := \
  Completion/Unix/Command/_darcs \
  Completion/Linux/Command/_qdbus \
  Completion/openSUSE/Command/_osc \
  Completion/openSUSE/Command/_zypper

# Completion sets for systems that are not this one. Dropping them is size,
# not licensing -- together they are most of Completion/ and none of them can
# ever match a command that exists here.
FOREIGN_COMP := Completion/openSUSE Completion/Mandriva Completion/Redhat \
                Completion/Debian Completion/Solaris Completion/Cygwin \
                Completion/AIX Completion/BSD Completion/Darwin

HOSTCC_ZSH ?= gcc
GCC_INC    := $(shell $(HOSTCC_ZSH) -print-file-name=include)
LIBGCC     := $(shell $(HOSTCC_ZSH) -print-libgcc-file-name)

# Split the way autoconf consumes them -- see ports/ncurses/Makefile, which
# had to learn the same lesson: crt objects ride in LDFLAGS (before the object
# being linked), libraries in LIBS (after it).
ZSH_CPPFLAGS := -nostdinc -isystem $(MUSL)/include -isystem $(GCC_INC) \
                -isystem $(NCURSES)/include -D__SCHOS__
# zsh 5.9 is from 2022; GCC 14 and 15 promoted three long-standing warnings to
# errors by default, and old autoconf projects fail their own feature probes on
# them. The probe that matters here is
#
#     char **test = boolcodes; puts(*test);
#
# which ncurses answers with `const char * const boolcodes[]`. GCC 15 rejects
# the conversion outright, so the test "fails", HAVE_BOOLCODES goes undefined,
# and zsh helpfully declares its own `static char *boolcodes[]` -- which then
# collides with the declaration in term.h that was there the whole time. The
# error surfaces in termcap.c, three steps from the cause.
#
# These restore the diagnostic level the code was written against. They are
# scoped to this port and nothing else in the tree relaxes them.
ZSH_COMPAT := -Wno-incompatible-pointer-types -Wno-implicit-function-declaration \
              -Wno-int-conversion

ZSH_CFLAGS   := -O2 $(CFLAGS_BT) $(ZSH_COMPAT)
ZSH_LDFLAGS  := -static -nostdlib -L$(MUSL)/lib -L$(NCURSES)/lib \
                $(MUSL)/lib/crt1.o $(MUSL)/lib/crti.o
ZSH_LIBS     := -lncursesw $(MUSL)/lib/libc.a $(LIBGCC) $(MUSL)/lib/libc.a \
                $(MUSL)/lib/crtn.o

# Answers configure cannot work out without running a program on the target.
# Each one is a fact about SchOS, so each one is written down here rather than
# guessed by autoconf's cross-compilation defaults.
#
# **Read each name as the question configure asks, not as a property.** These
# are `zsh_cv_*` cache entries for AC_CACHE_CHECK, and the value answers the
# *check's* wording. `zsh_cv_sys_killesrch` is "does kill(pid, 0) return ESRCH
# correctly", so `yes` is the healthy answer and `no` means broken.
#
# It was `no` here, and that single word cost every `$(command substitution)`
# in the shell. `no` makes configure emit BROKEN_KILL_ESRCH, and zsh_system.h
# answers that with `#undef ESRCH / #define ESRCH EINVAL`. zsh's waitforpid()
# ends its wait on `errno != ESRCH`; with ESRCH redefined to EINVAL (22) and
# the kernel correctly returning ESRCH (3), that test was true for ever and
# the shell waited on a child it had already reaped. Every `X=$(...)` hung.
#
# The cruel part: autoconf's own cross-compilation default for this one is
# `yes`. Leaving the line out entirely would have been correct.
#
# Two more in this block claimed capabilities SchOS does not have, found by
# checking the rest after the one above. Both were `yes`, and both are facts
# that can simply be measured on the running system:
#
#   zsh_cv_sys_path_dev_fd   /dev/fd and /proc/self/fd do not exist. Telling
#                            zsh otherwise made `<(cmd)` fail at the point of
#                            use with `cat: /dev/fd/11: No such file or
#                            directory`, which reads as a broken command
#                            rather than an unimplemented feature.
#
#   zsh_cv_sys_fifo          mkfifo() "succeeds" and produces a *character
#                            device*: the kernel's mknod turns S_IFIFO into a
#                            T_DEV, so `ls -l` shows `crw-rw-rw- 438, 0`, and
#                            nothing written to it comes back out. Named pipes
#                            are not implemented.
#
# With both `no`, zsh knows it cannot do process substitution and says so,
# which is the honest state until FIFOs exist.
ZSH_CACHE := \
  zsh_cv_shared_environ=yes \
  zsh_cv_shared_tgetent=no \
  zsh_cv_shared_tigetstr=no \
  zsh_cv_sys_nis=no \
  zsh_cv_sys_nss=no \
  zsh_cv_func_realpath_accepts_null=yes \
  zsh_cv_sys_path_dev_fd=no \
  zsh_cv_sys_fifo=no \
  zsh_cv_sys_killesrch=yes \
  zsh_cv_sys_signal_handler=void \
  zsh_cv_prog_tcsetpgrp=yes \
  ac_cv_func_mmap_fixed_mapped=yes \
  ac_cv_func_setpgrp_void=yes

# --disable-dynamic: SchOS links everything static, and zsh's module system
#   would otherwise want dlopen. Every module is compiled in instead.
# --with-term-lib: name it, so configure cannot quietly decide it needs none
#   and leave the line editor unable to move a cursor.
# --enable-etcdir: where zsh looks for zshenv/zshrc. NOT --sysconfdir, which
#   autoconf accepts, zsh ignores, and configure reports no complaint about --
#   the shell simply starts with its default prompt and no completion, looking
#   like a broken config file rather than a config file that was never read.
ZSH_CONFIGURE := \
  --host=x86_64-linux-musl \
  --build=x86_64-pc-linux-gnu \
  --prefix=/usr \
  --bindir=/bin \
  --enable-etcdir=/etc/zsh \
  --disable-dynamic \
  --enable-multibyte \
  --with-term-lib="ncursesw" \
  --disable-gdbm \
  --disable-pcre \
  --disable-cap \
  --disable-largefile \
  --enable-zsh-secure-free \
  --enable-fndir=/usr/share/zsh/$(ZSH_VERSION)/functions \
  --enable-scriptdir=/usr/share/zsh/$(ZSH_VERSION)/scripts

$(TARBALL): | $(DISTDIR)
	curl -fsSL -o $@ $(ZSH_URL)
	sh $(SHA256CHECK) "$(ZSH_SHA256)" "$@"

download: $(TARBALL)

# rm -rf before the move, and touch after it. Both matter:
#
#   Without the rm, `mv zsh-5.9 src` when src already exists moves the tree
#   *inside* it, producing src/zsh-5.9 -- and zsh's configure globs */*/*.mdd,
#   so it finds the nested copy and writes modfile=zsh-5.9/Src/zsh.mdd into
#   config.modules. The build then fails a long way from the cause.
#
#   Without the touch, this rule re-runs whenever the directory's mtime is
#   older than the tarball's, which is how src came to exist in the first
#   place. Editing files *inside* a directory does not update the directory.
$(SRCDIR): $(TARBALL)
	mkdir -p $(PORTDIR)
	rm -rf $@ $(PORTDIR)/zsh-$(ZSH_VERSION)
	tar -xJf $< -C $(PORTDIR)
	mv $(PORTDIR)/zsh-$(ZSH_VERSION) $@
	@echo "zsh: removing GPL-licensed functions named by zsh's own LICENCE"
	@cd $@ && rm -f $(GPL_FILES)
	@cd $@ && rm -rf $(FOREIGN_COMP)
	touch $@

extract: $(SRCDIR)

$(BUILDDIR)/config.status: | $(SRCDIR)
	@test -f $(NCURSES)/lib/libncursesw.a || { \
	  echo "zsh: build ports/ncurses first -- no libncursesw.a" >&2; exit 1; }
	mkdir -p $(BUILDDIR)
	cd $(BUILDDIR) && \
	  CC="$(HOSTCC_ZSH)" \
	  CPPFLAGS="$(ZSH_CPPFLAGS)" \
	  CFLAGS="$(ZSH_CFLAGS)" \
	  LDFLAGS="$(ZSH_LDFLAGS)" \
	  LIBS="$(ZSH_LIBS)" \
	  $(ZSH_CACHE) \
	  $(SRCDIR)/configure $(ZSH_CONFIGURE)

$(BUILDDIR)/Src/zsh: $(BUILDDIR)/config.status
	$(MAKE) -C $(BUILDDIR)

build: $(BUILDDIR)/Src/zsh

# Refuse to install if a GPL file is somehow present. The rm above should have
# made this impossible; the check is here because "should have" is not a
# licence audit, and this runs before every image is built.
.PHONY: licence-check
licence-check: | $(SRCDIR)
	@bad=0; for f in $(GPL_FILES); do \
	  if [ -e "$(SRCDIR)/$$f" ]; then echo "zsh: GPL file present: $$f" >&2; bad=1; fi; \
	done; \
	if [ $$bad -ne 0 ]; then echo "zsh: refusing to install" >&2; exit 1; fi; \
	echo "zsh: no GPL-licensed files in the tree"

# licence-check runs *inside* the rule, not as a prerequisite.
#
# It is .PHONY, and a phony prerequisite is always out of date -- so having it
# on the left of the colon re-ran the whole install on every `make`, which is
# slow, and which eventually failed outright when zsh's installfns.sh tripped
# over the installfnsdir its previous run had left behind.
$(SYSROOT)/bin/zsh: $(BUILDDIR)/Src/zsh | $(SYSROOT)/bin
	$(MAKE) licence-check
	rm -rf $(BUILDDIR)/installfnsdir
	$(MAKE) -C $(BUILDDIR) install DESTDIR=$(SYSROOT)
	rm -rf $(SYSROOT)/usr/share/man $(SYSROOT)/usr/share/zsh/$(ZSH_VERSION)/help
	chmod 755 $@

install: $(SYSROOT)/bin/zsh

clean:
	rm -rf $(PORTDIR)

distclean: clean
	rm -f $(TARBALL)
