SHELL		=	/bin/sh

CC		=	@CC@

MAKECMD		=	make

OSNAME		=	@osname@

PACKAGE		=	@PACKAGE@
VERSION		=	@VERSION@

SUBDIRS		=	intl po

X_INCLUDE	=	.
X_LIBS		=	.

OPTIMIZE	=	
DEBUG		=	

INCLUDES	=	-I$(X_INCLUDE) @DESINCLUDEPATH@ @ZLIB_INCLUDEPATH@
DEFINES		=	-D$(OSNAME) -DOSNAME=$(OSNAME) @SYSDEFINES@ \
				@DESDEFINES@ @ZLIB_DEFINES@

CFLAGS		=	@CFLAGS@ $(INCLUDES) $(DEFINES) $(DEBUG) $(OPTIMIZE)	\
				$(EXTRA_CFLAGS) @EXTRACFLAGS@ \
				-DLOCALEDIR=\"$(LOCALEDIR)\" 

UTILSLIB	=	x_utils

X_HEADERS	=	crptauth.h	\
			fileutil.h	\
			mvals.h		\
			netutils.h	\
			packer.h	\
			sysutils.h	\
			utils.h		\
			x_data.h	\
			x_defs.h	\
			x_errno.h	\
			x_regex.h	\
			x_timer.h	\
			x_types.h	\
			zutils.h

X_LIBTOOLS	=	prosname.sh	\
			prosspec.sh


BULIB		=	afbackup

LIBRARIES	=	-l$(BULIB) -l$(UTILSLIB) @LIBS@ @DESLIBPATH@ \
			@DESLIB@ @ZLIB_LIBPATH@ @ZLIB_LIB@ -lm @INTLLIBS@

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

X_LIBSRCS	=	crptauth.c	\
			pack.c		\
			unpack.c	\
			utils.c		\
			goptions.c	\
			fileutil.c	\
			x_regex.c	\
			mutils.c	\
			sysutils.c	\
			netutils.c	\
			timeutil.c	\
			typeutil.c	\
			safewrap.c	\
			zutils.c

X_LIBOBJS	=	$(X_LIBSRCS:.c=.o)

BULIBSRCS	=	buutil.c	\
			prot.c

BULIBOBJS	=	$(BULIBSRCS:.c=.o)

CRYPTKEY	=	cryptkey.h

CL_HELPERS	=	__piper __packpats @DESCRPT@ @ZIP@
SV_HELPERS	=	__numset

PROGRAMS	=	afserver afmserver afclient cartready label_tape \
				full_backup $(CL_HELPERS) $(SV_HELPERS)

RESTRCLNTPROGS	=	afclient afbackup afrestore afverify copy_tape \
			full_backup incr_backup afbackout update_indexes \
			__descrpt __z
CLIENTPROGRAMS	=	$(RESTRCLNTPROGS) autocptapes afclientconfig \
			xafclientconfig xafrestore
RESTRSVRPROGS	=	afmserver afserver label_tape cartready changerready \
			cartis
SERVERPROGRAMS	=	$(RESTRSVRPROGS) afserverconfig cartagehandler \
			xafserverconfig xafserverstatus __inc_link __mt __numset

X_LIBRARY	=	lib$(UTILSLIB).a
BACKUPLIB	=	lib$(BULIB).a

BASELIBS	=	intllibs

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

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

