#!/bin/make -f
# @(#)util/Makefile	1.20 9/20/92 16:54:43
# Makefile for the smail utility tools
#
#    Copyright (C) 1987, 1988 Ronald S. Karr and Landon Curt Noll
#    Copyright (C) 1992  Ronald S. Karr
# 
# See the file COPYING, distributed with smail, for restriction
# and warranty information.

SHELL=/bin/sh
MAKE=make
GET=sccs get
AR=ar
LINT=lint
CLEAN=sccs clean
MK=${MAKE} -${MAKEFLAGS} SHELL=${SHELL}
SRC_PREFIX=
MKDEPEND=${ROOT}/conf/lib/mkdepend.sh
MKDEFS=${ROOT}/conf/lib/mkdefs.sh
CHECKDEFS=${ROOT}/conf/lib/checkdefs.sh
MKDIRS=${ROOT}/conf/lib/mkdirs.sh
INSTM=${ROOT}/conf/lib/instm.sh
INST=${ROOT}/conf/lib/inst.sh
XEXEC=${SHELL} ${ROOT}/conf/lib/xexec.sh
DEFS_SH=defs.sh
DEFS_H=defs.h
DEFS_SED=defs.sed

ROOT=..
SMAILSRC=${ROOT}/src
UTILSRC=${ROOT}/util
INCLUDES=-I${SMAILSRC} $$DBM_INCLUDES $$INCLUDES
REMOTE_SRC=${SMAILSRC}/field.c ${SMAILSRC}/parse.c \
	   ${SMAILSRC}/ascii.c ${SMAILSRC}/string.c
LOCAL_SRC=field.c parse.c ascii.c string.c
LOCAL_OBJ=field.o parse.o ascii.o string.o
UTIL_TARGETS=mkline mksort dcasehost mkdbm mkpath \
	     pathmerge checkerr getmap gleem unsharmap savelog \
	     mkuuwho mkhpath
BIN_TARGETS=mkaliases
TARGETS=${UTIL_TARGETS} ${BIN_TARGETS}
STRIP_TARGETS=mkline mksort dcasehost mkdbm pathmerge gleem unsharmap
OBJ=mkline.o mksort.o dcasehost.o mkdbm.o pathmerge.o \
    gleem.o unsharmap.o ${LOCAL_OBJ}
CSRC=mkline.c mksort.c dcasehost.c mkdbm.c pathmerge.c gleem.c unsharmap.c
HSRC=dbm_compat.h
SHSRC=mkpath.sh checkerr.sh getmap.sh mkaliases.sh \
      savelog.sh mkuuwho.sh mkhpath.sh
AWK_SRC=mkpath.awk mkuuwho.awk
MISCSRC=Makefile
SRC=${CSRC} ${HSRC} ${SHSRC} ${AWK_SRC} ${MISCSRC}
COMPAT_LIB_DIR=${ROOT}/compat
COMPAT_LIB=${COMPAT_LIB_DIR}/compat.a

.c.o:
	@. ./${DEFS_SH}; ${XEXEC} $$CC $$CFLAGS ${INCLUDES} -c $*.c

all: ${TARGETS}

sources: ${SRC}

${SRC}:
	${GET} $@

${DEFS_H} ${DEFS_SH} ${DEFS_SED}:
	ROOT=${ROOT} ${SHELL} ${MKDEFS}

${COMPAT_LIB}:
	cd ${COMPAT_LIB_DIR}; ${MK}

names:
	@for i in ${SRC}; do echo ${SRC_PREFIX}$$i; done

install: all
	@. ./${DEFS_SH}; \
	   case "$$DONT_INSTALL" in \
	   ?*)	echo Testing ... install ignored; exit 0;; \
	   esac; \
	   l_flag=; \
	   case "$$USE_SYMLINKS" in \
	   ?*)	l_flag=-l;; \
	   esac; \
	   ${XEXEC} ${SHELL} ${MKDIRS} -m 0755 $$UTIL_BIN_DIR $$SMAIL_BIN_DIR \
				      $$LIB_DIR $$LIB_DIR/maps $$TMP_DIR; \
	   case "$$UNSHAR_MAP_DIR" in \
	   ?*)	${XEXEC} ${SHELL} ${MKDIRS} -m 0755 $$UNSHAR_MAP_DIR \
					   $$UNSHAR_MAP_DIR/work;; \
	   esac; \
	   ${XEXEC} ${SHELL} ${INSTM} -m 0555 $$UTIL_BIN_DIR ${UTIL_TARGETS}; \
	   case $$UTIL_BIN_DIR in \
	   "$$SMAIL_BIN_DIR") x=;; \
	   *)	xx=$$SMAIL_BIN_DIR/mkaliases;; \
	   esac; \
	   ${XEXEC} ${SHELL} ${INST} -m 0555 $$l_flag -d $$UTIL_BIN_DIR \
		mkaliases mkaliases $$xx; \
	   ${XEXEC} ${SHELL} ${INSTM} -m 0444 $$UTIL_BIN_DIR ${AWK_SRC}; \
	   case "$$STRIP" in \
	   ?*)	for i in ${STRIP_TARGETS}; do \
			${XEXEC} strip $$UTIL_BIN_DIR/$$i; \
			if [ -x /usr/bin/mcs ]; then \
				${XEXEC} mcs -d $$UTIL_BIN_DIR/$$i; \
			fi; \
		done; \
	   esac

