#
# Term program makefile. for HP-UX 8.07  -ot
# valids targets are:
#	all term linecheck test tags clean install realclean ${PROGS}
PROG=term
SHELL=/bin/sh
CC=gcc -Wall
# Hpux. c89 instead of cc
# AIX: replace CC with this line...
# CC=xlc -D_ALL_SOURCE
# if use a NeXT machine, then replace CC with this line...
# CC=cc -DNeXT
CPP=$(CC) -E
# if useing a NeXT then use..
# CPP=cc -E -DNeXT
DEBUGFLAGS= $(DEBUG) -O

# action
DO=all

# -DLOGIN_SHELL causes trsh to start a login shell by default
CFLAGS = $(DEBUGFLAGS) -DLOGIN_SHELL -DSHAREDIR='"$(SHAREDIR)"' \
	$(USERFLAGS) $(OSFLAGS)

LINKFLAGS=-O $(OSLINK)
# Hpux. -Aa for lib.c -s for stripping
  
RM=rm -f
LN=ln 
# If the following fails to work, the change it to be
# ar rc 
# and uncomment the ranlib below.
# on a sun you can use /usr/5bin/ar.
AR=ar rcs
RANLIB=echo # ranlib
INSTALL= install
USERINSTALL = cp
OWNER= bin
GROUP = term
INSTPGMFLAGS = -s -o $(OWNER) -g $(GROUP) -m 0755
INSTPGMSGIDFLAGS = -s -o $(OWNER) -g $(GROUP) -m 0755
MKDIR= mkdir
CHGRP= chgrp 
CHMOD= chmod
SHAREDIR=/usr/lib/term
BINDIR=/usr/local/bin
USERBINDIR=$$HOME/bin
MANDIR=/usr/local/man/man1
USERMANDIR=$$HOME/man/man1
#
# Change this if yours is different.
#
CURSES= -ltermcap

TERMOBJS=main.o serial.o misc.o link.o pty.o compress.o \
		checksum.o meta.o statistics.o sevenbit.o \
		spipe.o libtermnet.a
CLIENTOBJS=lib.o client.o termnet.o terminal.o select.o socket.o connect.o spipe.o

TRSHELLOBJS=trshell.o libtermnet.a
UPLOADOBJS=upload.o libtermnet.a
RTELNETOBJS=rtelnet.o libtermnet.a
REDIROBJS=redir.o libtermnet.a
XCONNOBJS=xconn.o libtermnet.a
STATOBJS=tmon.o libtermnet.a
SHUTDOWNOBJS=shutdown.o libtermnet.a

PROGS=term trsh tupload txconn tredir tmon tshutdown 

MANPAGES=term.1 term_clients.1 term_setup.1 tupload.1 tredir.1 \
	trsh.1 tmon.1

list:
	@echo "Look for you operating system, and type one of the"
	@echo "following to build term for that system."
	@echo
	@echo "netbsd linux aix sun sgi mips next hpux svr4"
	@echo "osf1 sol21 hcx sco titan bsdi "
	@echo
	@echo "To install type 'make DO=install <ostype>'"

MAKEDO = if [ "X$(USERSHARE)" = X ] ; then \
	    FLAGS="$(DO) GROUP=$(GROUP) BINDIR=$(BINDIR) SHAREDIR=$(SHAREDIR) \
		OWNER=$(OWNER) MANDIR=$(MANDIR)"; \
	else \
		FLAGS="$(DO) BINDIR=$(USERBINDIR) SHAREDIR=$(USERSHARE) \
		MANDIR=$(USERMANDIR)  USERSHARE=$(USERSHARE)  \
		USERFLAGS=-DUSERSHARE"; \
	fi; \
	$(MAKE) $$FLAGS 

Linux: linux
linux:
	$(MAKEDO)

bsdi:
	$(MAKEDO) AR="ar rc" RANLIB=ranlib \
		CPP="cc -E" \
		CC=cc \
		OSFLAGS="-funsigned-char -DBSDI" 


TitanOS: titan
titan:
	$(MAKEDO) CC="cc -43 -inline" RANLIB=ranlib

aix:
	$(MAKEDO) OSFLAGS="-D_BSD" OSLINK="-lbsd"

netbsd:
	$(MAKEDO) AR="ar rc" RANLIB=ranlib

sun:
	$(MAKEDO) AR="ar rc" RANLIB=ranlib 

sgi osf1:
	$(MAKEDO) 

mips:
	$(MAKEDO) AR="ar rc" RANLIB=ranlib 

# The NeXTSTEP 3.0 cpp defines NeXT, so we don't have to.
# But, just in case, you can set NeXTFLAG:
# add 'OSFLAGS=-DNeXT' to the $MAKE line below..

