# SUROOT      - If this is defined then the "su" utility is used to link
#               executables.  In some cases it will also cause the setuid
#               bit to be set.  When "su" is run it will ask for a password
#               if there is one.  SUROOT can most easily be defined by
#               creating an environment variable in $HOME/.profile.
#               Note that if you are linking as root and running as root then
#               there is no need to define this variable.

CC=cc -4 -mf -Wc,-s
LD=$(CC) -T1

CFLAGS=-Osax -w4 -g
CFLAGS+=-I../iolib
LDFLAGS+=-L../iolib -liomgr -g

all: pipe null echo iorecv volt writer reader writeread simple phsimple

pipe: main.o pipe.o
ifndef SUROOT
	$(LD) $(LDFLAGS) -o $@ $^
else
	su -c "$(LD) $(LDFLAGS) -o $@ $^"
	su -c "chmod u+s pipe"
endif

null: main.o null.o
ifndef SUROOT
	$(LD) $(LDFLAGS) -o $@ $^
else
	su -c "$(LD) $(LDFLAGS) -o $@ $^"
	su -c "chmod u+s null"
endif

echo: main.o echo.o
ifndef SUROOT
	$(LD) $(LDFLAGS) -o $@ $^
else
	su -c "$(LD) $(LDFLAGS) -o $@ $^"
	su -c "chmod u+s echo"
endif

iorecv: main.o iorecv.o
ifndef SUROOT
	$(LD) $(LDFLAGS) -o $@ $^
else
	su -c "$(LD) $(LDFLAGS) -o $@ $^"
	su -c "chmod u+s iorecv"
endif

volt: main.o volt.o
ifndef SUROOT
	$(LD) $(LDFLAGS) -o $@ $^
else
	su -c "$(LD) $(LDFLAGS) -o $@ $^"
	su -c "chmod u+s volt"
endif

main.o: main.c
	$(CC) $(CFLAGS) -c -DSIMPLE=0 -o $@ $<

simple: simple.o
ifndef SUROOT
	$(LD) $(LDFLAGS) -o $@ $^
else
	su -c "$(LD) $(LDFLAGS) -o $@ $^"
	su -c "chmod u+s simple"
endif

phsimple: phsimple.o
ifndef SUROOT
	$(LD) $(LDFLAGS) -lphoton_s -o $@ $^
else
	su -c "$(LD) $(LDFLAGS) -lphoton_s -o $@ $^"
	su -c "chmod u+s phsimple"
endif

writer: writer.o
ifndef SUROOT
	$(LD) $(LDFLAGS) -o $@ $^
else
	su -c "$(LD) $(LDFLAGS) -o $@ $^"
	su -c "chmod u+s writer"
endif

writer.o: writer.c
	$(CC) $(CFLAGS) -c -o $@ $^

reader: reader.o
ifndef SUROOT
	$(LD) $(LDFLAGS) -o $@ $^
else
	su -c "$(LD) $(LDFLAGS) -o $@ $^"
	su -c "chmod u+s reader"
endif

reader.o: reader.c
	$(CC) $(CFLAGS) -c -o $@ $^

writeread: writeread.o
ifndef SUROOT
	$(LD) $(LDFLAGS) -o $@ $^
else
	su -c "$(LD) $(LDFLAGS) -o $@ $^"
	su -c "chmod u+s writeread"
endif

writeread.o: writeread.c
	$(CC) $(CFLAGS) -c -o $@ $^

clean:
	-rm -f *.o *.err *.map null echo pipe iorecv volt writer reader writeread \
		simple phsimple
