# Copyright (C) 2000  Britton Leo Kerin, see copyright. 

PROG = rawrec
ALT_INVOC = rawplay
OBJS = audio_close.o audio_init.o process_command_line.o data_close.o \
       data_init.o get_au_blksz.o get_format_code.o globals.o is_pow_two.o \
       main.o min.o move_au.o move_fd.o play.o record.o ringbuf_close.o \
       ringbuf_init.o set_au_blksz.o sleep_on_option.o \
       shutdown_signal_handler.o test_dsp_params.o usage.o \
       write_silence_on_option.o
SRCS = audio_close.c audio_init.c process_command_line.c data_close.c \
       data_init.c get_au_blksz.c get_format_code.c globals.c is_pow_two.c \
       main.c min.c move_au.c move_fd.c play.c record.c ringbuf_close.c \
       ringbuf_init.c set_au_blksz.c sleep_on_option.c \
       shutdown_signal_handler.c test_dsp_params.c usage.c \
       write_silence_on_option.c
HEADERS = rawrec.h thread_functions.h
CC = gcc
# USEBUFFLOCK appears to be historic, and could probably go.
CFLAGS = -Wall -Wpointer-arith -Wstrict-prototypes -O2 -DUSEBUFFLOCK \
-D_REENTRANT -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE
LDFLAGS = -lm -lpthread

# The GNU way is just too painful.  I'm not writing three tiers of
# variables just to get to /usr/local/man/man1.  If it starts to seem 
# worthwhile to use autoconf, this stuff can change then.
EXE_DIR = /usr/local/bin
MAN_DIR = /usr/local/man/man1

# If a build command exits with non-zero return code, delete any target 
# file corresponding to that command.  Probably not relevant for this 
# Makefile, but very much the right thing to do in general. 
.DELETE_ON_ERROR:

all: $(PROG) $(ALT_INVOC)

$(PROG): $(OBJS)
	$(CC) $(LDFLAGS) $(OBJS) -o $(PROG)

$(ALT_INVOC): $(PROG)
	ln -sf $(PROG) $(ALT_INVOC)

# This target definately needs work.
debug: $(SRCS)
	$(CC) -Wall -DUSEBUFFLOCK -D_REENTRANT -DDEBUG -D_GNU_SOURCE \
                -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -g -o $(PROG) \
                $(LIBS) $(SRCS) $(LDFLAGS)
	ln -sf $(PROG) $(ALT_INVOC)

bin_install: all
	install -d $(EXE_DIR)
	install $(PROG) $(EXE_DIR)
	ln -sf $(PROG) $(EXE_DIR)/$(ALT_INVOC)
	chown root $(EXE_DIR)/$(PROG) $(EXE_DIR)/$(ALT_INVOC)
	chgrp root $(EXE_DIR)/$(PROG) $(EXE_DIR)/$(ALT_INVOC)
	@echo ''
	@echo '***** SECURITY WARNING *****'
	@echo ''
	@echo 'Making executable suid.'
	@echo '$(PROG) can provide soft real time scheduling capabilities as'
	@echo 'root that it cannot offer otherwise, but if security paranoia' 
	@echo 'is an issue, root permissions are no longer required.'
	@echo ''
	chmod go-w $(EXE_DIR)/$(PROG)
	chmod u+s $(EXE_DIR)/$(PROG)

man_install:
	install -d $(MAN_DIR)
	install --mode='u=rw,go=r' ../docs/user/$(PROG).1 $(MAN_DIR)
	ln -sf $(PROG).1 $(MAN_DIR)/$(ALT_INVOC).1

install: bin_install man_install

clean:
	rm -f $(PROG) $(ALT_INVOC) *.o

# This is mainly used for development work.
distclean: clean
	@echo 'Cleaning src directory (cruft may remain in other dirs)...'
	rm -f TAGS
	rm -f *~

uninstall:
	rm -f $(EXE_DIR)/$(ALT_INVOC)
	rm -f $(EXE_DIR)/$(PROG)
	rm -f $(MAN_DIR)/$(ALT_INVOC).1
	rm -f $(MAN_DIR)/$(PROG).1

$(OBJS): $(HEADERS)

.PHONY: all debug install bin_install man_install clean uninstall
