#
#   Copyright 2009 Free Software Foundation, Inc.
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#	Makefile for main sources
#

SHELL		=	/bin/sh
@SET_MAKE@
prefix		=	@prefix@
exec_prefix	=	@exec_prefix@
CC		=	@CC@
CCFLAGS		=	-O @gcc_useful_options@ @funny_compiler_options@
LDFLAGS		=	-s
LIBS		=	@LIBS@
LIBCURSES	=	@LIBCURSES@
LIBDIR		=	@libdir@
LIBNET		=	@SOCKLIBS@
MV		=	mv
RM		= 	rm -f
HDRS		=	hdrs
PARENT		=	.
BASE		=	..
LIB		=	lib/libgnubatch_int.a
SHLIB		=	lib/libgnubatch_int.so
SHLIBCLIENT	=	lib/libgnubatch_client.so
SHLIBCURS	=	lib/libgnubatch_curs.so
CFLAGS		=	$(CCFLAGS) -I$(HDRS) -I$(BASE)
LDFLAGS		=
INSTALL		=	@INSTALL@
INSTALL_DATA	=	@INSTALL_DATA@
BATCHUSER	=	@BATCHUSER@
USERMODES	=	-o $(BATCHUSER) -g root -m 4755
NOSETMODES	=	-o $(BATCHUSER) -g root
SUIDROOT	=	-o root -g root -m 4755
TTYGROUP	=	-o $(BATCHUSER) -g tty -m 2755
BINDIR		=	@bindir@
INTBINDIR	=	@pkgexecdir@
USERBINS	=	gbch-charge gbch-cichange gbch-cilist gbch-hols gbch-jchange \
			gbch-jdel gbch-jlist gbch-jstat gbch-q gbch-quit gbch-r \
			gbch-rr gbch-start gbch-uchange gbch-ulist gbch-user gbch-var \
			gbch-vlist
NOSETUBINS	=	gbch-filemon
SUIDROOTBINS	=	btsched btexec btpwchk btmdisp xbnetserv
SUIDBTBINS	=	jobdump
TTYGRPBINS	=	btwrite dosbtwrite

PROGS		=	$(USERBINS) $(NOSETUBINS) $(SUIDROOTBINS) $(SUIDBTBINS) $(TTYGRPBINS)

SHPROGS		=	btcharge_s btcichange_s btcilist_s bthols_s btjchange_s \
			btjdel_s btjlist_s btjstat_s btq_s btquit_s btr_s \
			rbtr_s btstart_s btuchange_s btulist_s btuser_s btvar_s \
			btvlist_s \
			btfilemon_s \
			btsched_s btexec btpwchk btmdisp xbnetserv_s \
			jobdump_s btwrite_s dosbtwrite_s

BTSTARTLNKS	=	gbch-disconn gbch-conn gbch-dst
BTJDELLNKS	=	gbch-go gbch-goadv

BTCHARGEOBJS	=	btcharge.o
BTCICHANGEOBJS	=	btcichange.o
BTCILISTOBJS	=	btcilist.o
BTHOLSOBJS	=	bthols.o
BTJCHANGEOBJS	=	btjchange.o rbt_access.o
BTJDELOBJS	=	btjdel.o
BTJLISTOBJS	=	btjlist.o
BTJSTATOBJS	=	btjstat.o
BTQOBJS		=	btq.o bq_jlist.o bq_jobops.o bq_viewj.o bq_jci.o bq_paredit.o bq_vlist.o bq_varops.o bq_miscscr.o wtimes.o bq_propts.o bq_hols.o
BTQUITOBJS	=	btquit.o
BTROBJS		=	btr.o
RBTROBJS	=	rbtr.o rbt_access.o rbt_job.o
BTSTARTOBJS	=	btstart.o
BTUCHANGEOBJS	=	btuchange.o
BTULISTOBJS	=	btulist.o
BTUSEROBJS	=	btuser.o btu_propts.o
BTVAROBJS	=	btvar.o
BTVLISTOBJS	=	btvlist.o
BTFILEMONOBJS	=	btfilemon.o
BTSCHEDOBJS	=	btsched.o sh_jlist.o sh_cmdint.o sh_vlist.o sh_oper.o sh_exec.o sh_misc.o sh_log.o sh_network.o sh_netlock.o sh_netfeed.o sh_pack.o
BTEXECOBJS	=	btexec.o
BTPWCHKOBJS	=	btpwchk.o
BTMDISPOBJS	=	btmdisp.o
XBNETSERVOBJS	=	xbnetserv.o xbnet_ua.o xbnet_api.o

