ifndef ARCH
       ARCH=$(shell uname -m)
endif

ifdef DESTDIR
	ifdef INSTALLPREFIX
		DESTDIR=$(INSTALLPREFIX)
	else
		DESTDIR=
	endif
endif

ifndef LIB_DIR
	LIB_DIR=$(DESTDIR)/usr/lib/sangoma_prid
endif

ifndef FREETDM_MOD_DIR
	FREETDM_MOD_DIR=$(DESTDIR)/usr/local/freeswitch/mod
endif

SANGOMA_PRI_DAEMON=sangoma_prid.i386
SANGOMA_PRI_LIB=sangoma_prid.i386.so
SRC_LIBDIR=./lib32
ifeq ($(ARCH),x86_64)
	SANGOMA_PRI_DAEMON=sangoma_prid.x86_64 
	SANGOMA_PRI_LIB=sangoma_prid.x86_64.so
	SRC_LIBDIR=./lib64
endif


all:
	

install_all: install_libs install_daemon install_freetdm_mod

#for now, default install is daemon
install:install_libs install_daemon

install_mod: install_libs install_freetdm_mod

#kept for backward compatibility
install_freetdm_lib: install_freetdm_mod

install_libs:
	@echo "Verying syslog..."
	./chk_syslog.sh 
	@echo " "
	@echo "Copying Libraries..."
	./chk_dir.sh $(LIB_DIR)
	@cp -f $(SRC_LIBDIR)/* $(LIB_DIR)
	@echo " "
	@if [[ ! -d /etc/ld.so.conf.d ]]; then \
		cat sangoma_prid.ld.conf >> /etc/ld.so.conf; \
	elif [[ ! -e /etc/ld.so.conf.d/sangoma_prid.ld.conf ]]; then \
		cp sangoma_prid.ld.conf /etc/ld.so.conf.d ; \
	fi
	@ldconfig ;

install_daemon:
	@echo "Installing Binary..."
	install -D bin/$(SANGOMA_PRI_DAEMON) $(DESTDIR)/usr/sbin/sangoma_prid

	@if [[ ! -e /etc/wanpipe/smg_pri.conf ]]; then \
		cp smg_pri.conf /etc/wanpipe ; \
	fi


#Needed for cases when SMG is not installed

	@echo "Installing smg_ctrl scripts"
	@cp -f ../sangoma_mgd.trunk/smg_ctrl $(DESTDIR)/usr/sbin/smg_ctrl
	@cp -f ../sangoma_mgd.trunk/safe_sangoma $(DESTDIR)/usr/sbin/safe_sangoma
	@cp -f ../sangoma_mgd.trunk/rc/smg.rc.pri_only $(DESTDIR)/etc/wanpipe/smg.rc

	@echo
	@echo "Sangoma PRI Install Done"
	@echo


install_freetdm_mod:
	@echo "Installing FreeTDM Module..."
	./chk_dir.sh $(FREETDM_MOD_DIR)
	install -D lib/$(SANGOMA_PRI_LIB) $(FREETDM_MOD_DIR)/sangoma_prid.so
