#
# Makefile for the backup system.  Since it isn't wise (or possible)
# to run setuid or setgid perl scripts, we use a C wrapper program
# that is setgid which runs perl on the script.  So installation
# consists of putting a bunch of scripts somewhere and in putting the
# compiled wrappers somewhere.  
#
# This Makefile provides several entries:
#	all		compiles the wrappers
#	install		copies the wrappers and scripts as needed to
#			the suitable places.
#	clean		removes junk from this directory
#	print		prints everything
#
# There are several variables here at the beginning that you might
# want to hack on.
#

#
# PERL is the pathname of perl as installed on your systems.
#
PERL =	/usr/athena/bin/perl

#
# SDIR is the directory in which all scripts should be placed.
#
SDIR =	/usr/local/backup/bin

#
# BDIR is the directory in which all binaries should be placed.
#
BDIR =	/usr/local/backup/bin

# 
# PERLLIBDIR is the directory in which Perl libraries should be placed.
# 
PERLLIBDIR = /usr/local/backup/lib/perl

#
# PRINTER is the name of the printer to print things to.  Note that
# the print: entry below uses 'enscript' and 'up' - you may need to
# change it to suit your environment (or get enscript and up!)
#
PRINTER =	meadow

#
# BACKUP is the name of the group that owns and can access the backup
# database files.  The backup programs run setgid to backup in the hopes
# that they will be able to write them.
#
BACKUP =	staff

#
# You might need to change these also...
#
RM =	/bin/rm
LN =	/bin/ln
CC =	/afs/athena/project/gnu/bin/gcc
CP =	/bin/cp
CHGRP =	/bin/chgrp
CHMOD =	/bin/chmod
#
# What strrchr to use - strrchr or rindex.
#
STRRCHR = strrchr			# Suns, Encore, HP, BBN Butterfly
#STRRCHR = rindex			# Pyramid
#
# What setreuid to use - define SETRESUID if this system uses it rather than
# setreuid.
#
#SETRESUID = 				# Sun, Encore, Pyramid, Butterfly
SETRESUID = -DNEED_SETUID		# Solaris
#SETRESUID = -DSETRESUID		# HP


EXTRALIBS = "/usr/ucblib/libucb.a"

#
#----------------------------------------------------------------------
# The rest of the stuff shouldn't be changed in general.
#

#
# list of the executable programs we will generate from the wrapper.
#
EXEC =				\
	add-tape		\
	all-backups		\
	backup-dump		\
	backup-gtar		\
	check-backups		\
	clean-db		\
	db-edit			\
	do-backups		\
	getback			\
	getdata			\
	list-backups		\
	restore-dump		\
	reuse			\
	run-clean-db		\
	show			\
	tape-check		\
	unlock-dumpdates	\
	krsh			\
	offsite			\
	checkinout

#
# list of the Perl scripts that are used in the backup system 
#
SCRIPTS = 			\
	add-tape.perl		\
	all-backups.perl	\
	backup-dump.perl	\
	backup-gtar.perl	\
	backuplib.perl		\
	check-backups.perl	\
	clean-db.perl		\
	db-edit.perl		\
	do-backups.perl		\
	getback.perl		\
	getdata.perl		\
	lib-db.perl		\
	lib-lock.perl		\
	lib-log.perl		\
	lib-misc.perl		\
	lib-unctime.perl	\
	lib-tape.perl		\
	list-backups.perl	\
	restore-dump.perl	\
	reuse.perl		\
	run-clean-db.perl	\
	show.perl		\
	tape-check.perl		\
	unlock-dumpdates.perl	\
	krsh.perl		\
	offsite.perl		\
	checkinout.perl

#
# other miscellaneous stuff
#
OTHER =	global.defs local.defs
PERLLIB = yagrip.pl

#
#----------------------------------------------------------------------
# The entry points...
#
# all: compiles everything, places it in the current directory.
#
all:	wrapper

#
# For all the things that use the wrapper, here's how to make them...
#	-DSETSAME tells wrapper to set the uid = euid and gid = egid
#	-DPERL is the path to perl
#	-DSCRIPT is the path to the script directory $SDIR
#
wrapper:	wrapper.c
	$(CC) -o wrapper -DSETSAME -DPERL=\"$(PERL)\" -DSCRIPT=\"$(SDIR)\" -DPERLLIBDIR=\"$(PERLLIBDIR)\" -DSTRRCHR=$(STRRCHR) $(SETRESUID) wrapper.c $(EXTRALIBS)

#
# install: copy to SDIR and BDIR
#
install:	install-scripts install-other install-progs install-links

install-scripts:	maketime.scripts

install-progs:		maketime.progs

install-other:		maketime.other

install-links:		maketime.links

maketime.other:		$(OTHER) $(PERLLIB)
	@for NAME in $(OTHER) ; \
		do echo Installing $$NAME ; \
		$(CP) $$NAME $(SDIR) ; \
		$(CHMOD) 0644 $(SDIR)/$$NAME ; \
	done
	@for NAME in $(PERLLIB) ; \
		do echo Installing $$NAME ; \
		$(CP) $$NAME $(PERLLIBDIR) ; \
		$(CHMOD) 0644 $(PERLLIBDIR)/$$NAME ; \
	done
	-touch maketime.other

maketime.scripts:	$(SCRIPTS)
	@for NAME in $? ; \
		do echo Installing $$NAME ; \
		$(CP) $$NAME $(SDIR) ; \
		$(CHMOD) 0644 $(SDIR)/$$NAME ; \
	done
	-touch maketime.scripts

maketime.progs:		wrapper backup-init backup-cleanup
	@for NAME in $? ; \
		do echo Installing $$NAME ; \
		$(CP) $$NAME $(BDIR) ; \
		$(CHGRP) $(BACKUP) $(BDIR)/$$NAME ; \
		$(CHMOD) g+s $(BDIR)/$$NAME ; \
	done
	-touch maketime.progs

maketime.links:		
	@for NAME in $(EXEC) ; \
		do echo Remake $$NAME link ; \
		$(RM) -f $(BDIR)/$$NAME ; \
		$(LN) -s $(BDIR)/wrapper $(BDIR)/$$NAME ; \
	done
	-touch maketime.links

#
# clean: clean up this messy directory
#
clean:
	$(RM) -i -f *~ *.o wrapper *.unknown

#
# syntax: check perl syntax
#
syntax:
	@for NAME in $(SCRIPTS) global.defs local.defs ; \
		do perl -c $$NAME ; \
	done

#
# variables: check for unknown variables
#
variables:
	-@for NAME in $(SCRIPTS) ; \
		do echo $$NAME ; \
		NNAME=`basename $$NAME .perl` ; \
		perl -w -c $$NAME > $$NNAME.unknown 2>&1 ; \
	done

#
# print: print everything in a nice way
#
print:
	enscript -G -p - $(SCRIPTS) $(OTHER) wrapper.c | up -n 4up | lpr -P $(PRINTER)

yagrip.pl: ../misc/yagrip.pl
	ln -s ../misc/yagrip.pl yagrip.pl
