
#---------------------------------------------------------------------
# Makefile for installing Asterisk, Zaptel, Libpri, mISDN, chan_misdn
#
# Copyright (C) 2006, Nadi Sarrar <nadi@beronet.com>
#
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2.
#---------------------------------------------------------------------

#--------------------------------------------
# CONFIGURATION: Start
#--------------------------------------------

# source locations
ASTERISK = http://ftp.digium.com/pub/asterisk/asterisk-1.2-current.tar.gz
ASTERISK_ADDONS = http://ftp.digium.com/pub/asterisk/asterisk-addons-1.2-current.tar.gz
ASTERISK_SOUNDS = http://ftp.digium.com/pub/asterisk/releases/asterisk-sounds-1.2.1.tar.gz
LIBPRI = http://ftp.digium.com/pub/libpri/libpri-1.2-current.tar.gz
ZAPTEL = http://ftp.digium.com/pub/zaptel/zaptel-1.2-current.tar.gz
INSTALL_MISDN = http://www.beronet.com/downloads/install-misdn-mqueue.tar.gz

# bins that are reqired. format: debian-package/binname
BINS = sed/sed gcc/gcc tar/tar gzip/gzip bzip2/bzip2 bc/bc flex/flex bison/bison cvs/cvs usbutils/lsusb

# files that need to exist. format: debian-package/filename
FILES  = libc6-dev//usr/include/string.h
FILES += libc6-dev//usr/include/unistd.h
FILES += libc6-dev//usr/include/stdlib.h
FILES += libncurses5-dev//usr/include/curses.h
FILES += libncurses5-dev//usr/include/ncurses.h
FILES += libncurses5-dev//usr/lib/libtermcap.so
FILES += libssl-dev//usr/lib/libssl.so
FILES += zlib1g-dev//usr/lib/libz.so

#--------------------------------------------
# CONFIGURATION: End
#--------------------------------------------

BIN_CHECKS = $(BINS:=.bin_check)
FILE_CHECKS = $(FILES:=.file_check)

ifndef IS_DEBIAN
  IS_DEBIAN := $(shell if [ ! -z $$(ls /etc/debian_version 2> /dev/null) ]; then echo "y"; fi)
endif

ifndef KVERS
  KVERS := $(shell uname -r)
endif
ifndef KSRC
  ifneq (,$(wildcard /lib/modules/$(KVERS)/build))
    KSRC := /lib/modules/$(KVERS)/build
  else
    KSRC_SEARCH_PATH := /usr/src/linux
    KSRC := $(shell for dir in $(KSRC_SEARCH_PATH); do if [ -d $$dir ]; then echo $$dir; break; fi; done)
  endif
endif


all: libpri zaptel asterisk misdn

install: all

clean:
	rm -rf asterisk-* libpri-* install-misdn* zaptel-*

FORCE: check-root check-env

check-root:
	@echo -n "checking for root privileges ..."
	@if [ "$(shell id -u)" != "0" ]; then \
		echo " FAILED"; echo; \
		echo "[!!] FATAL: You need to be root for this."; \
		echo; exit -1; \
	else \
		echo " got em all :)"; \
	fi

check-env: $(BIN_CHECKS) $(FILE_CHECKS)

%.bin_check:
	@echo -n "checking for $(notdir $*) ..."
	@if ! which $(notdir $*) > /dev/null; then \
		echo " FAILED"; echo; \
		echo "[!!] FATAL: $(notdir $*) not in path, please install."; \
		echo "     On a Debian system, please run the following command to fix this:"; \
		echo "         apt-get install $(shell echo $* | sed -ne 's/^\([^\/]*\)\/.*$//\1/p')"; \
		if [ "$(IS_DEBIAN)" = "y" ]; then \
			echo; echo -n "May i do this for you? [Y/n] "; read re; \
			if [ -z "$$re" -o "$$re" = "y" -o "$$re" = "Y" ]; then \
				apt-get install $(shell echo $* | sed -ne 's/^\([^\/]*\)\/.*$//\1/p') || exit -1; \
			else \
				echo; exit -1; \
			fi; \
		else \
			echo; exit -1; \
		fi; \
	else \
		echo " $(shell which $(notdir $*))"; \
	fi

%.file_check:
	@echo -n "checking for $(notdir $*) ..."
	@if [[ ! -e "$(shell echo $* | sed -ne 's/^[^\/]*\/\(.*\)$//\1/p')" ]] && [[ $(shell echo $* | sed -ne 's/^[^\/]*\/\(.*\)\/\(.*\)$//\1 -name \2\*/p' | xargs find | wc -l) -lt 1 ]]; then \
		echo " FAILED"; echo; \
		echo "[!!] FATAL: $(shell echo $* | sed -ne 's/^[^\/]*\/\(.*\)$//\1/p') not found."; \
		echo "     On a Debian system, please run the following command to fix this:"; \
		echo "         apt-get install $(shell echo $* | sed -ne 's/^\([^\/]*\)\/.*$//\1/p')"; \
		if [ "$(IS_DEBIAN)" = "y" ]; then \
			echo; echo -n "May i do this for you? [Y/n] "; read re; \
			if [ -z "$$re" -o "$$re" = "y" -o "$$re" = "Y" ]; then \
				apt-get install $(shell echo $* | sed -ne 's/^\([^\/]*\)\/.*$//\1/p') || exit -1; \
			else \
				echo; exit -1; \
			fi; \
		else \
			echo; exit -1; \
		fi; \
	else \
		echo " $(shell echo $* | sed -ne 's/^[^\/]*\/\(.*\)$//\1/p')"; \
	fi

