# Makefile -- Makefile for simpleinit-msb utilities
# Created: Sat Dec 26 20:09:40 1992
# Revised: Sun Nov 10 20:28:43 1996 by faith@cs.unc.edu
# Copyright 1992, 1993, 1994, 1995 Rickard E. Faith (faith@cs.unc.edu)
#
# butchered 5 Sep 2001 Matthias S. Benkmann <m.s.b@gmx.net>
#
# Suggested changes from Bauke Jan Douma <bjdouma@xs4all.nl> have been
# implemented to handle shadow and sysvinit systems 

include ../make_include
include ../mconfig

# Where to put man pages?

MAN1.MISC=	

MAN1.PUTILS= 	
MAN1.PASSWD=    

MAN8.GETTY=	

MAN8.INIT=	fastboot.8 fasthalt.8 halt.8 reboot.8 simpleinit.8 shutdown.8 \
		initctl.8 telinit.8

MAN5.INIT=	inittab.5

# Where to put binaries?
# See the "install" rule for the links. . .

SBIN.INIT=	simpleinit shutdown initctl telinit

#ifeq "$(NEED_LIBCRYPT)" "yes"
#CRYPT=-lcrypt
#endif

#ifeq "$(HAVE_PAM)" "yes"
#PAM=-lpam -ldl -lpam_misc
#PAMFL=-DUSE_PAM=1
#endif

ifeq "$(HAVE_SYSVINIT)" "no"
WHAT_TO_BUILD:=$(WHAT_TO_BUILD) all-init
WHAT_TO_INSTALL:=$(WHAT_TO_INSTALL) install-init
endif

all: $(WHAT_TO_BUILD)
all-init: $(SBIN.INIT)

debug:
	$(MAKE) OPT=-g LDFLAGS= all

# Rules for everything else

simpleinit.o: simpleinit.h simpleinit.c  $(LIB)/pathnames.h \
	      $(LIB)/linux_reboot.h
initctl.o: simpleinit.h initctl.c $(LIB)/pathnames.h
shutdown.o: $(LIB)/pathnames.h $(LIB)/linux_reboot.h shutdown.c

shutdown: shutdown.o $(LIB)/my_reboot.o $(LIB)/xstrncpy.o

simpleinit: simpleinit.o $(LIB)/my_reboot.o $(LIB)/xstrncpy.o
	$(CC) $(LDFLAGS) -o $@ $^      #$(CRYPT)

telinit:
	ln -f telinit.sh telinit
	chmod +x telinit

initctl: initctl.o
	$(CC) $(LDFLAGS) -o $@ $^

install: all $(WHAT_TO_INSTALL)

install-init: $(SBIN.INIT)
	if [ ! -e $(SBINDIR) ]; then $(INSTALLDIR) $(SBINDIR); fi
	if [ ! -e $(MAN8DIR) ]; then $(INSTALLDIR) $(MAN8DIR) ; fi
	if [ ! -e $(MAN5DIR) ]; then $(INSTALLDIR) $(MAN5DIR) ; fi

	$(INSTALLBIN) $(SBIN.INIT) $(SBINDIR)	
	$(INSTALLMAN) $(MAN8.INIT) $(MAN8DIR)
	$(INSTALLMAN) $(MAN5.INIT) $(MAN5DIR)
	
	(cd $(SHUTDOWNDIR); ln -sf shutdown reboot)
	(cd $(SHUTDOWNDIR); ln -sf shutdown fastboot)
	(cd $(SHUTDOWNDIR); ln -sf shutdown halt)
	(cd $(SHUTDOWNDIR); ln -sf shutdown fasthalt)
	(cd $(SHUTDOWNDIR); ln -sf initctl need)
	(cd $(SHUTDOWNDIR); ln -sf initctl display-services)
	(cd $(SHUTDOWNDIR); ln -sf initctl provide)
	(cd $(SHUTDOWNDIR); ln -sf simpleinit init)
	(cd $(MAN8DIR);     ln -sf initctl.8 need.8)
	(cd $(MAN8DIR);     ln -sf initctl.8 display-services.8)
	(cd $(MAN8DIR);     ln -sf initctl.8 provide.8)
	(cd $(MAN8DIR);     ln -sf simpleinit.8 init.8)

	$(MAKE) installfifo

.PHONY: installfifo
installfifo:
	@if [ $$UID != 0 ]; then \
	  echo 1>&2 $$'\n\n************************************************' ;\
	  echo 1>&2 $$'WARNING! You are not installing as root!' ; \
	  echo 1>&2 $$'A FIFO '$(DEVDIR)/initctl 'with mode 600 is required' ;\
	  echo 1>&2 $$'for proper operation of init!' ; \
	  echo 1>&2 $$'Run `make installfifo\' as root to create it.' ; \
	  echo 1>&2 $$'************************************************\n\n' ;\
	else \
	  if [ ! -e $(DEVDIR) ]; then $(INSTALLDIR) $(DEVDIR) ; fi && \
	  if [ ! -p $(DEVDIR)/initctl ]; then \
	    echo rm -f $(DEVDIR)/initctl; \
	    rm -f $(DEVDIR)/initctl; \
	    echo mkfifo $(DEVDIR)/initctl; \
	    mkfifo $(DEVDIR)/initctl; \
	  fi && \
	  { echo chown root.root $(DEVDIR)/initctl ; \
	  chown root.root $(DEVDIR)/initctl ;} && \
	  { echo chmod 600 $(DEVDIR)/initctl ; \
	  chmod 600 $(DEVDIR)/initctl ;} ; \
	fi

.PHONY: clean distclean
clean:
	-rm -f *.o core $(SBIN.INIT)
	-rm -f *~

distclean: clean
	
