DESTDIR=$(shell ./get-install-path.sh)
WEBPHPTOKEN=XXX_NSG_WEB_SCRIPT_FILENAME_XXX
dirs="bin conf db lib log mod nginx run"

.PHONY: default
default:
	@echo "Nothing to build. Please read the README file. You can also just type 'make install' or make 'DESTDIR=<your-dest-dir> install' to install Netborder SS7 Gateway"

.PHONY: dep_check
dep_check:
	@if [ ! -e $(DESTDIR) ]; then \
		mkdir -p $(DESTDIR); \
	fi
	./dep-check.sh $(DESTDIR)

.PHONY: clean
clean:
	@echo "Nothing to clean"

.PHONY: install-init-scripts
install-init-scripts:
	@if [ -f /etc/redhat-release ]; then \
		echo "Red Hat / CentOS detected"; \
		install -m755 init.d/nsg.centos /etc/init.d/nsg; \
		install -m755 init.d/nsg-webgui.centos /etc/init.d/nsg-webgui; \
	elif [ -f /etc/lsb-release ]; then \
		echo "Ubuntu detected"; \
		install -m755 init.d/nsg.ubuntu /etc/init.d/nsg; \
		install -m755 init.d/nsg-webgui.ubuntu /etc/init.d/nsg-webgui; \
	elif [ -f /etc/debian_version ] || [ -f /etc/debian_release ]; then \
		echo "Debian detected"; \
		install -m755 init.d/nsg.ubuntu /etc/init.d/nsg; \
		install -m755 init.d/nsg-webgui.ubuntu /etc/init.d/nsg-webgui; \
	else \
		echo "Error: Failed to identify linux distribution" ; \
		exit -1; \
	fi
	install -m755 init.d/sngtc_server_ctrl /etc/init.d/sngtc_server_ctrl
	install -m755 init.d/mediarec.centos /etc/init.d/mediarec

#sed command not working this using head to delete last line from the file
	head -n -1 init.d/mediarec.sysconfig > init.d/mediarec.sysconfig.temp
	rm init.d/mediarec.sysconfig
	mv init.d/mediarec.sysconfig.temp init.d/mediarec.sysconfig
	echo "INSTALLDIR=$(DESTDIR)" >> init.d/mediarec.sysconfig
	install -m755 init.d/mediarec.sysconfig /etc/sysconfig/mediarec

.PHONY: remove-init
remove-init: remove-boot
	rm -f  /etc/init.d/nsg  
	rm -f  /etc/init.d/nsg-webgui 
	rm -f  /etc/init.d/sngtc_server_ctrl
	rm -f  /etc/init.d/mediarec
	rm -f  /etc/sysconfig/mediarec

.PHONY: boot
boot:
	@if [ -e /sbin/chkconfig ]; then \
		echo "chkconfig --add nsg"; \
		chkconfig --add nsg; \
		chkconfig --add nsg-webgui; \
		chkconfig --add sngtc_server_ctrl; \
		chkconfig --add mediarec; \
	elif [ -f /etc/lsb-release ] || [ -f /etc/debian_version ] || [ -f /etc/debian_release ]; then \
		update-rc.d nsg defaults; \
		update-rc.d nsg-webgui defaults; \
		update-rc.d sngtc_server_ctrl defaults; \
		update-rc.d mediarec defaults; \
	else \
		echo ; \
		echo "You have to add Netborder SS7 services to the boot sequence manually, your operating system is not supported for automatic boot configuration." ; \
		echo ; \
	fi

.PHONY: remove-boot
remove-boot:
	@if [ -e /sbin/chkconfig ]; then \
		echo "chkconfig --del nsg" ; \
		chkconfig --del nsg; \
		chkconfig --del nsg-webgui; \
		chkconfig --del sngtc_server_ctrl; \
		chkconfig --del mediarec; \
	elif [ -f /etc/lsb-release ] || [ -f /etc/debian_version ] || [ -f /etc/debian_release ]; then \
		update-rc.d -f nsg remove; \
		update-rc.d -f nsg-webgui remove; \
		update-rc.d -f sngtc_server_ctrl remove; \
		update-rc.d -f mediarec remove; \
	fi

