SHELL		=	/bin/sh

CC		=	@CC@

MAKECMD		=	make

OSNAME		=	@osname@

X_INCLUDE	=	.
X_LIBS		=	.

OPTIMIZE	=	@OPTIMIZE@
DEBUG		=	

INCLUDES	=	-I$(X_INCLUDE) @DESINCLUDEPATH@
DEFINES		=	-D$(OSNAME) -DOSNAME=$(OSNAME) @DESDEFINES@

CFLAGS		=	$(INCLUDES) $(DEFINES) $(DEBUG) $(OPTIMIZE)	\
				$(EXTRA_CFLAGS) @EXTRACFLAGS@

UTILSLIB	=	afbuutils

LIBRARIES	=	-l$(UTILSLIB) -lm @LIBS@ @DESLIBPATH@ @DESLIB@

LDFLAGS		=	-L$(X_LIBS) $(LIBRARIES) $(EXTRA_LD_FLAGS)

CRYPTSRC	=	crypt.c

SRCS		=	$(CRYPTSRC)	\
			write.c		\
			read.c		\
			buutil.c	\
			utils.c		\
			goptions.c	\
			fileutil.c	\
			x_regex.c	\
			mutils.c	\
			sysutils.c

OBJS		=	$(SRCS:.c=.o)

CRYPTOBJ	=	$(CRYPTSRC:.c=.o)

CRYPTKEY	=	cryptkey.h

PROGRAMS	=	server client cartready label_tape full_backup

BACKUPLIB	=	lib$(UTILSLIB).a

TESTMAINSRCS	=	testmain.c
TESTMAINOBJS	=	$(TESTMAINSRCS:.c=.o)

SERVERSRCS	=	server.c
SERVEROBJS	=	$(SERVERSRCS:.c=.o)

CARTRDYSRCS	=	cartready.c
CARTRDYOBJS	=	$(CARTRDYSRCS:.c=.o)

LABELTAPESRCS	=	label_tape.c
LABELTAPEOBJS	=	$(LABELTAPESRCS:.c=.o)

CLIENTSRCS	=	client.c
CLIENTOBJS	=	$(CLIENTSRCS:.c=.o)

FULLBUSRCS	=	full_backup.c
FULLBUOBJS	=	$(FULLBUSRCS:.c=.o)

TESTCLIENTSRCS	=	testclient.c
TESTCLIENTOBJS	=	$(TESTCLIENTSRCS:.c=.o)

SERVERCONFFILE	=	server.conf
CLIENTCONFFILE	=	client.conf

CLIENTLIBDIR	=	@clientlibdir@
CLIENTBINDIR	=	@clientbindir@
CLIENTVARDIR	=	@clientvardir@
CLIENTMANDIR	=	@clientmandir@
SERVERLIBDIR	=	@serverlibdir@
SERVERBINDIR	=	@serverbindir@
SERVERVARDIR	=	@servervardir@
SERVERMANDIR	=	@servermandir@
SERVERREXECDIR	=	@REXECDIR@

CLIENTMANFILES	=	afclient.X	\
			afrestore.X	\
			afverify.X	\
			full_backup.X	\
			incr_backup.X	\
			print_errors.X	\
			afclient.conf.X
SERVERMANFILES	=	afserver.X	\
			cartis.X	\
			cartready.X	\
			label_tape.X	\
			afserver.conf.X

SRCMANSUFFIX	=	8
DESTMANSUFFIX	=	8
CLIENTMANSRCS	=	$(CLIENTMANFILES:.X=.8)
SERVERMANSRCS	=	$(SERVERMANFILES:.X=.8)
#					     ^ this must be $(SRCMANSUFFIX)

INSTALL		=	cp

.c.o:	conf.h
	$(CC) -c $(CPPFLAGS) $(CFLAGS) $<

all::	$(PROGRAMS)

server:	$(BACKUPLIB) $(SERVEROBJS) $(CARTRDYOBJS) $(LABELTAPEOBJS)  xserverconfig
	$(CC) -o server $(SERVEROBJS) $(LDFLAGS)
	$(CC) -o cartready $(CARTRDYOBJS) $(LDFLAGS)
	$(CC) -o label_tape $(LABELTAPEOBJS) $(LDFLAGS)

cartready:	$(BACKUPLIB) $(CARTRDYOBJS)
	$(CC) -o cartready $(CARTRDYOBJS) $(LDFLAGS)

label_tape:	$(BACKUPLIB) $(LABELTAPEOBJS)
	$(CC) -o label_tape $(LABELTAPEOBJS) $(LDFLAGS)

client:	$(BACKUPLIB) $(CLIENTOBJS) $(FULLBUOBJS) xclientconfig
	$(CC) -o client $(CLIENTOBJS) $(LDFLAGS)
	$(CC) -o full_backup $(FULLBUOBJS) $(LDFLAGS)

$(CRYPTOBJ):	$(CRYPTKEY)

$(CRYPTKEY):
	@csh ./ask_for_key

$(BACKUPLIB):	$(OBJS)
	ar rcv $(BACKUPLIB) $(OBJS)

install:: install.client install.server install.client.man install.server.man

install.client: client full_backup install.client.man
	- mkdir -p $(CLIENTBINDIR) $(CLIENTLIBDIR) $(CLIENTVARDIR)
	- rm -f $(CLIENTBINDIR)/afclient $(CLIENTBINDIR)/client
	$(INSTALL) client $(CLIENTBINDIR)/afclient
#	ln -s afclient $(CLIENTBINDIR)/client
	- strip $(CLIENTBINDIR)/afclient
	- rm -f $(CLIENTBINDIR)/backup $(CLIENTBINDIR)/afbackup
	ln $(CLIENTBINDIR)/afclient $(CLIENTBINDIR)/afbackup
