LIBES = -laa

ifdef DEFS
LIBES += -lbasis_g
else
LIBES += -lbasis
endif

include ../Config

ifeq ("${WITH_PANEL}","yes")
PANEL_LIB  = -lpanel
PANEL_FLAGS = -DWITH_PANEL=1
endif

ifeq ("${WITH_NCURSES}","yes")
LIBES  += -lndialog $(PANEL_LIB) -lncurses
CFLAGS += $(PANEL_FLAGS) -DWITH_NCURSES=1
else
LIBES  += -lndcurses ${PANEL_LIB} -lcurses -ltermcap
CFLAGS += ${PANEL_FLAGS}
endif

CXXFLAGS=$(CFLAGS)

CXX = $(CC)
CFLAGS += $(DEFS) -DDIALOG_CHAR=char -I../include -I. #-Wall
CXXFLAGS = $(CFLAGS)
LDFLAGS = $(DEFS) -L../lib -L.

ifdef EFENCE
LIBES += -lefence
endif

COMMON=libaa.a
COMMONOBJ=messages.o rootmenu.o passwordcheck.o getrc.o \
          lib/streq.o lib/END.o lib/CLEAR.o lib/util.o lib/safecopy.o

MODS=bin/sysconfig bin/mouse bin/resolver bin/yellowpages \
     bin/timezone bin/network bin/services bin/user bin/aliases bin/date \
     bin/ntp
TOOLS=bin/chkconfig bin/ifadmin
PRGS=bin/admintool bin/admintoolx bin/stage2 \
	bin/xf86config bin/Xsetup bin/ifadmin \
	bin/chkconfig
SUBDIRS=helpers
STAGE2=S2/sysconfig.o S2/mouse.o S2/resolver.o S2/yellowpages.o \
       S2/network.o S2/timezone.o # S2/date.o S2/ntp.o
XFOBJ=config/Card.o config/Monitor.o config/Mouse.o config/KeyBoard.o \
      config/Display.o config/VideoCard.o config/Geometry.o
BIN=S2 bin Admin

all: $(BIN) $(COMMON) $(MODS) $(PRGS)
	for x in $(SUBDIRS); do \
	    $(MAKE) -C $$x all; \
	done

$(BIN):
	-mkdir -p $@
	
$(COMMON): $(COMMONOBJ)
	rm -f $(COMMON)
	ar crv $(COMMON) $(COMMONOBJ)
	ranlib $(COMMON)

bin/stage2: stage2.o $(STAGE2) $(COMMON)
	$(CC) $(LDFLAGS) -static -o bin/stage2 stage2.o $(STAGE2) $(LIBES)

stage2.o: stage2.cc $(HEADERS)
	$(CXX) $(CXXFLAGS) -DSTAGE2=1 -o stage2.o -c stage2.cc

bin/admintoolx: $(COMMON) admintool.c $(HEADERS)
	$(CC) $(CFLAGS) $(LDFLAGS) -DWITHX -o bin/admintoolx admintool.c $(LIBES)

bin/Xsetup: S2/xf86config.o $(XFOBJ) $(HEADERS)
	$(CC) $(LDFLAGS) -static -o bin/Xsetup S2/xf86config.o $(XFOBJ) $(LIBES)

bin/xf86config: Admin/xf86config.o $(XFOBJ) $(HEADERS)
	$(CC) $(LDFLAGS) -o bin/xf86config Admin/xf86config.o $(XFOBJ) $(LIBES)

S2/%.o: %.cc
	$(CXX) $(CFLAGS) -DSTAGE2=1 -o S2/$*.o -c $*.cc

S2/%.o: %.c
	$(CC) $(CFLAGS) -DSTAGE2=1 -o S2/$*.o -c $*.c

bin/%: Admin/%.o
	$(CC) $(LDFLAGS) -o $@ Admin/$*.o $(LIBES)

config/%.o: config/%.cc
	$(CXX) $(CFLAGS) -c -o $@ config/$*.cc

Admin/%.o: %.cc
	$(CXX) $(CFLAGS) -c -o $@ $*.cc

Admin/%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $*.c

install: $(PRGS) $(MODS) $(TOOLS)
	install -m 755 -d $(DESTDIR)/sbin
	install -m 711 -o bin -g bin -c -s bin/admintool $(DESTDIR)/sbin
	install -m 755 -d $(DESTDIR)/usr/lib/admin/doc
	install -m 711 -o bin -g bin -c -s $(MODS) $(DESTDIR)/usr/lib/admin
	install -m 444 -o bin -g bin -c doc/*html $(DESTDIR)/usr/lib/admin/doc
	install -m 711 -o bin -g bin -c -s $(TOOLS) $(DESTDIR)/sbin
	install -o man -g man -d $(DESTDIR)/usr/man/man1
	install -o man -g man -m 444 -c *.1 $(DESTDIR)/usr/man/man1
	for x in $(SUBDIRS); do \
	    $(MAKE) -C $$x install; \
	done


Xinstall: $(PRGS)
	install -m 755 -d $(DESTDIR)/sbin
	install -m 511 -o bin -g bin -c -s bin/admintoolx \
			  $(DESTDIR)/sbin/admintool

clean:
	rm -rf *.o $(BIN) lib/*.o config/*.o S2/*.o
	for x in $(SUBDIRS); do \
	    $(MAKE) -C $$x clean; \
	done

tidy:
	rm -f $(PRGS)

config/Cards.o: config/Card.cc Card.h
config/Monitors.o: config/Monitor.cc Monitor.h
Admin/xf86config.o: xf86config.cc xf86config.h Card.h Monitor.h
S2/date.o: date.c
S2/mouse.o: mouse.cc
S2/network.o: network.cc buttons.h util.h
S2/ntp.o: ntp.cc buttons.h util.h
S2/resolver.o: resolver.cc buttons.h util.h
S2/sysconfig.o: sysconfig.cc buttons.h util.h
S2/timezone.o: timezone.c buttons.h
S2/xf86config.o: xf86config.cc xf86config.h Card.h Monitor.h
S2/yellowpages.o: yellowpages.cc buttons.h
admintool.o: admintool.c admin.h
aliases.o: aliases.cc
chkconfig.o: chkconfig.c
config/VideoCard.o: config/VideoCard.cc xf86config.h Card.h Monitor.h \
		    buildmenu.h
config/KeyBoard.o: config/KeyBoard.cc xf86config.h Card.h Monitor.h
config/Display.o: config/Display.cc xf86config.h Card.h Monitor.h buildmenu.h
config/mouse.o: config/mouse.cc xf86config.h Card.h Monitor.h
config/geometry.o: config/geometry.cc xf86config.h Card.h
getrc.o: getrc.c getrc.h
glue.o: glue.c
hosts.o: hosts.cc
ifadmin.o: ifadmin.cc
messages.o: messages.c
passwordcheck.o: passwordcheck.c
rootmenu.o: rootmenu.c admin.h
services.o: services.cc getrc.h
stage2.o: stage2.cc admin.h buttons.h util.h
user.o: user.cc util.h
lib/util.o: lib/util.c util.h
lib/CLEAR.o: lib/CLEAR.c util.h
lib/END.o: lib/END.c util.h
lib/safecopy.o: lib/safecopy.c util.h
lib/streq.o: lib/streq.c util.h
