SHELL = /bin/sh

prefix=@prefix@
exec_prefix=@exec_prefix@
libexecdir=@libexecdir@
confdir=@confdir@
sbindir=@sbindir@
mandir=@mandir@

srcdir=@srcdir@
top_srcdir=@top_srcdir@
BUILDTOP=..
# VPATH is used by GNU make to allow building outside the source tree
VPATH=@srcdir@
CC=@CC@
ENSUREDIR=${top_srcdir}/ensure-dir.sh
INSTALL=@INSTALL@
INSTALL_PROGRAM=@INSTALL_PROGRAM@
INSTALL_DATA=@INSTALL_DATA@

DEBUG=-O
CFLAGS=${DEBUG} -DCONFDIR=\"${confdir}\" -I${BUILDTOP} -I${top_srcdir}/config  -I${top_srcdir}/common-src @CPPFLAGS@
# -I${top_srcdir}/server-src -I${top_srcdir}/common-src @CPPFLAGS@
LDFLAGS=${DEBUG} -L${BUILDTOP}/common-src @LDFLAGS@

SUF=@SUF@

AMLIB=${BUILDTOP}/common-src/libamanda.a

LIBS=-lamanda @LIBS@

OBJS= amtape.o amcheck.o amlabel.o amrestore.o amadmin.o amflush.o \
      taper.o dumper.o planner.o reporter.o getconf.o driver.o driverio.o

DRVSIMS=   drv-orig drv-drain-ends drv-drain-npos
USRPROGS=  amrestore${SUF} amadmin${SUF} amflush${SUF} amlabel${SUF} \
	   amcheck${SUF} amdump${SUF} amcleanup${SUF} amtape${SUF}
LIBPROGS=  taper${SUF} dumper${SUF} driver${SUF} planner${SUF} \
	   reporter${SUF} getconf${SUF}
SUIDEXEC=  dumper${SUF} planner${SUF}
SUIDUSR=   amcheck${SUF}

all server: ${LIBPROGS} ${USRPROGS}

amtape${SUF}: amtape.o ${AMLIB}
	${CC} ${LDFLAGS} -o $@ amtape.o ${LIBS}

amcheck${SUF}: amcheck.o ${AMLIB}
	${CC} ${LDFLAGS} -o $@ amcheck.o ${LIBS}

amlabel${SUF}: amlabel.o ${AMLIB}
	${CC} ${LDFLAGS} -o $@ amlabel.o ${LIBS}

amrestore${SUF}: amrestore.o ${AMLIB}
	${CC} ${LDFLAGS} -o $@ amrestore.o ${LIBS}

amadmin${SUF}: amadmin.o ${AMLIB}
	${CC} ${LDFLAGS} -o $@ amadmin.o ${LIBS}

amflush${SUF}: amflush.o ${AMLIB}
	${CC} ${LDFLAGS} -o $@ amflush.o ${LIBS}

taper${SUF}: taper.o ${AMLIB}
	${CC} ${LDFLAGS} -o $@ taper.o ${LIBS}

dumper${SUF}: dumper.o ${AMLIB}
	${CC} ${LDFLAGS} -o $@ dumper.o ${LIBS}

planner${SUF}: planner.o ${AMLIB}
	${CC} ${LDFLAGS} -o $@ planner.o ${LIBS}

reporter${SUF}: reporter.o ${AMLIB}
	${CC} ${LDFLAGS} -o $@ reporter.o ${LIBS}

getconf${SUF}: getconf.o ${AMLIB}
	${CC} ${LDFLAGS} -o $@ getconf.o ${LIBS}

driver${SUF}: driver.o driverio.o ${AMLIB}
	${CC} ${LDFLAGS} -o $@ driver.o driverio.o ${LIBS}

#
# {amdump,amcleanup} generated from {amdump,amcleanup}.in by configure
# when it's run.
#
amdump: ${srcdir}/amdump.in
	(cd ${top_srcdir} ; ./config.status)

amcleanup: ${srcdir}/amcleanup.in
	(cd ${top_srcdir} ; ./config.status)


install-libexec-bins:
	@echo "Installing Amanda libexec programs:"
	$(ENSUREDIR) $(libexecdir) 755
	@for f in $(LIBPROGS); do \
		echo " " $(INSTALL_PROGRAM) $$f $(libexecdir); \
		$(INSTALL_PROGRAM) $$f $(libexecdir); \
	done

install-usr-bins:
	@echo "Installing Amanda user programs:"
	$(ENSUREDIR) $(sbindir) 755
	@for f in $(USRPROGS); do \
		echo " " $(INSTALL_PROGRAM) $$f $(sbindir); \
		$(INSTALL_PROGRAM) $$f $(sbindir); \
	done

install-libexec-perms: @server_suid_p@-libexec-perms
install-usr-perms: @server_suid_p@-usr-perms

no-libexec-perms:
no-usr-perms:

suid-libexec-perms:
	@echo "Setting permissions for setuid-root libexec programs:"
	@for f in $(SUIDEXEC); do \
	   echo "  chown root $(libexecdir)/$$f" ;\
	   chown root $(libexecdir)/$$f || echo "Error: chown of $$f failed.";\
	   echo "  chmod u+s $(libexecdir)/$$f" ;\
	   chmod u+s $(libexecdir)/$$f || echo "Error: chmod of $$f failed.";\
	done

suid-usr-perms:
	@echo "Setting permissions for setuid-root user programs:"
	@for f in $(SUIDUSR); do \
	    echo "  chown root $(sbindir)/$$f" ;\
	    chown root $(sbindir)/$$f || echo "Error: chown of $$f failed.";\
	    echo "  chmod u+s $(sbindir)/$$f" ;\
	    chmod u+s $(sbindir)/$$f || echo "Error: chmod of $$f failed.";\
	done

install-perms: install-libexec-perms install-usr-perms
install-libexec: install-libexec-bins install-libexec-perms
install-usr: install-usr-bins install-usr-perms

install install-server: install-libexec install-usr

common client install-client install-man:

clean:
	-rm -f ${USRPROGS} ${LIBPROGS} ${OBJS}

mostlyclean: clean

distclean: clean
	-rm -f Makefile