.PHONY: rpm-install
rpm-install: 
	mkdir -p $(DESTDIR) 
	cp -r nsg/* $(DESTDIR) 
	cp -r init.d/* $(DESTDIR)/init.d 
	cp ./post-configure.sh $(DESTDIR)/init.d

.PHONY: install
install: dep_check install-init-scripts boot
	
	@if [ -f "/etc/init.d/nsg-webgui" ] ; then \
		if [ -f "/var/lock/subsys/nsg-webgui" ] ; then \
			/etc/init.d/nsg-webgui stop ; \
		fi ; \
		if [ -f "/var/lock/subsys/nsg" ]; then \
			/etc/init.d/nsg stop ; \
		fi ; \
		if [ -f "$(shell readlink -s /usr/sbin/sngtc_server_ctrl)" ] || [ -f "/usr/sbin/sngtc_server_ctrl" ]; then \
			/usr/sbin/sngtc_server_ctrl stop ; \
		fi ; \
	fi
	@echo "Installing Netborder SS7 Gateway at $(DESTDIR)"
	mkdir -p /etc/nsg
	echo "NSG_INSTALLDIR=$(DESTDIR)" > /etc/nsg/nsg.conf
	echo "NSG_TRANSCODING_SERVER=NO" >> /etc/nsg/nsg.conf
	echo "NSG_TMPFS_OPTIMIZATION=NO" >> /etc/nsg/nsg.conf
	echo "NSG_TMPFS_LOG_BACKUP=NO" >> /etc/nsg/nsg.conf
	mkdir -p $(DESTDIR)
	rm -rf /tmp/nsg.bk
	mkdir -p /tmp/nsg.bk

	@if [ -f "$(DESTDIR)/nginx/html/fusionpbx/fusionpbx.db" ]; then \
		echo "Generating backup of $(DESTDIR)/nginx/html/fusionpbx/fusionpbx.db on /tmp/nsg.bk/fusionpbx.db" ; \
		cp $(DESTDIR)/nginx/html/fusionpbx/fusionpbx.db /tmp/nsg.bk ; \
	fi

	@if [ -f "$(DESTDIR)/conf/license.txt" ]; then \
		echo "Generating backup of $(DESTDIR)/license on /tmp/nsg.bk/" ; \
		cp $(DESTDIR)/conf/license.* /tmp/nsg.bk ; \
	fi

	@if [ -f "$(DESTDIR)/conf/freetdm.conf" ]; then \
		echo "Generating backup of $(DESTDIR)/conf/freetdm.conf on $(DESTDIR)/conf/freetdm.conf.sngbkp" ; \
		cp $(DESTDIR)/conf/freetdm.conf $(DESTDIR)/conf/freetdm.conf.sngbkp ; \
	fi

	@if [ -f "$(DESTDIR)/conf/autoload_configs/freetdm.conf.xml" ]; then \
		echo "Generating backup of $(DESTDIR)/conf/autoload_configs/freetdm.conf.xml on $(DESTDIR)/conf/autoload_configs/freetdm.conf.xml.sngbkp" ; \
		cp $(DESTDIR)/conf/autoload_configs/freetdm.conf.xml $(DESTDIR)/conf/autoload_configs/freetdm.conf.xml.sngbkp ; \
	fi

	cp -r nsg/* $(DESTDIR) 

	@if [ -f "/tmp/nsg.bk/fusionpbx.db" ]; then \
		echo "Recovering backup from /tmp/nsg.bk/fusionpbx.db to $(DESTDIR)/nginx/html/fusionpbx/fusionpbx.db" ; \
		mv -f /tmp/nsg.bk/fusionpbx.db $(DESTDIR)/nginx/html/fusionpbx/fusionpbx.db ; \
	fi

	@if [ -f "/tmp/nsg.bk/license.txt" ]; then \
		echo "Recovering backup from /tmp/nsg.bk/license to $(DESTDIR)/conf/license" ; \
		mv -f /tmp/nsg.bk/license.* $(DESTDIR)/conf ; \
	fi

	@if [ -f "$(DESTDIR)/conf/freetdm.conf.sngbkp" ]; then \
		echo "Recovering backup from $(DESTDIR)/conf/freetdm.conf.sngbkp to $(DESTDIR)/conf/freetdm.conf" ; \
		mv $(DESTDIR)/conf/freetdm.conf.sngbkp $(DESTDIR)/conf/freetdm.conf ; \
	fi

	@if [ -f "$(DESTDIR)/conf/autoload_configs/freetdm.conf.xml.sngbkp" ]; then \
		echo "Recovering backup from $(DESTDIR)/conf/autoload_configs/freetdm.conf.xml.sngbkp to $(DESTDIR)/conf/autoload_configs/freetdm.conf.xml" ; \
		mv $(DESTDIR)/conf/autoload_configs/freetdm.conf.xml.sngbkp $(DESTDIR)/conf/autoload_configs/freetdm.conf.xml ; \
	fi

	ln -sf $(DESTDIR)/bin/nsg_cli /usr/sbin/
	ln -sf $(DESTDIR)/usr/sbin/safe_sngtc_server /usr/sbin/
	ln -sf $(DESTDIR)/usr/sbin/sngtc_cfg /usr/sbin/
	ln -sf $(DESTDIR)/usr/sbin/sngtc_server /usr/sbin/
	ln -sf $(DESTDIR)/usr/sbin/sngtc_server_ctrl /usr/sbin/
	ln -sf $(DESTDIR)/usr/sbin/sngtc_tool /usr/sbin/
	ln -sf $(DESTDIR)/usr/bin/mediarec /usr/sbin/

	cp -r nsg/etc/sngtc /etc/
	cp nsg/etc/sngtc/sngtc_server.conf.xml.sample /etc/sngtc/sngtc_server.conf.xml

	sed -i -e "1d" $(DESTDIR)/etc/mediarec/mediarec
	sed -i '1s#^#\#!$(DESTDIR)\/usr\/bin\/python\n#' $(DESTDIR)/etc/mediarec/mediarec

	sed -i 's#\/usr\/local\/nsg\/recordings#$(DESTDIR)\/recordings#g' $(DESTDIR)/etc/mediarec/mediarec.xml
	cp -r nsg/etc/mediarec /etc/

	@if [ -d /etc/ld.so.conf.d ]; then \
		echo "$(DESTDIR)/usr/lib" > /etc/ld.so.conf.d/nsg.conf ; \
		echo "$(DESTDIR)/usr/lib64" >> /etc/ld.so.conf.d/nsg.conf ; \
		echo "$(DESTDIR)/lib" >> /etc/ld.so.conf.d/nsg.conf ; \
	fi
	@if [ $(shell id -u) = "0" ]; then \
		/sbin/ldconfig ; \
	fi

	@if [ -f /etc/lsb-release ] || [ -f /etc/debian_version ] || [ -f /etc/debian_release ]; then \
		echo "Debian-based Linux detected. Working around libssl and libcrypto version naming..." ; \
		echo "Creating symlink: /usr/lib/libcrypto.so.0.9.8 => /usr/lib/libcrypto.so.6" ; \
		ln -sf /usr/lib/libcrypto.so.0.9.8 /usr/lib/libcrypto.so.6 ; \
		echo "Creating symlink: /usr/lib/libssl.so.0.9.8 => /usr/lib/libssl.so.6" ; \
		ln -sf /usr/lib/libssl.so.0.9.8 /usr/lib/libssl.so.6 ; \
	fi

	@if [ -f /etc/debian_version ] || [ -f /etc/debian_release ]; then \
		echo "Creating symlink: /lib/libpcre.so.3 /lib/libpcre.so.0" ; \
		ln -sf /usr/lib/libpcre.so.3 /lib/libpcre.so.0 ; \
	fi
	@echo
	./post-configure.sh $(DESTDIR) $(WEBPHPTOKEN); 
	@echo

	@echo "******************************************************************"
	@echo "Netborder SS7 Gateway was installed at $(DESTDIR)"
	@echo "Whenever you're ready to configure, just execute 'service nsg-webgui start' to run the web server used for configuration"
	@echo "The Sangoma Web Configurator can be reached at http://<server-ip>:8888/ with default user and password 'admin'"
	@echo "You can also run 'make boot' to install the Netborder SS7 Gateway service as a startup service"
	@echo "******************************************************************"

.PHONY: uninstall
uninstall: remove-boot
	rm -rf /etc/nsg
	rm -rf /etc/sngtc
	rm -rf /etc/mediarec
	rm -f /usr/sbin/safe_sngtc_server
	rm -f /usr/sbin/sngtc_cfg
	rm -f /usr/sbin/sngtc_server
	rm -f /usr/sbin/sngtc_server_ctrl
	rm -f /usr/sbin/sngtc_tool
	rm -f /usr/sbin/nsg_cli
	rm -f /etc/init.d/nsg
	rm -f /etc/init.d/nsg-webgui
	rm -f /etc/init.d/sngtc_server_ctrl
	rm -f /etc/init.d/mediarec
	rm -f /etc/sysconfig/mediarec

	@if [ -f "/var/log/mediarec.log" ]; then \
		echo "Removing mediarec log files"; \
		rm -rf /var/log/mediarec.log*; \
	fi

	@if [ -d "$(DESTDIR)/nginx" ] && [ -d "$(DESTDIR)/run" ] && [ -d "$(DESTDIR)/conf" ]; then \
		echo "Removing $(DESTDIR) "; \
		rm -rf $(DESTDIR); \
	else \
		echo "$(DESTDIR) already empty"; \
	fi

	@if [ -f /etc/lsb-release ] || [ -f /etc/debian_version ] || [ -f /etc/debian_release ]; then \
		echo "Debian-based Linux detected. Removing libssl, libcrypto and libpcre work-arounds..." ; \
		rm -f /usr/lib/libcrypto.so.6 /usr/lib/libssl.so.6 /lib/libpcre.so.0 ; \
	fi

.PHONY: remove
remove: uninstall

.PHONY: archive
archive:
	@echo "Archiving existing Netborder SS7 install"
	tar Pcfz nsg_old.tgz $(DESTDIR)
	@echo "Done!"
	@echo "Saving existing FreeTDM configuration files"
	cp -f $(DESTDIR)/conf/freetdm.conf ./nsg/conf/
	cp -f $(DESTDIR)/conf/autoload_configs/freetdm.conf.xml ./nsg/conf/autoload_configs/
	@echo "Done, run make install to update Netborder SS7"
	

.PHONY: print
print:
	@echo "$(DESTDIR)"