MSERVERSRCS	=	mserver.c
MSERVEROBJS	=	$(MSERVERSRCS:.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

CHANGERCONFFILE	=	changer.conf

prefix		=	@prefix@
exec_prefix	=	@exec_prefix@

CLIENTLIBDIR	=	@clientlibdir@
CLIENTBINDIR	=	@clientbindir@
CLIENTVARDIR	=	@clientvardir@
CLIENTMANDIR	=	@clientmandir@
SERVERLIBDIR	=	@serverlibdir@
SERVERBINDIR	=	@serverbindir@
SERVERVARDIR	=	@servervardir@
SERVERMANDIR	=	@servermandir@
COMMONDIR	=	@commondir@
COMMONDATADIR	=	@commondatadir@
COMMONSHLIBDIR	=	@commonshlibdir@

BINDIR		=	@bindir@
SBINDIR		=	@sbindir@
LIBEXECDIR	=	@libexecdir@
DATADIR		=	@datadir@
SYSCONFDIR	=	@sysconfdir@
MANDIR		=	@mandir@
VARDIR		=	@localstatedir@
REXECDIR	=	@REXECDIR@

# Where to install locale stuff
LOCALEDIR	=	$(DATADIR)/locale

CLIENTMANFILES	=	afclient.X	\
			afrestore.X	\
			afverify.X	\
			full_backup.X	\
			incr_backup.X	\
			copy_tape.X	\
			update_indexes.X	\
			xafrestore.X	\
			afclient.conf.X
SERVERMANFILES	=	afserver.X	\
			afmserver.X	\
			cartis.X	\
			cartready.X	\
			label_tape.X	\
			cart_ctl.X	\
			afserver.conf.X

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

INSTALL		=	install -C

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

all::	$(PROGRAMS) potfiles

server:	afserver potfiles
	@:

afserver:	$(CRYPTKEY) $(BACKUPLIB) $(X_LIBRARY) $(BASELIBS) $(SERVEROBJS) $(MSERVEROBJS) $(CARTRDYOBJS) $(LABELTAPEOBJS) xserverconfig xserverstatus $(SV_HELPERS)
	$(CC) -o afserver $(SERVEROBJS) $(LDFLAGS)
	$(CC) -o afmserver $(MSERVEROBJS) $(LDFLAGS)
	$(CC) -o cartready $(CARTRDYOBJS) $(LDFLAGS)
	$(CC) -o label_tape $(LABELTAPEOBJS) $(LDFLAGS)

$(SERVEROBJS):	$(CRYPTKEY)

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

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

client:	afclient potfiles
	@:

afclient:	$(CRYPTKEY) $(BACKUPLIB) $(X_LIBRARY) $(BASELIBS) $(CLIENTOBJS) $(FULLBUOBJS) xclientconfig xrestore $(CL_HELPERS)
	$(CC) -o afclient $(CLIENTOBJS) $(LDFLAGS)
	$(CC) -o full_backup $(FULLBUOBJS) $(LDFLAGS)

$(CLIENTOBJS):	$(CRYPTKEY)

$(CRYPTKEY):	ask_for_key
	@if [ -f cryptkey ]; then sh ./ask_for_key <cryptkey; else sh ./ask_for_key; fi

potfiles: intllibs
	(cd po; $(MAKE) DATADIR=\"$(DATADIR)\" all)

intllibs:
	(cd intl; $(MAKE) all)

__piper:	__piper.o $(BACKUPLIB) $(X_LIBRARY)
	$(CC) -o __piper __piper.o $(LDFLAGS)

__packpats:	__packpats.o $(BACKUPLIB) $(X_LIBRARY)
	$(CC) -o __packpats __packpats.o $(LDFLAGS)

__numset:	__numset.o $(BACKUPLIB) $(X_LIBRARY)
	$(CC) -o __numset __numset.o $(LDFLAGS)

__descrpt:	$(CRYPTKEY) __descrpt.o $(BACKUPLIB) $(X_LIBRARY)
	$(CC) -o __descrpt __descrpt.o $(LDFLAGS)

__z:	__z.o $(BACKUPLIB) $(X_LIBRARY)
	$(CC) -o __z __z.o $(LDFLAGS)

$(BACKUPLIB):	$(BULIBOBJS)
	ar rcv $(BACKUPLIB) $(BULIBOBJS)
	if [ _@ranlib@ != _ ] ; then @ranlib@ $(BACKUPLIB) ; fi

$(X_LIBRARY):	$(X_LIBOBJS)
	ar rcv $(X_LIBRARY) $(X_LIBOBJS)
	if [ _@ranlib@ != _ ] ; then @ranlib@ $(X_LIBRARY) ; fi

$(UTILSLIB)::	$(X_LIBRARY)

install.$(UTILSLIB): $(UTILSLIB)
	install -d @utilsincdir@ @utilsincdir@/$(OSNAME) @utilslibdir@ \
		@utilslibdir@/$(OSNAME) @utilsbindir@
	for i in $(X_HEADERS) ; do /bin/rm -f @utilsincdir@/$$i ; \
		$(INSTALL) $$i @utilsincdir@/$$i ; done
	for i in $(X_LIBRARY) ; do /bin/rm -f @utilslibdir@/$(OSNAME)/$$i ; \
		$(INSTALL) $$i @utilslibdir@/$(OSNAME)/$$i ; done
	$(INSTALL) -m 0644 lconf.h @utilsincdir@/$(OSNAME)/lconf.h
	$(INSTALL) -m 0644 lconf.gen.h @utilsincdir@/lconf.h
	for i in $(X_LIBTOOLS) ; do \
		$(INSTALL) -m 0644 $$i @utilsbindir@/$$i ; done

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

install.client: client full_backup install.client.man install.l10n
	install -d $(CLIENTBINDIR) $(CLIENTLIBDIR) $(COMMONSHLIBDIR) $(LIBEXECDIR)
	install -d -o afbackup -g operator $(CLIENTVARDIR)
	$(INSTALL) -g operator -m 0750 afclient $(SBINDIR)
	ln -sf $(SBINDIR)/afclient $(SBINDIR)/afbackup
	$(INSTALL) full_backup $(SBINDIR)/full_backup
	ln -sf $(SBINDIR)/full_backup $(SBINDIR)/incr_backup
	ln -sf $(SBINDIR)/full_backup $(SBINDIR)/afverify
	ln -sf $(SBINDIR)/full_backup $(SBINDIR)/copy_tape
	ln -sf $(SBINDIR)/full_backup $(SBINDIR)/afbackout
	ln -sf $(SBINDIR)/full_backup $(SBINDIR)/update_indexes
	ln -sf $(SBINDIR)/full_backup $(SBINDIR)/afrestore
	$(INSTALL) -m 0755 clientconfig $(SBINDIR)/afclientconfig
	ln -sf $(SBINDIR)/afclientconfig $(SBINDIR)/clientconfig
	if [ -z "$(NO_GUI)" ]; then \
	    $(INSTALL) -m 0755 xclientconfig $(SBINDIR)/xafclientconfig; \
	    ln -sf $(SBINDIR)/xafclientconfig $(SBINDIR)/xclientconfig; \
	    $(INSTALL) -m 0755 xrestore $(SBINDIR)/xafrestore; \
	    ln -sf $(SBINDIR)/xafrestore $(SBINDIR)/xrestore; \
	fi
	$(INSTALL) -m 0755 autocptapes $(SBINDIR)/autocptapes
	$(INSTALL) -m 0755 __packpats $(LIBEXECDIR)/__packpats
	$(INSTALL) -m 0755 __piper $(LIBEXECDIR)/__piper
	if [ ! -z "@DESCRPT@" ] ; then \
		$(INSTALL) -m 0755 "@DESCRPT@" $(LIBEXECDIR)/@DESCRPT@ ; \
	fi
	if [ ! -z "@ZIP@" ] ; then \
		$(INSTALL) -m 0755 "@ZIP@" $(LIBEXECDIR)/@ZIP@ ; \
	fi
	$(INSTALL) -m 0644 aftcllib.tcl $(COMMONSHLIBDIR)/aftcllib.tcl
	if [ ! -f $(SYSCONFDIR)/@clientconf@.sample ] ; then $(INSTALL) -m 0644 $(CLIENTCONFFILE) $(CLIENTLIBDIR)/@clientconf@.sample ; fi

install.server: server cartready label_tape install.server.man install.l10n
	install -d $(SERVERBINDIR) $(SERVERLIBDIR) $(COMMONSHLIBDIR) $(REXECDIR) $(LIBEXECDIR)
	install -d -o afbackup -g operator $(SERVERVARDIR)
	$(INSTALL) -o afbackup -g operator -m 0700 afserver afmserver $(LIBEXECDIR)
	$(INSTALL) -m 04755 -o afbackup cartready $(SBINDIR) 
	$(INSTALL) -g operator -m 0750 label_tape $(SBINDIR) 
	$(INSTALL) -m 0755 __mt __inc_link $(LIBEXECDIR)
	ln -sf $(SBINDIR)/label_tape $(SBINDIR)/cart_ctl
	ln -sf $(SBINDIR)/label_tape $(SBINDIR)/cartis
	ln -sf $(SBINDIR)/cartready $(SBINDIR)/changerready
	$(INSTALL) -m 0755 serverconfig $(SBINDIR)/afserverconfig
	ln -sf afserverconfig $(SBINDIR)/serverconfig
	if [ -z "$(NO_GUI)" ]; then \
	    $(INSTALL) -m 0755 xserverconfig $(SBINDIR)/xafserverconfig; \
	    ln -sf xafserverconfig $(SBINDIR)/xserverconfig; \
	    $(INSTALL) -m 0755 xserverstatus $(SBINDIR)/xafserverstatus; \
	    ln -sf xafserverstatus $(SBINDIR)/xserverstatus; \
	fi
	$(INSTALL) -m 0644 aftcllib.tcl $(COMMONSHLIBDIR)/aftcllib.tcl
	$(INSTALL) -m 0755 __numset $(LIBEXECDIR)/__numset
	if [ ! -f $(LIBEXECDIR)/cartagehandler ] ; then $(INSTALL) cartagehandler $(LIBEXECDIR)/cartagehandler ; fi
	if [ ! -f $(SERVERLIBDIR)/@serverconf@.sample ] ; then $(INSTALL) -m 0644 $(SERVERCONFFILE) $(SERVERLIBDIR)/@serverconf@.sample ; fi
	if [ ! -f $(SERVERLIBDIR)/$(CHANGERCONFFILE).sample ] ; then $(INSTALL) -m 0644 $(CHANGERCONFFILE) $(SERVERLIBDIR)/$(CHANGERCONFFILE).sample ; fi
	touch $(SERVERVARDIR)/readonly_tapes

install.rclient: install.server install.client install.rexeclinks

install.userrestore:
	$(INSTALL) -m 04755 -o root -g wheel full_backup $(SBINDIR)/afrestore
	ln -sf $(SBINDIR)/afrestore $(SBINDIR)/afbackout
	ln -sf $(SBINDIR)/afrestore $(SBINDIR)/update_indexes

install.rexeclinks:
	( for i in full_backup incr_backup afverify afrestore copy_tape update_indexes ; do ln -sf $(CLIENTBINDIR)/"$$i" $(REXECDIR)/"$$i" ; done )

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

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

install.l10n: potfiles
	(cd po; $(MAKE) datadir=\"$(DATADIR)\" install)

xclientconfig: xcc
	if [ -z "${NO_GUI}" ]; then ( ./build_shwish $? > $@; chmod +x $@) fi

xserverconfig: xsc
	if [ -z "${NO_GUI}" ]; then ( ./build_shwish $? > $@; chmod +x $@) fi

xserverstatus: xss
	if [ -z "${NO_GUI}" ]; then ( ./build_shwish $? > $@; chmod +x $@) fi

xrestore: xrs
	if [ -z "${NO_GUI}" ]; then ( ./build_shwish $? > $@; chmod +x $@) fi

clean::
	- for subdir in $(SUBDIRS) ; do (cd $$subdir && $(MAKE) clean ) ; done
	- rm -f *% *.o *.a *~ core* *_

veryclean: clean
	- rm -f $(PROGRAMS)

distclean: clean
	- for subdir in $(SUBDIRS) ; do (cd $$subdir && $(MAKE) distclean ) ; done
	- rm -f $(PROGRAMS) $(CRYPTKEY) xclientconfig xserverconfig xrestore xserverstatus
	- 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

