# ****************************************************************************
#                Makefile for The Linux Ultra Sound Project
#         Copyright (c) 1994-1997 by Jaroslav Kysela (Perex soft)
# ****************************************************************************

.EXPORT_ALL_VARIABLES:

CALL_FROM_GLOBAL_MAKEFILE = 1
include Makefile.conf
CALL_FROM_GLOBAL_MAKEFILE =

ifdef ARCH_i386
ARCH_NAME	= i386
ARCH_COPTS	= -m386
endif

ifdef ARCH_i486
ARCH_NAME	= i486
ARCH_COPTS	= -m486
endif

ifdef ARCH_pentium
ARCH_NAME	= pentium
ARCH_COPTS	= -m486 -malign-loops=2 -malign-jumps=2 -malign-functions=2
endif

ifdef ARCH_ppro
ARCH_NAME	= ppro
ARCH_COPTS	= -m486 -malign-loops=2 -malign-jumps=2 -malign-functions=2
endif

ifdef ARCH_alpha
ARCH_NAME	= alpha
ARCH_COPTS	=
endif

ifndef ARCH_NAME
ARCH_NAME	= unknown
ARCH_COPTS	=
endif

ARCH_SUPPORTED	= "i386 i486 pentium ppro alpha"
ARCH_MP_SUPPORTED = "none smp"

ifdef ARCH_MP_SMP
ifndef ARCH_NUMCPU
ARCH_NUMCPU	= 2
endif
ARCH_MP_NAME	= "smp"
else
ARCH_NUMCPU	= 0
ARCH_MP_NAME	= "none"
endif

MAKE_JOBS	= `expr $(ARCH_NUMCPU) + 1`

# options for C compiler
ARCH_COPTS	+= -O2
# options for make
ARCH_MOPTS	=

# ****************************************************************************

U_SUBDIRS	= src include lib bin sbin etc examples apps-setup
U_LIST		= `pwd`/.files
U_REVISION	= `cat .revision`

MAKE1		= make $(ARCH_MOPTS)
MAKE		= $(MAKE1) -j $(MAKE_JOBS)

all: welcome0
	@echo " make compile    - compile all targets"
	@echo " make install    - compile & install all targets"
	@echo " make uninstall  - uninstall all targets"
	@echo
	@setterm -reverse on
	@echo "Trouble? Consult FAQ file or send e-mail to <ultra@pf.jcu.cz>."
	@setterm -reverse off
	@echo
	@echo "Please, don't waste my time by answering to some simple questions."
	@echo
	@echo "                                  thank you very much - Jaroslav Kysela"
	@echo

welcome0:
	@echo
	@echo "Welcome to the Linux Ultra Sound Project"
	@echo "========================================"
	@echo

welcome1: welcome0
	@echo "Supported system architectures    : $(ARCH_SUPPORTED)"
	@echo "Target system architecture        : $(ARCH_NAME)"
	@echo "Supported multiprocessor arch...s : $(ARCH_MP_SUPPORTED)"
	@echo "Multiprocessor architecture       : $(ARCH_MP_NAME)"
	@echo "Make - jobs to run simultaneously : $(MAKE_JOBS)"
	@echo "Targed directory for executables  : '$(U_BIN_DIR)'"
	@echo "Targed directory for daemons      : '$(U_DMN_DIR)'"
	@echo "Targed directory for modules/cfg  : '$(U_MOD_DIR)'"
	@echo "Targed directory for C libraries  : '$(U_LIB_DIR)'"
	@echo "Targed directory for C headers    : '$(U_HDR_DIR)'"
	@echo "Targed directory for instr..s cfg : '$(U_INS_DIR)'"
	@echo

