#~~syntax:makefile
#==============================================================================
# ttyrpld - TTY replay daemon
#   Copyright © Jan Engelhardt <jengelh [at] linux01 gwdg de>, 2004
#   -- License restrictions apply (GPL2)
#
#   This file is part of ttyrpld.
#   ttyrpld 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; however only version 2 of the License.
#
#   ttyrpld 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 kit; if not, write to:
#   Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
#   02111-1307, USA.
#
#   -- For details see doc/GPL2.txt.
#==============================================================================
DEBUG    := 0

### Programs ------------------------------------------------------------------
#
CC       := gcc
CFLAGS   := -D_REENTRANT -Iinclude -Wall -Waggregate-return \
            -Wmissing-declarations -Wmissing-prototypes -Wredundant-decls \
            -Wshadow -Wstrict-prototypes -Winline -pipe
LD       := gcc # ld
LDFLAGS  := -L. -Wl,-rpath,.

INST     := install
LN       := ln
RM       := rm

ifeq (${DEBUG},1)
  CFLAGS += -ggdb3
  STRIP  := @true
else
  CFLAGS += -O2 -finline-functions -fomit-frame-pointer
  STRIP  := strip -s
endif
ifeq (${PROF},1)
  CFLAGS += -pg -fprofile-arcs -ftest-coverage
  STRIP  := @true
endif

CFLAGS  += ${EXT_CFLAGS}
LDFLAGS += ${EXT_LDFLAGS}

ifeq (${VERBOSE},)
  CC    := @${CC}
  LD    := @${LD}
  INST  := @${INST}
  LN    := @${LN}
  RM    := @${RM}
  STRIP := @${STRIP}

  VECHO_CC  = @echo "  CC    " $@
  VECHO_LD  = @echo "  LD    " $@
  VECHO_DEP = @echo "  DEP   " $@
endif

#------------------------------------------------------------------------------
.PHONY: all kmod clean cleanx

all: rpld rplctl ttyreplay;

KERNEL_VER := $(shell uname -r | cut -d. -f1-2)
kmod:
	@echo "Compiling Kernel module...";
ifneq (${KERNEL_VER},2.6)
	@echo "*** Kernel versions other than 2.6.x are not supported.";
	@false;
else
	make -C kernel-2.6 install;
endif
	depmod -a;
	lsmod | grep ^rpldev &>/dev/null && rmmod rpldev &>/dev/null || :;
	modprobe rpldev;
	@echo "*** Kernel module compiled, installed and loaded. Either edit";
	@echo "*** your system configuration files so that it is also loaded";
	@echo "*** upon next bootup. You can leave it as it is if you use";
	@echo "*** etc-init.d-rpld, since it will try to load it anyway.";

ttyreplay: user/replay.o user/pctrl.o user/global.o;
	${VECHO_LD}
	${LD} ${LDFLAGS} -o $@ $^ -lHX -lpopt -lpthread;
	${STRIP} $@;

rpld: user/rpld.o user/infod.o user/rplctl.o user/rdsh.o user/global.o;
	${VECHO_LD}
	${LD} ${LDFLAGS} -o $@ $^ -lHX -lpopt -lpthread;
	${STRIP} $@;

rplctl: rpld;
	${LN} -fs $< $@;

user/%.o: user/%.c;
	${VECHO_CC}
	${CC} -D_REENTRANT ${CFLAGS} -c -o $@ $<;

install: ttyreplay rpld rplctl;
	@echo "Installing to /usr/local/{s,}bin and /etc"
	${INST} -oroot -groot -pm0755 ttyreplay /usr/local/bin/;
	${INST} -oroot -groot -pm0755 rpld /usr/local/sbin/;
	${LN} -fs rpld /usr/local/sbin/rplctl;
	${INST} -oroot -groot -pm0644 rpld.conf /etc/;
	@mkdir -p /etc/init.d;
	${INST} -oroot -groot -pm0644 etc-init.d-rpld /etc/init.d/;

uninstall:
	${RM} -f /usr/local/bin/ttyreplay /usr/local/sbin/{rpld,rplctl};
	${RM} -f /etc/rpld.conf;

clean: ;
	${RM} -f user/*.o rpld rplctl ttyreplay;

cleanx: clean;
	make -C kernel-2.6 clean;
	${RM} -f user/*.d;

#--( C/H Dependencies )--------------------------------------------------------
Makefile: $(patsubst %.c,%.d,$(wildcard user/*.c))

user/%.d: user/%.c;
	${VECHO_DEP}
	${CC} ${CFLAGS} -MM -MF $@ -MT $(@:.d=.o) $<;

_DEP_FILES := $(wildcard user/*.d)
ifneq (${_DEP_FILES},)
-include ${_DEP_FILES}
endif

#==[ End of file ]=============================================================
