#
# Makefile for
# Assembler/Disassembler for DOCSIS cable modem configuration files
#
# $Id: Makefile,v 1.2 2000/07/27 16:25:43 petras Exp $
#
# Copyright (C) 2000  Dietmar Petras  <Dietmar.Petras@ELSA.de>
# 
# This program 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; either version 2
# of the License, or (at your option) any later version.
# 
# This program 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; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# 
# History:
# $Log: Makefile,v $
# Revision 1.2  2000/07/27 16:25:43  petras
# File names changed to allow compilation on DOS with limited file names :-(
#
# Revision 1.1  2000/03/01 23:19:37  petras
# Initial revision.
#
#

# addapt the following settings to your local requirements

CC           = gcc
#DEBUG        = -g -Wall 
DEBUG        = -O3 -Wall -static -Wimplicit -W
LEX_COMMAND  = flex        # if yo do not want to install flex and bison,
YACC_COMMAND = bison       # you can simple call `make no_bison'
INSTALL_DIR  = /usr/local/bin

# do not edit behind this line

MODULES      = docas docdisas md5 hmac_md5 getopt
PROGRAMS     = docas docdisas
OBJ_DIR      = o

CFLAGS       = -I. $(DEBUG)
LFLAGS       = $(DEBUG)

all: $(PROGRAMS)

ifndef SRCS
SRCS	:= $(wildcard $(addsuffix .c, $(MODULES)))
endif
ifndef OBJS
OBJS	:= $(patsubst %.c, $(OBJ_DIR)/%.o, $(SRCS))
endif

obj:	$(OBJS)

$(OBJS):
	$(CC) $(CFLAGS) -o $@ -c $(patsubst $(OBJ_DIR)/%.o, %.c, $@)

YACC=yaccan
YACC.y=$(YACC).y
YACC.c=$(OBJ_DIR)/$(YACC).c
YACC.h=$(OBJ_DIR)/$(YACC)_t.h
YACC.o=$(OBJ_DIR)/$(YACC).o

$(YACC.c) $(YACC.h): $(YACC.y)
	$(YACC_COMMAND) -v -t -d $(YACC.y)
	mv $(YACC).tab.c $(YACC.c)
	mv $(YACC).tab.h $(YACC.h)

$(YACC.o): %.o: %.c %_t.h
	$(CC) $(CFLAGS) -o $@ -c $(@:%.o=%.c)

obj: $(YACC.o)

.PHONY: clean-yacc
clean-yacc:
	rm -f $(YACC.c) $(YACC.h) $(YACC).output

clean: clean-yacc

LEX=lexan
LEX.l=$(LEX).l
LEX.c=$(OBJ_DIR)/$(LEX).c
LEX.o=$(OBJ_DIR)/$(LEX).o

$(LEX.c): $(LEX.l) $(YACC.h)
	$(LEX_COMMAND) -d -L $(LEX.l)
	mv lex.yy.c $(LEX.c)

$(LEX.o): %.o: %.c
	$(CC) $(CFLAGS) -o $@ -c $(@:%.o=%.c)

obj: $(LEX.o)

.PHONY: clean-lex
clean-lex:
	rm -f $(LEX.c)

clean: clean-lex

SRCS	:= $(SRCS) $(YACC.c) $(LEX.c)
OBJS	:= $(OBJS) $(YACC.o) $(LEX.o)

docas: $(OBJ_DIR)/docas.o $(OBJ_DIR)/md5.o $(OBJ_DIR)/hmac_md5.o $(YACC.o) $(LEX.o)
	$(CC) $(LFLAGS) -o $@ $^

docdisas: $(OBJ_DIR)/docdisas.o $(OBJ_DIR)/md5.o
	$(CC) $(LFLAGS) -o $@ $^

clean:
	$(RM) -f $(OBJ_DIR)/*.o
	$(RM) -f $(PROGRAMS)

.PHONY: no_bison
no_bison:
	touch no_bison/*
	cp no_bison/* o

install:
	@mkdir -p $(INSTALL_DIR)
	install -s -m  0755 docas $(INSTALL_DIR)
	install -s -m  0755 docdisas $(INSTALL_DIR)

depend: $(SRCS)
	@echo "generating .depend"
	@$(CC) $(CFLAGS) -M $(SRCS) | sed 's:^\([^ ]\):$$(OBJ_DIR)/\1:; s: o/: $$(OBJ_DIR)/:g; s: \./: :g; s: /usr/[^ ]*::g; /^  \\/d;' >.depend

.depend:
	touch .depend

include .depend
