#
# Sloggi - A Solaris Uname Jacket - http://www.roqe.org/sloggi
# Copyright 2002,2003 Konrad Rieck <kr@roqe.org> - All rights reserved. 
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright   
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright   
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR     
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY   
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

#
# Directories for installation, don't change the KERNELDIR and SYSCONFDIR
# unless you know what you are doing.

#
# this make file is for x86/AMD64
#

BINDIR=/usr/sbin
KERNELDIR=/usr/kernel/misc
SYSCONFDIR=/etc

# Sun C Compiler setup
#CC=cc
#CFLAGS32=-xarch=generic
#CFLAGS64=-D_64BIT -xarch=generic64

# GCC version 3.x setup
CC=gcc
CFLAGS32=-m32 -ffreestanding  -mno-red-zone
CFLAGS64=-D_64BIT -m64 -ffreestanding -mcmodel=kernel -mno-red-zone

# Options for both compilers! 
COPTS=-DSVR4 -DSOL2 

# Uncomment to enable debugging. Not recommanded for non-developers, may blast
# your syslog.
# COPTS += -DDEBUG

# Uncomment the following line, if you are using GNU C Compiler and want to
# see compiler warnings.
# COPTS += -Wall

LD=/usr/ccs/bin/ld
LDFLAGS=-o

#
# You are finished ... don't change anything below. Okay? Yes, read through
# the rest, you are right. No information hiding, this is not a Microsoft
# release.
#

RM=/usr/bin/rm
LN=/usr/bin/ln
INSTALL=/usr/ucb/install

INITSCRIPT=$(SYSCONFDIR)/init.d/sloggi
RC2SCRIPT=$(SYSCONFDIR)/rc2.d/S02sloggi
SLUGCTLPATH=$(BINDIR)/$(SLUGCTL)

MODSCRIPT=init.sloggi

# Do both GNU make and Solaris make style exec
VERSION:sh=cat ../VERSION
VERSION+= $(shell cat ../VERSION)
BITS:sh=/usr/bin/isainfo -b
BITS+= $(shell /usr/bin/isainfo -b)

#SOL_REL:sh=/usr/bin/uname -r | cut -f2 -d.
#SOL_REL+= $(shell /usr/bin/uname -r | cut -f2 -d.)
SOL_REL=10

PACKAGE=sloggi-$(VERSION)
MODULE=sloggi
SLUGCTL=slogctl

C_FILES=sloggi.c refcnt.c ujacket.c utils.c
H_FILES=sloggi.h refcnt.h ujacket.h utils.h
M_FILES=Makefile
MODULE32=$(MODULE)
O_FILES32=sloggi.o refcnt.o ujacket.o utils.o

KERNELDIR64=$(KERNELDIR)/amd64
MODULE64=amd64/$(MODULE)
O_FILES64=amd64/sloggi.o amd64/refcnt.o amd64/ujacket.o amd64/utils.o

CFLAGS=$(COPTS) -DVERSION=\"$(VERSION)\" -DSOL_REL=$(SOL_REL)

all:		amd64 $(MODULE32) $(MODULE$(BITS)) $(SLUGCTL)

amd64:	
		mkdir amd64

%.o:		%.c
		$(CC) -D_KERNEL $(CFLAGS) $(CFLAGS32) -c -o $@ $<

amd64/%.o:	%.c amd64
		$(CC) -D_KERNEL $(CFLAGS) $(CFLAGS64) -c -o $@ $<

$(MODULE32):	$(O_FILES32)
		$(LD) $(LDFLAGS) $@ -r $(O_FILES32)
		sync

$(MODULE64):	$(O_FILES64)
		$(LD) $(LDFLAGS) $@ -r $(O_FILES64)
		sync

$(SLUGCTL):	slogctl.c slogctl.h
		$(CC) $(CFLAGS) -o $@ slogctl.c	

install:	$(MODULE32) $(MODULE$(BITS)) $(SLUGCTL) $(MODSCRIPT)
		$(INSTALL) -g bin -d $(BINDIR)
		$(INSTALL) -g bin -m 755 $(SLUGCTL) $(SLUGCTLPATH)
		$(INSTALL) -g sys -d $(KERNELDIR)
		$(INSTALL) -g sys -m 755 $(MODULE32) $(KERNELDIR)/$(MODULE)
		if test "$(BITS)" = 64 ; then \
			$(INSTALL) -g sys -m 755 $(MODULE64) \
			$(KERNELDIR64)/$(MODULE) ; \
		fi
		$(INSTALL) -g sys -m 755 $(MODSCRIPT) $(INITSCRIPT)
		$(RM) -f $(RC2SCRIPT)
		$(LN) -f $(INITSCRIPT) $(RC2SCRIPT)

uninstall:	$(INITSCRIPT) $(RC2SCRIPT)
		$(RM) -f $(INITSCRIPT) $(RC2SCRIPT)
		$(RM) -f $(SLUGCTLPATH) $(KERNELDIR)/$(MODULE)
		if test "$(BITS)" = 64 ; then \
			$(RM) -f $(KERNELDIR64)/$(MODULE) ; \
		fi

clean:
		rm -f $(MODULE32) $(MODULE64) $(SLUGCTL)
		rm -f $(O_FILES32) $(O_FILES64)
		rm -f *~ core
		rm -rf amd64

INDENT=indent
beautify: 
		$(INDENT) -kr -l77 -lc77 $(C_FILES)
		$(INDENT) -kr -l77 -lc77 $(H_FILES)
		$(INDENT) -kr -l77 -lc77 slogctl.c slogctl.h
		rm -f *~		
