#
# BACKUPD Makefile for Linux
#
# Ullrich von Bassewitz, 25.06.1998
#

# ------------------------------------------------------------------------------
# Change this according to your needs:

PREFIX = /usr

# ------------------------------------------------------------------------------
# Definitions

# Names of executables
AR = ar
LD = ld
CC = gcc

# Flags for the compiler
CFLAGS 	= -DLINUX -g -I $(INCDIR) -O2 -Wall


# ------------------------------------------------------------------------------
# All OBJ files

OBJS =	backupd.o      	\
	check.o		\
	client.o	\
	config.o       	\
	error.o	       	\
	extcmd.o	\
	global.o	\
	sig.o	       	\
	util.o


%.o:	%.cc
	$(CC) $(CFLAGS) -c $< -o $@

# ------------------------------------------------------------------------------
# Dummy targets

.PHONY:	all
ifeq (.depend,$(wildcard .depend))
all:	backupd backupc
include .depend
else
all:	depend
	@echo ""
	@echo "Run make again to make the binaries"
	@echo
endif

# ------------------------------------------------------------------------------
# Create the executables

backupd:	$(OBJS)
		gcc $(CFLAGS) -o $@ $(OBJS)

backupc:	backupc.o
		gcc $(CFLAGS) -o $@ backupc.o

# ------------------------------------------------------------------------------
# Create a dependency file

depend dep:
	@echo "Creating dependency information"
	$(CC) -I$(INCDIR) -DLINUX -MM *.c > .depend

# ------------------------------------------------------------------------------
# Install the stuff

.PHONY:	install
install:       	backupd backupc strip
		@if [ `id -u` != 0 ]; then				      \
		    echo "";   	     					      \
		    echo 'Do "make install" as root';			      \
		    echo "";   	     					      \
		    false;     	     					      \
		fi
		@# Install the binaries
		install -o root -g root -m 0755 backupd $(PREFIX)/sbin
		install -o root -g root -m 0755 backupc $(PREFIX)/bin
		@# Add an entry to /etc/services
		@cp /etc/services /tmp/services.old
		@gawk '		     					      \
		BEGIN {		     					      \
		  IGNORECASE=1;	     					      \
		}		     				    	      \
		{	      	       				      	      \
		  if (match ($$0, "^\# End of services")) {		      \
		    if (gotit == 0) {					      \
		      printf "backupd\t\t12153/tcp\t\t\t\# Backup daemon\n";  \
		      gotit = 1;     					      \
		    }	      	     					      \
		  }	      	     					      \
		  if (match ($$0, "^backupd")) {		      	      \
		    gotit = 1;	     				      	      \
		  }		     					      \
		  print;	     					      \
		}	     	     					      \
		END {		     					      \
		  if (gotit == 0) {  					      \
		    printf "backupd\t\t12153/tcp\t\t\t\# Backup daemon\n";    \
		  }		     					      \
		}' /tmp/services.old > /tmp/services.tmp
		cp /tmp/services.tmp /etc/services
		@# Add an entry to /etc/inetd.conf
		@cp /etc/inetd.conf /tmp/inetd.conf.old
		@gawk '		     		       			      \
		BEGIN {		     		       			      \
		  IGNORECASE=1;	     		       			      \
		}		     		       			      \
		{	      	     		       		      	      \
		  if (match ($$0, "^\# End of inetd")) {		      \
		    if (gotit == 0) {		       			      \
       	       	      print "\# Backup server";        			      \
		      print "backupd stream tcp nowait root /usr/sbin/tcpd $(PREFIX)/sbin/backupd"; \
		      gotit = 1;     		       			      \
		    }	      	     		       			      \
		  }	      	     		       			      \
		  if (match ($$0, "^backupd")) {       		      	      \
		    gotit = 1;	     		       		      	      \
		  }		     		       			      \
		  print;	     		       			      \
		}		     		       			      \
		END {		     		       			      \
		  if (gotit == 0) {  		       			      \
       	       	    print "\# Backup server"; 	       		      	      \
		    print "backupd stream tcp nowait root /usr/sbin/tcpd $(PREFIX)/sbin/backupd"; \
		  }		     		       			      \
		}' /tmp/inetd.conf.old > /tmp/inetd.conf.tmp
		cp /tmp/inetd.conf.tmp /etc/inetd.conf
		@killall -HUP inetd
		touch /etc/backupd.conf
		@echo ""
		@echo "Please check /etc/services and /etc/inetd.conf for correctness,"
		@echo "and remember to edit the /etc/backupd.conf file!"


# ------------------------------------------------------------------------------
# Strip debug info

.PHONY:	strip
strip:
	strip backupd backupc


# ------------------------------------------------------------------------------
# clean up



