#~~syntax:makefile
#==============================================================================
# ttyrpld - TTY replay daemon
#   Copyright © Jan Engelhardt <jengelh [at] linux01 gwdg de>, 2004 - 2005
#   -- License restrictions apply (GPL v2)
#
#   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 the COPYING file
#==============================================================================
DEBUG    := 0

## OS specific ----------------------------------------------------------------
#
KERNEL_OS  := $(shell uname -s)
KERNEL_VER := $(shell uname -r | cut -d- -f1 | cut -d. -f1-2)

### Programs ------------------------------------------------------------------
#
CC      := gcc
CFLAGS  := -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_REENTRANT -Iinclude \
           -Wall -Waggregate-return -Wmissing-declarations \
           -Wmissing-prototypes -Wredundant-decls -Wshadow \
           -Wstrict-prototypes -Winline -pipe
LD      := gcc # ld
LDFLAGS :=

INST    := install # no @ variant
LN      := ln
-include .Makefile.std.${KERNEL_OS}

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

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

ifeq (${V},)
  CC    := @${CC}
  LD    := @${LD}
  LN    := @${LN}
  STRIP := @${STRIP}

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

V_KMOD_SUCCESS := \
  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.";

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

all: rpld rplctl ttyreplay locale;

-include .Makefile.kmod.${KERNEL_OS}

locale: ;
	${MAKE} -C locale;

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} ${CFLAGS} -c -o $@ $<;

install: ttyreplay rpld rplctl locale;
	@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;
	[ ! -e /etc/rpld.conf ] && ${INST} -oroot -groot -pm0644 rpld.conf /etc/; true;
ifeq (${KERNEL_OS},Linux)
	${INST} -oroot -groot -pm0755 etc-init.d-rpld /etc/init.d/rpld;
endif
	make -C locale install;

uninstall:
	rm -f /usr/local/bin/ttyreplay /usr/local/sbin/{rpld,rplctl};
	rm -f /etc/rpld.conf;
	rm -f /usr/local/share/locale/*/LC_MESSAGES/ttyrpld.mo;

clean: ;
	rm -f user/*.o rpld rplctl ttyreplay;

cleanx: clean;
	rm -f user/*.d;
	${MAKE} -C locale clean;

#--( 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 ]=============================================================