depend local_depend: check_defs
	-@for i in ${LOCAL_SRC}; do \
		if cmp -s $$i ${SMAILSRC}/$$i; then \
			: ; \
		else \
			${XEXEC} rm -f $$i; cp ${SMAILSRC}/$$i $$i; \
		fi; \
	done
	@. ./${DEFS_SH}; ${XEXEC} ${SHELL} ${MKDEPEND} $$CPPFLAGS ${INCLUDES} \
		Makefile ${CSRC} ${LOCAL_SRC}
	. ./${DEFS_SH}; echo "$$DEFS_DEPEND" >> Makefile
	chmod -w Makefile

check_defs:
	SHELL=${SHELL} ROOT=${ROOT} ${SHELL} ${CHECKDEFS}

mkline:	mkline.o string.o field.o parse.o ascii.o ${DEFS_SH} ${COMPAT_LIB}
	@. ./${DEFS_SH}; ${XEXEC} $$CC $$CFLAGS $$LDFLAGS -o mkline mkline.o \
		string.o field.o parse.o ascii.o \
		$$LIBS ${COMPAT_LIB}

mksort: mksort.o ascii.o ${DEFS_SH} ${COMPAT_LIB}
	@. ./${DEFS_SH}; ${XEXEC} $$CC $$CFLAGS $$LDFLAGS -o mksort mksort.o \
		ascii.o $$LIBS ${COMPAT_LIB}

dcasehost: dcasehost.o ascii.o ${DEFS_SH} ${COMPAT_LIB}
	@. ./${DEFS_SH}; ${XEXEC} $$CC $$CFLAGS $$LDFLAGS -o dcasehost \
		dcasehost.o ascii.o $$LIBS ${COMPAT_LIB}

mkdbm: mkdbm.o ascii.o ${DEFS_SH} ${COMPAT_LIB}
	@. ./${DEFS_SH}; ${XEXEC} $$CC $$CFLAGS $$LDFLAGS -o mkdbm mkdbm.o \
		ascii.o $$LIBS $$DBM_LIB ${COMPAT_LIB}

mkpath: mkpath.sh ${DEFS_SED}
	@-rm -f $@
	sed -f ${DEFS_SED} $@.sh > $@
	chmod +x $@

checkerr: checkerr.sh ${DEFS_SED}
	@-rm -f $@
	sed -f ${DEFS_SED} $@.sh > $@
	chmod +x $@

mkaliases: mkaliases.sh ${DEFS_SED}
	@-rm -f $@
	sed -f ${DEFS_SED} $@.sh > $@
	chmod +x $@

pathmerge: pathmerge.o ascii.o ${DEFS_SH} ${COMPAT_LIB}
	@. ./${DEFS_SH}; ${XEXEC} $$CC $$CFLAGS $$LDFLAGS -o pathmerge \
		pathmerge.o ascii.o $$LIBS ${COMPAT_LIB}

getmap: getmap.sh ${DEFS_SED}
	@-rm -f $@
	sed -f ${DEFS_SED} $@.sh > $@
	chmod +x $@

mkuuwho: mkuuwho.sh ${DEFS_SED}
	@-rm -f $@
	sed -f ${DEFS_SED} $@.sh > $@
	chmod +x $@

savelog: savelog.sh ${DEFS_SED}
	@-rm -f $@
	sed -f ${DEFS_SED} $@.sh > $@
	chmod +x $@

mkhpath: mkhpath.sh ${DEFS_SED}
	@-rm -f $@
	sed -f ${DEFS_SED} $@.sh > $@
	chmod +x $@

gleem: gleem.o ${DEFS_SH} ${COMPAT_LIB}
	@. ./${DEFS_SH}; ${XEXEC} $$CC $$CFLAGS $$LDFLAGS -o gleem gleem.o \
		$$LIBS ${COMPAT_LIB}

unsharmap: unsharmap.o string.o ascii.o ${DEFS_SH} ${COMPAT_LIB}
	@. ./${DEFS_SH}; ${XEXEC} $$CC $$CFLAGS $$LDFLAGS -o unsharmap \
		unsharmap.o string.o ascii.o $$LIBS ${COMPAT_LIB}

field.c: ${SMAILSRC}/field.c
	-rm -f $@
	cp $? $@

parse.c: ${SMAILSRC}/parse.c
	-rm -f $@
	cp $? $@

ascii.c: ${SMAILSRC}/ascii.c
	-rm -f $@
	cp $? $@

string.c: ${SMAILSRC}/string.c
	-rm -f $@
	cp $? $@

clean:
	rm -f ${OBJ} core a.out
	rm -f ${DEFS_SH} ${DEFS_H} ${DEFS_SED} ${LOCAL_SRC} XMakefile

clobber: clean
	rm -f ${TARGETS}
	rm -f .${DEFS_SH} .${DEFS_H} .${DEFS_SED} .Makefile

nuke:	clobber
	-${CLEAN}
	-${GET} Makefile

${OBJ}:	${DEFS_SH}

# DO NOT REMOVE THIS LINE, OR "make depend" WILL NOT WORK