#	ln -s afbackup $(CLIENTBINDIR)/backup
	$(INSTALL) full_backup $(CLIENTBINDIR)/full_backup
	- strip $(CLIENTBINDIR)/full_backup
	- rm -f $(CLIENTBINDIR)/incr_backup $(CLIENTBINDIR)/print_errors \
		$(CLIENTBINDIR)/afrestore $(CLIENTBINDIR)/afbackout \
		$(CLIENTBINDIR)/afverify $(CLIENTBINDIR)/restore \
		$(CLIENTBINDIR)/backout $(CLIENTBINDIR)/verify \
		$(CLIENTBINDIR)/clientconfig \
		$(CLIENTBINDIR)/afclientconfig \
		$(CLIENTBINDIR)/xclientconfig \
		$(CLIENTBINDIR)/xafclientconfig
	ln $(CLIENTBINDIR)/full_backup $(CLIENTBINDIR)/incr_backup
	ln $(CLIENTBINDIR)/full_backup $(CLIENTBINDIR)/print_errors
	ln $(CLIENTBINDIR)/full_backup $(CLIENTBINDIR)/afverify
#	ln -s afverify $(CLIENTBINDIR)/verify
	ln $(CLIENTBINDIR)/full_backup $(CLIENTBINDIR)/afbackout
#	ln -s afbackout $(CLIENTBINDIR)/backout
	ln $(CLIENTBINDIR)/afbackout $(CLIENTBINDIR)/afrestore
#	ln -s afrestore $(CLIENTBINDIR)/restore
	$(INSTALL) clientconfig $(CLIENTBINDIR)/afclientconfig
#	ln -s afclientconfig $(CLIENTBINDIR)/clientconfig
	$(INSTALL) xclientconfig $(CLIENTBINDIR)/xafclientconfig
#	ln -s xafclientconfig $(CLIENTBINDIR)/xclientconfig
	if [ ! -f $(CLIENTLIBDIR)/@clientconf@ ] ; then $(INSTALL) $(CLIENTCONFFILE) $(CLIENTLIBDIR)/@clientconf@ ; fi
	chmod 700 $(CLIENTBINDIR)/*

install.server: server cartready label_tape install.server.man
	- mkdir -p $(SERVERBINDIR) $(SERVERVARDIR) $(SERVERLIBDIR) $(SERVERREXECDIR)
	- rm -f $(SERVERBINDIR)/server $(SERVERBINDIR)/afserver \
		$(SERVERBINDIR)/cartready $(SERVERBINDIR)/cartis \
		$(SERVERBINDIR)/label_tape $(SERVERBINDIR)/__mt \
		$(SERVERBINDIR)/__inc_link \
		$(SERVERBINDIR)/serverconfig \
		$(SERVERBINDIR)/afserverconfig \
		$(SERVERBINDIR)/xafserverconfig \
		$(SERVERBINDIR)/xserverconfig
	$(INSTALL) server $(SERVERBINDIR)/afserver
#	ln -s afserver $(SERVERBINDIR)/server
	$(INSTALL) cartready cartis label_tape __mt __inc_link $(SERVERBINDIR)
	- strip $(SERVERBINDIR)/afserver $(SERVERBINDIR)/cartready $(SERVERBINDIR)/label_tape
	$(INSTALL) serverconfig $(SERVERBINDIR)/afserverconfig
#	ln -s afserverconfig $(SERVERBINDIR)/serverconfig
	$(INSTALL) xserverconfig $(SERVERBINDIR)/xafserverconfig
#	ln -s xafserverconfig $(SERVERBINDIR)/xserverconfig
	chmod 700 $(SERVERBINDIR)/*
	chmod u+s $(SERVERBINDIR)/cartready
	if [ ! -f $(SERVERLIBDIR)/@serverconf@ ] ; then $(INSTALL) $(SERVERCONFFILE) $(SERVERLIBDIR)/@serverconf@ ; fi

install.rclient: install.server install.client
	( for i in full_backup incr_backup afverify ; do rm -f $(SERVERREXECDIR)/"$$i" ; ln -s @clientbindir@/"$$i" $(SERVERREXECDIR)/"$$i" ; done )

install.client.man: $(CLIENTMANSRCS)
	- mkdir -p $(CLIENTMANDIR)/man$(DESTMANSUFFIX)
	( for mansrc in $(CLIENTMANSRCS) ; do destfile=`echo $$mansrc|sed 's/[.]'$(SRCMANSUFFIX)'$$/.'$(DESTMANSUFFIX)/g` ; $(INSTALL) $$mansrc $(CLIENTMANDIR)/man$(DESTMANSUFFIX)/$$destfile ; done )

install.server.man: $(SERVERMANSRCS)
	- mkdir -p $(SERVERMANDIR)/man$(DESTMANSUFFIX)
	( for mansrc in $(SERVERMANSRCS) ; do destfile=`echo $$mansrc|sed 's/[.]'$(SRCMANSUFFIX)'$$/.'$(DESTMANSUFFIX)/g` ; $(INSTALL) $$mansrc $(SERVERMANDIR)/man$(DESTMANSUFFIX)/$$destfile ; done )

xclientconfig: xcc
	./build_cshwish $? > $@
	chmod +x $@

xserverconfig: xsc
	./build_cshwish $? > $@
	chmod +x $@

clean::
	- rm -f *% *.o *.a *~ core* *_

veryclean: clean
	- rm -f $(PROGRAMS)

distclean: clean
	- rm -f $(PROGRAMS) $(CRYPTKEY) xclientconfig xserverconfig
	- rm -f config.cache config.log config.status config.setup Install.cache
	- rm -f `/bin/ls *.in|sed 's/[.]in$$//g'|grep -v configure` confdefs.h