next:
	$(MAKEDO) AR="ar rc" RANLIB=ranlib \
		CPP="cc -E" \
		CC=cc \
		OSFLAGS="-funsigned-char" 

# you can use gcc on hp instead of c89
hpux:
	$(MAKEDO) CC="c89" \
		OSLINK=" -Aa -s" \
		RANLIB=echo \
		INSTALL=cp \
		INSTPGMFLAGS="" \
		OSFLAGS="-D_HPUX_SOURCE" 

svr4:
	$(MAKEDO) OSFLAGS="-funsigned-char -m486 -DSVR4" \
		LIBS="-lsocket -lnsl" \
		INSTALL="/usr/ucb/install -g bin -o bin" 

sco:
	$(MAKEDO) "OSFLAGS=-DSCO -DSVR3 -DNO_UNIX_DOMAIN" \
		LIBS="-lsocket -lpt" \
		INSTALL="/usr/local/contrib/bin/bsdinstall -g bin -o bin" \
		"CC=cc" \
		"DEBUGFLAGS=-g" 

sol21:
	$(MAKEDO) OSFLAGS="-DSVR4" \
		LIBS="-lsocket -lnsl" \
		INSTALL="/usr/ucb/install -g bin -o bin" 

# The HCX/UX 5.1 cc does not understand ANSI C.  To convert the term source
# to K&R on the fly, "unproto" must be compiled and the generated "cpp" 
# binary placed in some directory, specified below with -B.  This lets the 
# hcx cc be passed de-ANSIfied C code.  "unproto" can be found in archives 
# for comp.sources.misc, volume 27, issue 85.
#
hcx:
	universe ucb; $(MAKEDO) CC="cc -B/path/to/unproto_dir/ -tp" \
		CPP=/path/to/unproto_dir/cpp \
		INSTALL=cp 

all: $(PROGS)

  
install: all
	if [ ! -d $(DESTDIR)$(BINDIR) ]; then $(MKDIR) $(DESTDIR)$(BINDIR); \
	else true; fi
	for i in $(PROGS);\
	do \
	$(INSTALL)  $(INSTPGMFLAGS)  $$i $(DESTDIR)$(BINDIR);\
	done

+ installshare: all
	if [ ! -d $(DESTDIR)$(BINDIR) ]; then \
	  $(MKDIR) $(DESTDIR)$(BINDIR); \
	else true; fi
	for i in $(PROGS); do \
	  if [ "X$(USERSHARE)" = X ] ; then \
	    $(INSTALL) $(INSTPGMSGIDFLAGS)  $$i $(DESTDIR)$(BINDIR);\
	    $(CHMOD) g+s $(DESTDIR)$(BINDIR)/$$i; \
	  else \
	    $(USERINSTALL) $$i $(DESTDIR)$(BINDIR); \
	    $(CHMOD) 0755 $(DESTDIR)$(BINDIR)/$$i; \
	    $(CHMOD) u+s $(DESTDIR)$(BINDIR)/$$i; \
	  fi; \
	done
	if [ ! -d $(DESTDIR)$(SHAREDIR) ]; then \
	  $(MKDIR) $(DESTDIR)$(SHAREDIR); \
	  if [ "X$(USERSHARE)" = X ] ; then \
	    $(CHGRP) $(GROUP) $(DESTDIR)$(SHAREDIR); \
	  fi; \
	  $(CHMOD) 0775 $(DESTDIR)$(SHAREDIR); \
	else true; fi

installman:
	if [ ! -d $(DESTDIR)$(MANDIR) ]; then $(MKDIR) $(DESTDIR)$(MANDIR); \
	else true; fi
	for i in $(MANPAGES);\
	do \
	cp $$i $(DESTDIR)$(MANDIR);\
	done

#-----------------------------------------------------------------
# if your make complains about not know how to make .o files, you
# may need to uncomment following two lines.. (for gnu make).
#%.o: %.c
#	$(CC) $(CFLAGS) -c $< -o $@
#     --- or following 5 lines ------
#.SUFFIXES
#.SUFFIXES:  .o .c
#
#.c.o:
#	$(CC) $(CFLAGS) -c $< -o $@
#-----------------------------------------------------------------
# shouldn't need to mod anything below here..

#----------------------------------------------------------------- 
libtermnet.a: $(CLIENTOBJS)
	$(RM) libtermnet.a
	$(AR) libtermnet.a $(CLIENTOBJS)
	$(RANLIB) libtermnet.a

term: $(TERMOBJS) 
	$(CC) $(LINKFLAGS) -o term $(TERMOBJS) $(LIBS)