compile: welcome1
	@echo -n "Hit 'Enter' to compile all targets or 'Ctrl-C' to abort... "
	@read
	@echo
	
	@$(MAKE) -s -C src tgtcheck
	@echo
	@echo -n "Hit 'Enter' to continue or 'Ctrl-C' to abort... "
	@read

	@for i in $(U_SUBDIRS); do \
	  if ! $(MAKE) -C $$i \
		FINAL_VERSION=1 \
		LIST_OF_FILES=$(U_LIST) \
		INSTALL_DIRECTORY=$(U_BIN_DIR) \
		MODULES_DIRECTORY=$(U_MOD_DIR) \
		DAEMONS_DIRECTORY=$(U_DMN_DIR) \
		LIBRARIES_DIRECTORY=$(U_LIB_DIR) \
		HEADERS_DIRECTORY=$(U_HDR_DIR) gcompile; exit 1; fi; \
	 done

	@echo
	@echo "All targets for the Linux Ultra Sound Project was successfully compiled..."
	@echo

install: welcome1
	@echo -n "Hit 'Enter' to compile and install all targets or 'Ctrl-C' to abort... "
	@read
	@echo
	
	@$(MAKE) -s -C src tgtcheck
	@echo
	@echo -n "Hit 'Enter' to continue or 'Ctrl-C' to abort... "
	@read

	@rm -f .files
	@for i in $(U_BIN_DIR) $(U_DMN_DIR) $(U_MOD_DIR) $(U_LIB_DIR) $(U_HDR_DIR); do \
          if [ -d $$i ]; then \
            echo > /dev/null; \
          else \
            install -d -o root -g root $$i; \
          fi; \
         done
	@for i in $(U_SUBDIRS); do \
	  if ! $(MAKE) -C $$i \
		FINAL_VERSION=1 \
		LIST_OF_FILES=$(U_LIST) \
		INSTALL_DIRECTORY=$(U_BIN_DIR) \
		MODULES_DIRECTORY=$(U_MOD_DIR) \
		DAEMONS_DIRECTORY=$(U_DMN_DIR) \
		LIBRARIES_DIRECTORY=$(U_LIB_DIR) \
		HEADERS_DIRECTORY=$(U_HDR_DIR) \
		ginstall; then exit 1; fi; \
	 done

#	@echo "----"
#	@sh src/utils/lsfiles $(U_LIST)
#	@echo "----"
	@echo
	@echo "All targets for the Linux Ultra Sound Project are successfully installed..."
	@echo

uninstall:
	@sh src/utils/uninstall $(U_LIST)

clean:
	rm -rf ultra-* usr
	for i in $(U_SUBDIRS); do $(MAKE) -C $$i clean; done
	rm -f *~ .*~ .lgdb doc/*~

pack:	clean pack0 lsm
	@echo -----
	@ls -l ../ultra-${U_REVISION}.lsm
	@ls -l ../ultra-${U_REVISION}.tgz

diff:	clean clean0
	-diff -ruN ../ultra-${DIFF} ../ultra > ../ultra-${U_REVISION}.patch

floppy:	pack
	@echo "Insert EXT2 floppy disk to first drive..."
	@read
	@mount /mnt/floppy
	@rm -rf /mnt/floppy/ultra-* /mnt/floppy/backup
	@mkdir /mnt/floppy/backup
	@/bin/cp -v ../ultra-${U_REVISION}.* /mnt/floppy
	@/bin/cp -v ../ultra-${U_REVISION}.* /mnt/floppy/backup
	@umount /mnt/floppy

tape:	pack
	@echo "Insert TAPE to streamer..."
	@read
	@mt eod
	@tar cvf /dev/tape -C .. ultra-${U_REVISION}.lsm ultra-${U_REVISION}.tgz
	@mt rewind

clean0:
	rm -f `find . -name core`
	rm -f .config .config? .files src/driver/config.h
	chown -R root.root ../ultra

pack0: clean0
	@echo "Building package: ultra-${U_REVISION}.tgz"
	mv ../ultra ../ultra-${U_REVISION} 
	- tar -cvzC .. -f ../ultra-${U_REVISION}.tgz ultra-${U_REVISION}
	mv ../ultra-${U_REVISION} ../ultra

lsm:
	sh src/utils/ultra.lsm > ../ultra-${U_REVISION}.lsm
