#
# makefile for misc functions
#

PROGS=true false cat sync stfu uname whoami nl csplit readlink
TCFLAGS=$(DEFS) -O2 -fomit-frame-pointer
CFLAGS=$(DEFS) -O2
LIBES=-lbasis
EXTRA=-I../../include -L../../lib/aout
LDFLAGS=-m i386linux -N
MAN1= csplit.1 nl.1 true.1 uname.1 whoami.1
MAN8= readlink.8 sync.8

all: $(PROGS)

clean:
	rm -f $(PROGS) *.o

sync: sync.c
	@echo "compiling sync"
	@$(CC) $(TCFLAGS) -o sync.o -c sync.c && ld $(LDFLAGS) -o sync sync.o
	@rm -f sync.o

stfu: stfu.c
	@echo "compiling stfu"
	@$(CC) $(TCFLAGS) -o stfu.o -c stfu.c && ld $(LDFLAGS) -o stfu stfu.o
	@rm -f stfu.o

true: true.c
	@echo "compiling true"
	@$(CC) $(TCFLAGS) -o true.o -DRC=0 -c true.c && ld $(LDFLAGS) -o true true.o
	@./true && echo "true is true"
	@rm -f true.o

false: true.c
	@echo "compiling false"
	@$(CC) $(TCFLAGS) -o false.o -DRC=1 -c true.c && ld $(LDFLAGS) -o false false.o
	@./false || echo "false is false"
	@rm -f false.o

readlink: readlink.c
	$(CC) $(CFLAGS) $(EXTRA) -o readlink readlink.c $(LIBES)

csplit: csplit.c
	$(CC) $(CFLAGS) $(EXTRA) -o csplit csplit.c $(LIBES)

nl: nl.c
	$(CC) $(CFLAGS) $(EXTRA) -o nl nl.c $(LIBES)

uname: uname.c
	$(CC) $(CFLAGS) $(EXTRA) -o uname uname.c $(LIBES)

whoami: whoami.c
	$(CC) $(CFLAGS) $(EXTRA) -o whoami whoami.c $(LIBES) && strip whoami

cat: cat.c
	$(CC) $(CFLAGS) -o cat cat.c && strip cat

install: $(PROGS)
# don't install this version of cat into Mastodon build trees;
# it is for recovery floppies.
	install -d $(DESTDIR)/bin
	install -d $(DESTDIR)/sbin
	install -d $(DESTDIR)/usr/bin
	install -o man -g man -d $(DESTDIR)/usr/man/man1
	install -o man -g man -d $(DESTDIR)/usr/man/man8
	install -c -o bin -g bin -m 511 -c -s uname true false sync $(DESTDIR)/bin
	install -c -o bin -g bin -m 511 -c -s stfu readlink $(DESTDIR)/sbin
	install -c -o bin -g bin -m 511 -c -s whoami nl csplit $(DESTDIR)/usr/bin
	ln -fs whoami $(DESTDIR)/usr/bin/id
	ln -fs whoami $(DESTDIR)/usr/bin/groups
	install -o man -g man -m 444 -c $(MAN1) $(DESTDIR)/usr/man/man1
	install -o man -g man -m 444 -c $(MAN8) $(DESTDIR)/usr/man/man8
	echo ".so man1/true.1" > /tmp/$$; \
	install -o man -g man -m 444 -c /tmp/$$ $(DESTDIR)/usr/man/man1/false.1
	echo ".so man1/whoami.1" > /tmp/$$; \
	install -o man -g man -m 444 -c /tmp/$$ $(DESTDIR)/usr/man/man1/id.1
	echo ".so man1/whoami.1" > /tmp/$$; \
	install -o man -g man -m 444 -c /tmp/$$ $(DESTDIR)/usr/man/man1/groups.1
	rm /tmp/$$