trsh: $(TRSHELLOBJS)
	$(CC) $(LINKFLAGS) -o trsh $(TRSHELLOBJS) $(LIBS)

test: test.o spipe.o
	$(CC) $(LINKFLAGS) -o test test.o spipe.o

tupload: $(UPLOADOBJS) 
	$(CC) $(LINKFLAGS) -o tupload $(UPLOADOBJS) $(LIBS)

tredir: $(REDIROBJS)
	$(CC) $(LINKFLAGS) -o tredir $(REDIROBJS) $(LIBS)

txconn: $(XCONNOBJS)
	$(CC) $(LINKFLAGS) -o txconn $(XCONNOBJS) $(LIBS)

tshutdown: $(SHUTDOWNOBJS)
	$(CC) $(LINKFLAGS) -o tshutdown $(SHUTDOWNOBJS) $(LIBS)

tmon: $(STATOBJS)
	$(CC) $(LINKFLAGS) -o tmon $(STATOBJS) $(CURSES) $(LIBS)

linecheck: linecheck.c terminal.o
	$(CC) $(CFLAGS) -o linecheck linecheck.c terminal.o

tags:
	etags *.h *.c

myclean:
	$(RM) *~

clean:
	$(RM) *.o *.a core compress.debug out *~

realclean: clean
	$(RM) $(PROGS) test 1 linerem check trcmd linecheck TAGS


depend dep:
	sed '/\#\#\# Dependencies/q' < Makefile > tmp_make
	for i in *.c ;do $(CPP) -MM $$i;done >> tmp_make
	mv tmp_make Makefile

version: myclean diff
	(cd ..;tar cf term.tar term/*.c term/*.h term/F* term/M* term/P* \
	term/O* term/I* term/R* term/C* term/T* term/S* term/*.1 )
	(cd ..;compress term.tar)

diff:
	(cd ..;mv term term.new;tar xfz term.tar.Z;mv term term.old; \
		mv term.new term;diff -c +new-file term.old term > term.diff; \
		rm -rf term.old)

#
# The following targets are mostly for my personal use. I use them to 
# update the remote version of term when I am developing.
myver: myclean mydiff
	(cd ..;tar cf t.tar term/*.c term/*.h term/M* term/P* term/O* \
	 term/I* term/R* term/C* term/T* term/*.1 )
	(cd ..;compress t.tar)

mydiff:
	(cd ..;mv term term.new; tar xfz t.tar.Z;mv term term.old; \
		mv term.new term;diff -c +new-file term.old term > t.diff; \
		rm -rf term.old)

update: mydiff
	(export TERMDIR=/root; \
		cd ..;trsh rm term.diff;tupload term.diff; \
		trsh termpatch)

dummy:

spipe.o: spipe.c
checksum.o : checksum.c 
client.o : client.c includes.h term.h config.h client.h terminal.h 
compress.o : compress.c includes.h term.h config.h client.h terminal.h debug.h 
connect.o : connect.c includes.h term.h config.h client.h terminal.h 
lib.o : lib.c includes.h term.h config.h client.h terminal.h debug.h 
linecheck.o : linecheck.c terminal.h 
link.o : link.c includes.h term.h config.h client.h terminal.h debug.h 
main.o : main.c includes.h term.h config.h client.h terminal.h debug.h 
meta.o : meta.c includes.h term.h config.h client.h terminal.h debug.h 
misc.o : misc.c includes.h term.h config.h client.h terminal.h debug.h 
pty.o : pty.c includes.h term.h config.h client.h terminal.h debug.h 
redir.o : redir.c includes.h term.h config.h client.h terminal.h 
select.o : select.c includes.h term.h config.h client.h terminal.h 
serial.o : serial.c includes.h term.h config.h client.h terminal.h debug.h 
server.o : server.c includes.h term.h config.h client.h terminal.h 
sevenbit.o : sevenbit.c includes.h term.h config.h client.h terminal.h debug.h 
socket.o : socket.c includes.h term.h config.h client.h terminal.h 
statistics.o : statistics.c includes.h term.h config.h client.h terminal.h 
tclient.o : tclient.c includes.h term.h config.h client.h terminal.h 
terminal.o : terminal.c includes.h term.h config.h client.h terminal.h 
test.o : test.c includes.h term.h config.h client.h terminal.h 
tmon.o : tmon.c includes.h term.h config.h client.h terminal.h 
termnet.o : termnet.c includes.h term.h config.h client.h terminal.h 
trshell.o : trshell.c includes.h term.h config.h client.h terminal.h 
upload.o : upload.c includes.h term.h config.h client.h terminal.h 
xconn.o : xconn.c includes.h term.h config.h client.h terminal.h 
shutdown.o : shutdown.c includes.h term.h config.h client.h terminal.h