kernel-src-check:
	@echo -n "checking for kernel source ..."
	@if [ ! -d "$(KSRC)" ]; then \
		echo " FAILED"; echo; \
		echo "[!!] FATAL: kernel source not found."; \
		echo "     You need to have the kernel source tree of your running kernel ($(KVERS)) installed,"; \
		echo "     and a symbolic link /lib/modules/$(KVERS)/build pointing to it."; \
		echo; \
		echo "     A quick 1-2-3 guide to get this done:"; \
		echo; \
		echo "      (1) Visit http://www.kernel.org and download the source tarball matching your kernel version."; \
		echo "      (2) Unpack it (a common place is /usr/src/)."; \
		echo "      (3) Let /lib/modules/$(KVERS)/build point to the kernel source tree you just unpacked."; \
		echo; \
		echo "     Or, on a Debian system with a stock kernel, just run:"; \
		echo "         apt-get install kernel-headers-$(KVERS)"; \
		echo "     or"; \
		echo "         apt-get install linux-headers-$(KVERS)"; \
		echo "     depending on your version of Debian."; \
		if [ "$(IS_DEBIAN)" = "y" ]; then \
			echo; echo -n "May i do this for you? [Y/n] "; read re; \
			if [ -z "$$re" -o "$$re" = "y" -o "$$re" = "Y" ]; then \
				apt-get install kernel-headers-$(KVERS) || apt-get install linux-headers-$(KVERS) || exit -1; \
			else \
				echo; exit -1; \
			fi; \
		else \
			echo; exit -1; \
		fi; \
		echo; \
	else \
		echo " $(KSRC)"; \
	fi

libpri: FORCE libpri-download
	@echo; echo " >> Compiling and installing libpri ..."; echo; sleep 1
	@cd $(shell ls -d libpri* 2>/dev/null | grep -v tar) ; make install

libpri-download:
	@if [ -z $(shell ls -d libpri* 2>/dev/null | grep -v tar) ]; then \
		if [ ! -e "$(notdir $(LIBPRI))" ]; then make $(LIBPRI); fi; \
		echo; echo " >> Unpacking $(notdir $(LIBPRI)) ..."; echo; \
		tar zxf $(notdir $(LIBPRI)); \
	fi

zaptel: FORCE kernel-src-check zaptel-download
	@echo; echo " >> Compiling and installing zaptel ..."; echo; sleep 1
	@cd $(shell ls -d zaptel* 2>/dev/null | grep -v tar) ; make install

zaptel-download:
	@if [ -z $(shell ls -d zaptel* 2>/dev/null | grep -v tar) ]; then \
		if [ ! -e "$(notdir $(ZAPTEL))" ]; then make $(ZAPTEL); fi; \
		echo; echo " >> Unpacking $(notdir $(ZAPTEL)) ..."; echo; \
		tar zxf $(notdir $(ZAPTEL)); \
	fi

asterisk: FORCE asterisk-download
	@echo; echo " >> Compiling and installing Asterisk ..."; echo; sleep 1
	@cd $(shell ls -d asterisk* 2>/dev/null | grep -v tar | grep -v sounds | grep -v addons) ; make install
	@echo; echo " >> Compiling and installing Asterisk addons ..."; echo; sleep 1
	@cd $(shell ls -d asterisk* 2>/dev/null | grep -v tar | grep addons) ; make install
	@echo; echo " >> Installing Asterisk sounds ..."; echo; sleep 1
	@cd $(shell ls -d asterisk* 2>/dev/null | grep -v tar | grep sounds) ; make install

asterisk-download:	
	@if [ -z $(shell ls -d asterisk* 2>/dev/null | grep -v tar | grep -v sounds | grep -v addons) ]; then \
		if [ ! -e "$(notdir $(ASTERISK))" ]; then make $(ASTERISK); fi; \
		echo; echo " >> Unpacking $(notdir $(ASTERISK)) ..."; echo; \
		tar zxf $(notdir $(ASTERISK)); \
	fi
	@if [ -z $(shell ls -d asterisk* 2>/dev/null | grep -v tar | grep addons) ]; then \
		if [ ! -e "$(notdir $(ASTERISK_ADDONS))" ]; then make $(ASTERISK_ADDONS); fi; \
		echo; echo " >> Unpacking $(notdir $(ASTERISK_ADDONS)) ..."; echo; \
		tar zxf $(notdir $(ASTERISK_ADDONS)); \
	fi
	@if [ -z $(shell ls -d asterisk* 2>/dev/null | grep -v tar | grep sounds) ]; then \
		if [ ! -e "$(notdir $(ASTERISK_SOUNDS))" ]; then make $(ASTERISK_SOUNDS); fi; \
		echo; echo " >> Unpacking $(notdir $(ASTERISK_SOUNDS)) ..."; echo; \
		tar zxf $(notdir $(ASTERISK_SOUNDS)); \
	fi

misdn: FORCE kernel-src-check misdn-download
	@echo; echo " >> Compiling and installing mISDN and chan_misdn ..."; echo; sleep 1
	@cd $(shell ls -d install-misdn* 2>/dev/null | grep -v tar) ; make install

misdn-download:
	@if [ -z $(shell ls -d install-misdn* 2>/dev/null | grep -v tar) ]; then \
		if [ ! -e "$(notdir $(INSTALL_MISDN))" ]; then make $(INSTALL_MISDN); fi; \
		echo; echo " >> Unpacking $(notdir $(INSTALL_MISDN)) ..."; echo; \
		tar zxf $(notdir $(INSTALL_MISDN)); \
	fi

%.tar.gz:
	@echo; echo " >> Downloading $(notdir $@) ..."; echo
	@wget $@

force:
	make -C install-misdn-mqueue force

.PHONY: all install clean FORCE check-root check-env kernel-src-check libpri libpri-download zaptel zaptel-download asterisk asterisk-download misdn misdn-download force