JOBDUMPOBJS	=	jobdump.o
BTWRITEOBJS	=	btwrite.o
DOSBTWRITEOBJS	=	dosbtwrite.o

all:	@main_target@

# netsubdirs is set to apilibd and remcgid if building for networks

nonshared:	$(PROGS) insd gtkd loccgid @netsubdirs@

shared:		$(SHPROGS) insd gtkd loccgid @netsubdirs@

# Each target is nonshared and shared version with _s which we create as a "touched file"
# (Keeping pre-GNU names for _s files)

gbch-charge:	$(BTCHARGEOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTCHARGEOBJS) $(LIB) $(LIBNET) $(LIBS)

btcharge_s:	$(BTCHARGEOBJS) $(LIB)
		$(CC) -o gbch-charge $(LDFLAGS) $(BTCHARGEOBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

gbch-cichange:	$(BTCICHANGEOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTCICHANGEOBJS) $(LIB) $(LIBNET) $(LIBS)

btcichange_s:	$(BTCICHANGEOBJS) $(LIB)
		$(CC) -o gbch-cichange $(LDFLAGS) $(BTCICHANGEOBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

gbch-cilist:	$(BTCILISTOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTCILISTOBJS) $(LIB) $(LIBNET) $(LIBS)

btcilist_s:	$(BTCILISTOBJS) $(LIB)
		$(CC) -o gbch-cilist $(LDFLAGS) $(BTCILISTOBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

gbch-hols:	$(BTHOLSOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTHOLSOBJS) $(LIB) $(LIBNET) $(LIBS)

bthols_s:	$(BTHOLSOBJS) $(LIB)
		$(CC) -o gbch-hols $(LDFLAGS) $(BTHOLSOBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

gbch-jchange:	$(BTJCHANGEOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTJCHANGEOBJS) $(LIB) $(LIBNET) $(LIBS)

btjchange_s:	$(BTJCHANGEOBJS) $(LIB)
		$(CC) -o gbch-jchange $(LDFLAGS) $(BTJCHANGEOBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

gbch-jdel:	$(BTJDELOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTJDELOBJS) $(LIB) $(LIBNET) $(LIBS)

btjdel_s:	$(BTJDELOBJS) $(LIB)
		$(CC) -o gbch-jdel $(LDFLAGS) $(BTJDELOBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

gbch-jlist:	$(BTJLISTOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTJLISTOBJS) $(LIB) $(LIBNET) $(LIBS)

btjlist_s:	$(BTJLISTOBJS) $(LIB)
		$(CC) -o gbch-jlist $(LDFLAGS) $(BTJLISTOBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

gbch-jstat:	$(BTJSTATOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTJSTATOBJS) $(LIB) $(LIBNET) $(LIBS)

btjstat_s:	$(BTJSTATOBJS) $(LIB)
		$(CC) -o gbch-jstat $(LDFLAGS) $(BTJSTATOBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

gbch-q:		$(BTQOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTQOBJS) $(LIB) $(LIBCURSES) $(LIBNET) $(LIBS)

btq_s:		$(BTQOBJS) $(LIB)
		$(CC) -o gbch-q $(LDFLAGS) $(BTQOBJS) $(SHLIB) $(SHLIBCLIENT) $(SHLIBCURS) $(LIBCURSES) $(LIBNET) $(LIBS)
		touch $@

gbch-quit:		$(BTQUITOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTQUITOBJS) $(LIB) $(LIBNET) $(LIBS)

btquit_s:		$(BTQUITOBJS) $(LIB)
		$(CC) -o gbch-quit $(LDFLAGS) $(BTQUITOBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

gbch-r:		$(BTROBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTROBJS) $(LIB) $(LIBNET) $(LIBS)

btr_s:		$(BTROBJS) $(LIB)
		$(CC) -o gbch-r $(LDFLAGS) $(BTROBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

gbch-rr:	$(RBTROBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(RBTROBJS) $(LIB) $(LIBNET) $(LIBS)

rbtr_s:		$(RBTROBJS) $(LIB)
		$(CC) -o gbch-rr $(LDFLAGS) $(RBTROBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

gbch-start:	$(BTSTARTOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTSTARTOBJS) $(LIB) $(LIBNET) $(LIBS)

btstart_s:	$(BTSTARTOBJS) $(LIB)
		$(CC) -o gbch-start $(LDFLAGS) $(BTSTARTOBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

gbch-uchange:	$(BTUCHANGEOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTUCHANGEOBJS) $(LIB) $(LIBNET) $(LIBS)

btuchange_s:	$(BTUCHANGEOBJS) $(LIB)
		$(CC) -o gbch-uchange $(LDFLAGS) $(BTUCHANGEOBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

gbch-ulist:	$(BTULISTOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTULISTOBJS) $(LIB) $(LIBNET) $(LIBS)

btulist_s:	$(BTULISTOBJS) $(LIB)
		$(CC) -o gbch-ulist $(LDFLAGS) $(BTULISTOBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

gbch-user:	$(BTUSEROBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTUSEROBJS) $(LIB) $(LIBCURSES) $(LIBNET) $(LIBS)

btuser_s:	$(BTUSEROBJS) $(LIB)
		$(CC) -o gbch-user $(LDFLAGS) $(BTUSEROBJS) $(SHLIB) $(SHLIBCLIENT) $(SHLIBCURS) $(LIBCURSES) $(LIBNET) $(LIBS)
		touch $@

gbch-var:	$(BTVAROBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTVAROBJS) $(LIB) $(LIBNET) $(LIBS)

btvar_s:	$(BTVAROBJS) $(LIB)
		$(CC) -o gbch-var $(LDFLAGS) $(BTVAROBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

gbch-vlist:	$(BTVLISTOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTVLISTOBJS) $(LIB) $(LIBNET) $(LIBS)

btvlist_s:	$(BTVLISTOBJS) $(LIB)
		$(CC) -o gbch-vlist $(LDFLAGS) $(BTVLISTOBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

gbch-filemon:	$(BTFILEMONOBJS)
		$(CC) -o $@ $(LDFLAGS) $(BTFILEMONOBJS) $(LIB) $(LIBNET) $(LIBS)

btfilemon_s:	$(BTFILEMONOBJS)
		$(CC) -o gbch-filemon $(LDFLAGS) $(BTFILEMONOBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

# The rest are all internal programs so they don't need to have gbch- stuck in fron

btsched:	$(BTSCHEDOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTSCHEDOBJS) $(LIB) $(LIBNET) $(LIBS)

btsched_s:	$(BTSCHEDOBJS) $(LIB)
		$(CC) -o btsched $(LDFLAGS) $(BTSCHEDOBJS) $(SHLIB) $(LIBNET) $(LIBS)
		touch $@

# We don't do a shared library version of btexec because it doesn't use any library routines (apart from versionprint)

btexec:		$(BTEXECOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTEXECOBJS) $(LIB)

# Ditto btpwchk

btpwchk:	$(BTPWCHKOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTPWCHKOBJS) $(LIB) $(LIBNET) @LIBSHAD@ @CRYPTLIB@ $(LIBS)

# Ditto btmdisp - uses some but not enough to be worth including all libs

btmdisp:	$(BTMDISPOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTMDISPOBJS) $(LIB) $(LIBNET) $(LIBS)

xbnetserv:	$(XBNETSERVOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(XBNETSERVOBJS) $(LIB) $(LIBNET) $(LIBS)

xbnetserv_s:	$(XBNETSERVOBJS) $(LIB)
		$(CC) -o xbnetserv $(LDFLAGS) $(XBNETSERVOBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

jobdump:	$(JOBDUMPOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(JOBDUMPOBJS) $(LIB) $(LIBNET) $(LIBS)

jobdump_s:	$(JOBDUMPOBJS) $(LIB)
		$(CC) -o jobdump $(LDFLAGS) $(JOBDUMPOBJS) $(SHLIB) $(SHLIBCLIENT) $(LIBNET) $(LIBS)
		touch $@

btwrite:	$(BTWRITEOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(BTWRITEOBJS) $(LIB) $(LIBNET) $(LIBS)

btwrite_s:	$(BTWRITEOBJS) $(LIB)
		$(CC) -o btwrite $(LDFLAGS) $(BTWRITEOBJS) $(SHLIB) $(LIBNET) $(LIBS)
		touch $@

dosbtwrite:	$(DOSBTWRITEOBJS) $(LIB)
		$(CC) -o $@ $(LDFLAGS) $(DOSBTWRITEOBJS) $(LIB) $(LIBNET) $(LIBS)

dosbtwrite_s:	$(DOSBTWRITEOBJS) $(LIB)
		$(CC) -o dosbtwrite $(LDFLAGS) $(DOSBTWRITEOBJS) $(SHLIB) $(LIBNET) $(LIBS)
		touch $@

apilibd:
		cd apilib;$(MAKE)
		touch $@

# Commenting out Motif stuff
# This should work if you have a Motif Library and #includes but Motif is not supported by GNU
#motifd:$(LIB)
#	cd motif;$(MAKE)

gtkd:		$(LIB)
		-cd gtk;$(MAKE)

loccgid:$(LIB)
		cd loccgi;$(MAKE)

remcgid:$(LIB)
		cd remcgi;$(MAKE)

insd:		$(LIB)
		cd ins;$(MAKE)

$(LIB):
		cd lib;$(MAKE)

##################################################################################
#
#		INSTALL
#
#	NB we need to have user "$(BATCHUSER)" set up
#	Also need to set up initial host file
#
##################################################################################

install: all
	@perlbin@ $(BASE)/Checkuserinst $(BATCHUSER) @spooldir@
	$(INSTALL) -d -o $(BATCHUSER) -g root -m 755 @spooldir@
	$(INSTALL) -d -o $(BATCHUSER) -g root -m 755 @pkgexecdir@
	$(INSTALL) -d -o $(BATCHUSER) -g root -m 755 @sphelpdir@
	cd lib;$(MAKE) install prefix="$(prefix)" exec_prefix="$(exec_prefix)"
	$(INSTALL) $(USERMODES) $(USERBINS) $(BINDIR)
	$(INSTALL) $(NOSETMODES) $(NOSETUBINS) $(BINDIR)
	cd $(BINDIR);for p in $(BTSTARTLNKS); do ln -f gbch-start $$p; done
	cd $(BINDIR);for p in $(BTJDELLNKS); do ln -f gbch-jdel $$p; done
	$(INSTALL) $(SUIDROOT) $(SUIDROOTBINS) $(INTBINDIR)
	$(INSTALL) $(USERMODES) $(SUIDBTBINS) $(INTBINDIR)
	$(INSTALL) $(TTYGROUP) $(TTYGRPBINS) $(INTBINDIR)
	cd helpmsg;$(MAKE) install prefix="$(prefix)" exec_prefix="$(exec_prefix)"
	cd loccgi;$(MAKE) install prefix="$(prefix)" exec_prefix="$(exec_prefix)"
	cd ins;$(MAKE) install prefix="$(prefix)" exec_prefix="$(exec_prefix)"
	cd apilib;$(MAKE) install prefix="$(prefix)" exec_prefix="$(exec_prefix)"
	cd remcgi;$(MAKE) install prefix="$(prefix)" exec_prefix="$(exec_prefix)"
	-cd gtk;$(MAKE) install prefix="$(prefix)" exec_prefix="$(exec_prefix)"
##	-cd motif;$(MAKE) install prefix="$(prefix)" exec_prefix="$(exec_prefix)"
	@perlbin@ $(BASE)/Checknetinst

install-strip:
	@perlbin@ $(BASE)/Checkuserinst $(BATCHUSER) @spooldir@
	$(INSTALL) -d -o $(BATCHUSER) -g root -m 755 @spooldir@
	$(INSTALL) -d -o $(BATCHUSER) -g root -m 755 @pkgexecdir@
	$(INSTALL) -d -o $(BATCHUSER) -g root -m 755 @sphelpdir@
	cd lib;$(MAKE) install prefix="$(prefix)" exec_prefix="$(exec_prefix)"
	$(INSTALL) -s $(USERMODES) $(USERBINS) $(BINDIR)
	$(INSTALL) -s $(NOSETMODES) $(NOSETUBINS) $(BINDIR)
	cd $(BINDIR);for p in $(BTSTARTLNKS); do ln -f gbch-start $$p; done
	cd $(BINDIR);for p in $(BTJDELLNKS); do ln -f gbch-jdel $$p; done
	$(INSTALL) -s $(SUIDROOT) $(SUIDROOTBINS) $(INTBINDIR)
	$(INSTALL) -s $(USERMODES) $(SUIDBTBINS) $(INTBINDIR)
	$(INSTALL) -s $(TTYGROUP) $(TTYGRPBINS) $(INTBINDIR)
	cd helpmsg;$(MAKE) install-strip prefix="$(prefix)" exec_prefix="$(exec_prefix)"
	cd loccgi;$(MAKE) install-strip prefix="$(prefix)" exec_prefix="$(exec_prefix)"
	cd ins;$(MAKE) install-strip prefix="$(prefix)" exec_prefix="$(exec_prefix)"
	cd apilib;$(MAKE) install-strip prefix="$(prefix)" exec_prefix="$(exec_prefix)"
	cd remcgi;$(MAKE) install-strip prefix="$(prefix)" exec_prefix="$(exec_prefix)"
	-cd gtk;$(MAKE) install-strip prefix="$(prefix)" exec_prefix="$(exec_prefix)"
##	-cd motif;$(MAKE) install-strip prefix="$(prefix)" exec_prefix="$(exec_prefix)"
	@perlbin@ $(BASE)/Checknetinst

#
#	Clean-ups.
#

clean:
	$(RM) $(USERBINS) $(NOSETUBINS) $(SUIDROOTBINS) $(SUIDBTBINS) $(TTYGRPBINS) *.o *_s
	cd lib;$(MAKE) clean
	cd apilib;$(MAKE) clean
	cd loccgi;$(MAKE) clean
	cd remcgi;$(MAKE) clean
	cd ins;$(MAKE) clean
	cd helpmsg;$(MAKE) clean
	-cd motif;$(MAKE) clean
	-cd gtk;$(MAKE) clean

distclean:	clean
	cd lib;$(MAKE) distclean
	cd apilib;$(MAKE) distclean
	cd loccgi;$(MAKE) distclean
	cd remcgi;$(MAKE) distclean
	cd ins;$(MAKE) distclean
	cd helpmsg;$(MAKE) distclean
	-cd motif;$(MAKE) distclean
	-cd gtk;$(MAKE) distclean
	$(RM) Makefile

reallyclean:	distclean
	$(RM) TAGS
