INSTALL = install
CHKCONFIG = /sbin/chkconfig
prefix  ?= /usr/local
SHELL := /bin/bash
MYSQL = mysql

PROGRAMS := precheck parselog asterniclog ioncube mysql webfiles perlfinal
	
$(phony all): $(PROGRAMS)

.PHONY install: precheck parselog asterniclog ioncube mysql setpasswords webfiles pdf sqlupdate perlfinal 
.PHONY upgrade: precheckupgrade parselog asterniclog webfiles pdf sqlupdate asterniclogrestart

DESTDIR?=/var/www/html/stats

precheck:
	@echo "Checking if Asternic is already installed"
	@if [ -f $(DESTDIR)/answered.php ]; then \
		echo -e "\033[31mAlready Installed!\e[0m"; \
		echo -e "If you want to upgrade an existing installation, use the command: \033[32mmake upgrade\e[0m"; \
		exit 1; \
	fi
	@if [ -f /var/www/html/stats/answered.php ]; then \
		echo -e "\033[31mAlready Installed!\e[0m"; \
		echo -e "If you want to upgrade an existing installation, use the command: \033[32mmake upgrade\e[0m"; \
		exit 1; \
	fi
	@if [ -f /var/www/stats/answered.php ]; then \
		echo -e "\033[31mAlready Installed!\e[0m"; \
		echo -e "If you want to upgrade an existing installation, use the command: \033[32mmake upgrade\e[0m"; \
		exit 1; \
	fi

precheckupgrade:
	@echo "Checking if Asternic is installed"
	@if [ ! -f /var/www/html/stats/asterniclic.php ] && [ ! -f /var/www/stats/asterniclic.php ] && [ ! -f $(DESTDIR)/asterniclic.php ]; then \
		echo "Asternic is not installed, or license not found! Upgrade aborted"; \
		exit 1; \
	fi
	@PHPVER=$$(php -v | head -n1 | cut -d ' ' -f2 | awk -F. '{print $$1}'); \
	if [ "$$PHPVER" = "5" ]; then \
		echo "Detected PHP version 5"; \
		HTMLDIR="html"; \
	elif [ "$$PHPVER" = "7" ]; then \
		echo "Detected PHP version 7"; \
		HTMLDIR="html"; \
	else \
		echo "Detected PHP version 8"; \
		HTMLDIR="html"; \
	fi; \
	if [ -f $(DESTDIR)/asterniclic.php ]; then \
		cp -f $$HTMLDIR/check-upgrade.* $(DESTDIR); \
		php $(DESTDIR)/check-upgrade.php; \
	elif [ -f /var/www/html/stats/asterniclic.php ]; then \
		echo "dos"; \
		cp -f $$HTMLDIR/check-upgrade.* /var/www/html/stats  \
		php /var/www/html/stats/check-upgrade.php; \
	elif [ -f /var/www/stats/asterniclic.php ]; then \
		cp -f $$HTMLDIR/check-upgrade.* /var/www/stats; \
		php /var/www/stats/check-upgrade.php; \
	fi;\
	if [ $$? -ne 0 ]; then \
		exit 1; \
	fi

asterniclogrestart:
	@if [ -d /var/log/asterisk ]; then \
	if [ -f /etc/debian_version ]; then \
		if [ $$(ps --no-headers -o comm 1) = "systemd" ]; then \
			systemctl restart asterniclog; \
		else \
			/etc/init.d/asterniclog restart; \
		fi; \
	elif [ -f /etc/redhat-release ] || [ -f /etc/os-release ]; then \
		if [ $$(ps --no-headers -o comm 1) = "systemd" ]; then \
			systemctl restart asterniclog; \
		else \
			/etc/init.d/asterniclog restart; \
		fi; \
	fi; \
	fi

setpasswords:
	@admin_pw=1; \
	admin_pw_confirm=2; \
	mysqlpassword=$$(cat /tmp/mpwd); \
	rm -f /tmp/mpwd; \
	x=1;\
	while [ "$$admin_pw" != "$$admin_pw_confirm" ] ;\
	do \
	if [ $$x -gt 1 ]; then \
		echo -e "\033[31mPasswords do not match!\e[0m"; \
	fi; \
	x=$$(( $$x + 1 )); \
	read -s -p "Select a password for the 'admin' user: " admin_pw ;\
	echo ;\
	read -s -p "Confirm the password for the 'admin' user: " admin_pw_confirm ;\
	echo ;\
	done ;\
	${MYSQL} -sss -u root $${mysqlpassword} qstats -e "UPDATE users SET password=sha1('$$admin_pw') WHERE login='admin'"; \
	export REST_PWD=$$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1); \
	${MYSQL} -sss -u root $${mysqlpassword} qstats -e "UPDATE users SET password=sha1('$$REST_PWD') WHERE login='restUser'"; \
	sed -i "s/restPassX/$$REST_PWD/" html/config.php  ;\

parselog:
	@test -d $(prefix) || mkdir -p $(prefix); 
	@$(INSTALL) -d -m 755 parselog /usr/local/parselog;
	@$(INSTALL) -m 755 parselog/* /usr/local/parselog/; 

sqlupdate:
	@echo "Updating tables and data..."
	@php $(DESTDIR)/update_db.php
	
uninstall:
	@if [ ! -f $(DESTDIR)/answered.php ]; then \
		echo "Asternic is not installed!"; \
		exit 1; \
	fi
	@cuantos=$$(echo "$(DESTDIR)" | awk -F"/" '{print NF-1}'); \
	if [ $$cuantos -lt 4 ]; then \
		echo "I am not comfortable removing $(DESTDIR). Aborting"; \
		exit 1; \
	fi
	@echo "I am about to remove $(DESTDIR) and all of its subdirectories"
	@read -r -p "Are you sure? [y/N] " response; case "$$response" in [yY][eE][sS]|[yY]) \
		if [ -f /etc/redhat-release ]; then \
			systemctl stop asterniclog; \
			systemctl disable asterniclog; \
			rm /etc/systemd/system/asterniclog.service; \
			rm /etc/sysconfig/asterniclog; \
			systemctl daemon-reload; \
			systemctl reset-failed; \
		elif [ -f /etc/debian_version ]; then \
			if [ $$(ps --no-headers -o comm 1) = "systemd" ]; then \
				systemctl stop asterniclog; \
				systemctl disable asterniclog; \
			else \
				/etc/init.d/asterniclog stop; \
				update-rc.d -f asterniclog remove; \
			fi ; \
		fi ; \
		rm -rf $(DESTDIR);  \
		rm -rf /usr/local/parselog; \
	;; \
	esac

asterniclog:
	@echo "Installing Asterniclog Service..."
	@if [ -d /var/log/asterisk ]; then \
	if [ -f /etc/debian_version ]; then \
		if [ $$(ps --no-headers -o comm 1) = "systemd" ]; then \
			$(INSTALL) -m 755 init/asterniclog.debian.service /etc/systemd/system/asterniclog.service; \
			systemctl daemon-reload; \
			systemctl enable asterniclog.service; \
		else \
			$(INSTALL) -m 755 init/asterniclog.debian /etc/init.d/asterniclog; \
			update-rc.d asterniclog defaults; \
		fi; \
		if [ ! -f /etc/default/asterniclog ]; then \
			$(INSTALL) -m 644 init/default.asterniclog /etc/default/asterniclog; \
		fi; \
	elif [ -f /etc/redhat-release ] || [ -f /etc/os-release ]; then \
		if [ $$(ps --no-headers -o comm 1) = "systemd" ]; then \
			$(INSTALL) -m 755 init/asterniclog.redhat.service /etc/systemd/system/asterniclog.service; \
			systemctl daemon-reload; \
			systemctl enable asterniclog.service; \
		else \
			$(INSTALL) -m 755 init/asterniclog.redhat /etc/rc.d/init.d/asterniclog; \
			${CHKCONFIG} --add asterniclog ; \
		fi; \
		if [ ! -f /etc/sysconfig/asterniclog ]; then \
			$(INSTALL) -m 644 init/sysconfig.asterniclog /etc/sysconfig/asterniclog; \
		fi; \
	else \
		echo "\033[1;33mAsterniclog Service not installed\033[0m: Could not detect Linux distro"; \
	fi; \
	else \
		echo -e "\033[1;33mAsterniclog Service not installed\033[0m: could not find /var/log/asterisk directory"; \
	fi
	@if [ ! -f /etc/freepbx.conf ]; then \
		if [ ! -f /etc/amportal.conf ]; then \
			if [ -f /etc/default/asterniclog ]; then \
				cat /etc/default/asterniclog  | sed 's/-c //' >/etc/default/asterniclognew; \
				mv -f /etc/default/asterniclognew /etc/default/asterniclog; \
 			fi; \
			if [ -f /etc/sysconfig/asterniclog ]; then \
				cat /etc/sysconfig/asterniclog  | sed 's/-c //' >/etc/sysconfig/asterniclognew; \
				mv -f /etc/sysconfig/asterniclognew /etc/sysconfig/asterniclog; \
 			fi; \
 		fi; \
	fi;

webfiles:
	@echo "Installing html files and directories..."
	@PHPVER=$$(php -v | head -n1 | cut -d ' ' -f2 | awk -F. '{print $$1}'); \
	if [ "$$PHPVER" = "5" ]; then \
		echo "Detected PHP version 5"; \
		HTMLDIR="html"; \
	elif [ "$$PHPVER" = "7" ]; then \
		echo "Detected PHP version 7"; \
		HTMLDIR="html"; \
	else \
		echo "Detected PHP version 8"; \
		HTMLDIR="html"; \
	fi; \
	if [ -x /var/www/html ]; then \
		if [ -f $(DESTDIR)/config.php ]; then \
			cp $(DESTDIR)/config.php $(DESTDIR)/config.orig.php; \
		fi; \
		if [ -f $(DESTDIR)/custom.php ]; then \
			cp $(DESTDIR)/custom.php $(DESTDIR)/custom.orig.php; \
		fi; \
		if [ -f $(DESTDIR)/css/custom.css ]; then \
			cp $(DESTDIR)/css/custom.css $(DESTDIR)/css/custom.orig.css; \
		fi; \
		if [ -f $(DESTDIR)/js/custom.js ]; then \
			cp $(DESTDIR)/js/custom.js $(DESTDIR)/js/custom.orig.js; \
		fi; \
		$(INSTALL) -d -m 755 $$HTMLDIR $(DESTDIR); \
		cp -rp $$HTMLDIR/* $(DESTDIR)/; \
		if [ -f $(DESTDIR)/config.orig.php ]; then \
			cp $(DESTDIR)/config.php $(DESTDIR)/config.new.php; \
			cp $(DESTDIR)/config.orig.php $(DESTDIR)/config.php; \
			rm $(DESTDIR)/config.orig.php; \
		fi; \
		if [ -f $(DESTDIR)/custom.orig.php ]; then \
			cp $(DESTDIR)/custom.php $(DESTDIR)/custom.new.php; \
			cp $(DESTDIR)/custom.orig.php $(DESTDIR)/custom.php; \
			rm $(DESTDIR)/custom.orig.php; \
		fi; \
		if [ -f $(DESTDIR)/css/custom.orig.css ]; then \
			rm $(DESTDIR)/css/custom.css; \
			cp $(DESTDIR)/css/custom.orig.css $(DESTDIR)/css/custom.css; \
			rm $(DESTDIR)/css/custom.orig.css; \
		fi; \
		if [ -f $(DESTDIR)/js/custom.orig.js ]; then \
			rm $(DESTDIR)/js/custom.js; \
			cp $(DESTDIR)/js/custom.orig.js $(DESTDIR)/js/custom.js; \
			rm $(DESTDIR)/js/custom.orig.js; \
		fi; \
		if [ -f /etc/amportal.conf ]; then \
			chown asterisk:asterisk $(DESTDIR); \
			chown -R asterisk:asterisk $(DESTDIR)/*; \
		else \
			if hash lsof 2>/dev/null; then \
			user=$$(lsof -i :80 | awk '{print $$3}' | uniq -c | sort -n | tail -n 1 | awk '{print $$2}'); \
			fi; \
			if [ "Z$$user" = "Z" ]; then \
			user=$$(lsof -i :443 | awk '{print $$3}' | uniq -c | sort -n | tail -n 1 | awk '{print $$2}'); \
			fi; \
			if [ "Z$$user" = "Z" ]; then \
			user=$$(getent passwd asterisk > /dev/null && if [ $$? -eq 0 ]; then echo 'asterisk'; fi); \
			fi; \
			if [ "Z$$user" = "Z" ]; then \
			user=$$(getent passwd apache > /dev/null && if [ $$? -eq 0 ]; then echo 'apache'; fi); \
			fi; \
			if [ "Z$$user" = "Z" ]; then \
			user=$$(getent passwd www-data > /dev/null && if [ $$? -eq 0 ]; then echo 'www-data'; fi); \
			fi; \
			chown $$user $(DESTDIR); \
			chown -R $$user $(DESTDIR)/*; \
		fi;\
		selinuxenabled 2>/dev/null; \
		if [ $$? -eq 0 ]; then \
			chcon -R -t httpd_sys_rw_content_t $(DESTDIR); \
			semanage fcontext -a -t httpd_sys_rw_content_t "$(DESTDIR)(/.*)?"; \
			setsebool -P telepathy_tcp_connect_generic_network_ports 1; \
			setsebool -P httpd_can_network_connect 1; \
			setsebool -P allow_user_mysql_connect 1; \
			setsebool -P httpd_can_network_connect_db 1; \
		fi \
	elif [ -x /var/www ]; then \
		$(INSTALL) -d -m 755 $$HTMLDIR $(DESTDIR); \
		$(INSTALL) --backup=numbered -m 644 $$HTMLDIR/config.php $(DESTDIR)/; \
		$(INSTALL) --backup=numbered -m 644 $$HTMLDIR/custom.php $(DESTDIR)/; \
		cp -rp $$HTMLDIR/* $(DESTDIR)/; \
	fi; 

ioncube:
	@php -v | grep ionCube >/dev/null; \
	if [ $$? -eq 1 ]; then \
		echo "Installing Ioncube Loader..."; \
		PHPVER=$$(php -v | head -n1 | cut -d ' ' -f2 | awk -F. '{print $$1"."$$2}'); \
		PHPSOFILE="zend_extension=/usr/local/ioncubeloader/ioncube_loader_lin_$$PHPVER.so"; \
		if [ -f /etc/debian_version ]; then \
			if [ -d /etc/php5/conf.d ]; then \
				if [ ! -f /etc/php5/conf.d/00-ioncube.ini ]; then \
					echo $$PHPSOFILE > /etc/php5/conf.d/00-ioncube.ini; \
				fi; \
			fi; \
			if [ -d /etc/php5/apache2/conf.d ]; then \
				if [ ! -f /etc/php5/apache2/conf.d/00-ioncube.ini ]; then \
					echo $$PHPSOFILE > /etc/php5/apache2/conf.d/00-ioncube.ini; \
				fi; \
			fi; \
			if [ -d /etc/php5/cli/conf.d ]; then \
				if [ ! -f /etc/php5/cli/conf.d/00-ioncube.ini ]; then \
					echo $$PHPSOFILE > /etc/php5/cli/conf.d/00-ioncube.ini; \
				fi; \
			fi; \
			if [ -d /etc/php/$$PHPVER/apache2/conf.d ]; then \
				if [ ! -f /etc/php/$$PHPVER/apache2/conf.d/00-ioncube.ini ]; then \
					echo $$PHPSOFILE > /etc/php/$$PHPVER/apache2/conf.d/00-ioncube.ini; \
				fi; \
			fi; \
			if [ -d /etc/php/$$PHPVER/cli/conf.d ]; then \
				if [ ! -f /etc/php/$$PHPVER/cli/conf.d/00-ioncube.ini ]; then \
					echo $$PHPSOFILE > /etc/php/$$PHPVER/cli/conf.d/00-ioncube.ini; \
				fi; \
			fi; \
		elif [ -f /etc/redhat-release ]; then \
			if [ ! -f /etc/php.d/00-ioncube.ini ]; then \
				echo $$PHPSOFILE > /etc/php.d/00-ioncube.ini; \
			fi; \
		fi; \
		uname -m | grep 64; \
		if [ $$? -eq 1 ]; then \
			echo "IONCUBE 32 bits"; \
			wget -qnc http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_lin_x86.tar.gz; \
			tar zxvf ioncube_loaders_lin_x86.tar.gz >/dev/null; \
			mkdir -p /usr/local/ioncubeloader; \
			cp -rp ioncube/*.so /usr/local/ioncubeloader/; \
		else \
			echo "IONCUBE 64 bits"; \
			wget -qnc http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.gz; \
			tar zxvf ioncube_loaders_lin_x86-64.tar.gz >/dev/null; \
			mkdir -p /usr/local/ioncubeloader; \
			cp -rp ioncube/*.so /usr/local/ioncubeloader/; \
		fi; \
	else \
		echo "Ioncube already installed..."; \
	fi; 
mysql:
	@echo "Creating MySQL database and tables..."
	@if [ -f /etc/elastix.conf ]; then \
		echo "Using MySQL root password from elastix.conf"; \
		AMIADMINPW=$$(cat /etc/elastix.conf | grep amiadminpwd | cut -d= -f2); \
		sed -i "s/amp111/$${AMIADMINPW}/" html/config.php; \
		NEWMYSQLROOT=$$(cat /etc/elastix.conf | grep mysqlrootpwd | cut -d= -f2); \
		export MYSQLROOT=$${NEWMYSQLROOT}; \
		export PARAMMYSQLPWD="-p$${NEWMYSQLROOT}"; \
		${MYSQL} -sss -u root $${PARAMMYSQLPWD} -e "SELECT ''"; \
		if [ $$? -ne 0 ]; then \
			echo "Bad MySQL password in elastix.conf. Abort!"; \
			exit 1; \
		fi; \
	fi; \
	if [ -f /etc/issabel.conf ]; then \
		echo "Using MySQL root password from issabel.conf"; \
		AMIADMINPW=$$(cat /etc/issabel.conf | grep amiadminpwd | cut -d= -f2); \
		sed -i "s/amp111/$${AMIADMINPW}/" html/config.php; \
		NEWMYSQLROOT=$$(cat /etc/issabel.conf | grep mysqlrootpwd | cut -d= -f2); \
		export MYSQLROOT=$${NEWMYSQLROOT}; \
		export PARAMMYSQLPWD="-p$${NEWMYSQLROOT}"; \
		${MYSQL} -sss -u root $${PARAMMYSQLPWD} -e "SELECT ''" >/dev/null; \
 		if [ $$? -ne 0 ]; then \
			echo "Bad MySQL password in issabel.conf. Abort!"; \
			exit 1; \
		fi; \
	fi; \
	if [ "$${MYSQLROOT}" = "" ]; then \
		export x=1; \
		while [ $$x -le 3 ]; do \
			x=$$(( $$x + 1 )); \
			read -p "Please enter the MySQL root password: " MYSQLROOT; \
			if [ "$${MYSQLROOT}" = "" ]; then \
				export PARAMMYSQLPWD=""; \
			else \
				export PARAMMYSQLPWD="-p$${MYSQLROOT}"; \
			fi; \
			${MYSQL} -sss -u root $${PARAMMYSQLPWD} -e "SELECT ''"; \
			if [ $$? -ne 0 ]; then \
				echo "Bad MySQL password!"; \
			else \
				echo "Correct!"; \
				x=$$(( $$x + 100 )); \
			fi; \
		done; \
		if [ $${x} -lt 90 ]; then \
			echo "Bad Password. Abort"; \
			exit 1; \
		fi; \
	fi; \
	echo "Creating qstats database"; \
	${MYSQL} -sss -u root $${PARAMMYSQLPWD} -e "CREATE DATABASE qstats DEFAULT CHARACTER SET utf8" 2>/dev/null; \
	if [ $$? -eq 0 ]; then \
		${MYSQL} -u root $${PARAMMYSQLPWD} qstats < sql/mysql-tables.sql; \
		${MYSQL} -u root $${PARAMMYSQLPWD} qstats < sql/trigger.sql; \
		res=$$(${MYSQL} -sss -u root $${PARAMMYSQLPWD} -e "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'qstatsUser')"); \
		if [ $$res -eq 0 ]; then \
		${MYSQL} -u root $${PARAMMYSQLPWD} -e "CREATE USER qstatsUser@localhost identified by 'qstatsPassw0rd'";\
		fi; \
		${MYSQL} -u root $${PARAMMYSQLPWD} -e "GRANT all on qstats.* to qstatsUser@localhost";\
		${MYSQL} -u root $${PARAMMYSQLPWD} -e "GRANT super on *.* to qstatsUser@localhost";\
		${MYSQL} -B -u root $${PARAMMYSQLPWD} -e "show databases like 'asterisk'" | grep asterisk >/dev/null; \
		if [ $$? -eq 0 ]; then \
			if [ -f /etc/freepbx/freepbx.conf ] || [ -f /etc/issabel.conf ] || [ -f /etc/elastix.conf ] || [ -f /etc/amportal.conf ]; then \
				${MYSQL} -u root $${PARAMMYSQLPWD} -e "GRANT SELECT on asterisk.queues_config to qstatsUser@localhost identified by 'qstatsPassw0rd'";\
			fi; \
		fi; \
	else \
		echo -e "\033[31mThe qstats database already exists.\e[0m"; \
		echo -e "If you want to upgrade an existing installation, use the command: \033[32mmake upgrade\e[0m"; \
		exit 1; \
	fi; \
	echo $${PARAMMYSQLPWD} >/tmp/mpwd 

perlfinal:
	@if [ -f /etc/redhat-release ]; then \
		perl -MDBI -e 1 2>/dev/null; \
		if [ $$? -ne 0 ]; then \
			yum -y install perl-DBD-MySQL; \
		fi; \
		yum -q -y install perl-Time-HiRes ; \
		yum -q -y install at ; \
		grep Sangoma /etc/redhat-release ; \
		if [ $$? -eq 0 ]; then \
			PHPVER=$$(php -v | head -n1 | cut -d ' ' -f2 | awk -F. '{print $$1}'); \
			if [ "$${PHPVER}" = "5" ]; then \
				yum -q -y install php56w-pecl-apcu.x86_64 ; \
			fi; \
		else \
			yum -q -y install php-pecl-apcu ; \
		fi \
	fi
	@if [ -f /etc/redhat-release ]; then \
		service httpd restart; \
		if [ -d /var/log/asterisk ]; then \
		if [ $$(ps --no-headers -o comm 1) = "systemd" ]; then \
			systemctl start asterniclog.service; \
		else \
			service asterniclog start; \
		fi; \
		fi; \
	fi
	@if [ -f /etc/debian_version ]; then \
		apt install php-xml \
		apt install php-apcu \
		perl -MDBI -e 1 2>/dev/null; \
		if [ $$? -ne 0 ]; then \
			apt-get install libdbi-perl; \
		fi; \
		perl -e 'use DBI; my $$dbh = DBI->connect("dbi:mysql:dbname=mysql","root","", {RaiseError=>1});' 2>/dev/null; \
		if [ $$? -eq 2 ]; then \
			apt-get install libdbd-mysql-perl; \
		fi; \
		/etc/init.d/apache2 restart; \
		if [ -d /var/log/asterisk ]; then \
		if [ $$(ps --no-headers -o comm 1) = "systemd" ]; then \
			systemctl start asterniclog.service; \
		else \
			/etc/init.d/asterniclog start; \
		fi; \
		fi; \
	fi
	@if [ -f /etc/redhat-release ]; then \
		echo ""; \
		echo "Done!"; \
		echo ""; \
	else \
		echo ""; \
		echo "Done!"; \
		echo ""; \
	fi;

pdf:
	@osversion=`awk -F= '/^VERSION_ID/{print $$2}' /etc/os-release | sed 's/"//g' | cut -d\. -f 1`; \
	if [ -f /etc/debian_version ]; then \
		dpkg -s wkhtmltox | grep Status | grep -v deinstall >/dev/null; \
		if [ $$? -eq 0 ]; then \
			echo "wkhtmltopdf already installed"; \
		else \
		osvendor=`awk -F= '/^ID=/{print $$2}' /etc/os-release | sed 's/"//'g`; \
		if [ "$$osvendor" = "ubuntu" ]; then \
		        if [ "$$osversion" = "20" ]; then \
				wget download.asternic.net/wkhtmltopdf/wkhtmltox_0.12.6-1.focal_amd64.deb -O /tmp/wkhtmltopdf.deb; \
				dpkg -i /tmp/wkhtmltopdf.deb 2>/dev/null; \
				apt-get -y -f install; \
			elif [ "$$osversion" = "18" ]; then \
				wget download.asternic.net/wkhtmltopdf/wkhtmltox_0.12.6-1.bionic_amd64.deb -O /tmp/wkhtmltopdf.deb; \
				dpkg -i /tmp/wkhtmltopdf.deb 2>/dev/null; \
				apt-get -y -f install; \
			elif [ "$$osversion" = "16" ]; then \
				wget download.asternic.net/wkhtmltopdf/wkhtmltox_0.12.6-1.xenial_amd64.deb -O /tmp/wkhtmltopdf.deb; \
				dpkg -i /tmp/wkhtmltopdf.deb 2>/dev/null; \
				apt-get -y -f install; \
                        else \
                                echo "I do not have wkhtmltopdf available for $$osvendor $$osversion"; \
                        fi; \
		else \
		        if [ "$$osversion" = "11" ]; then \
				wget download.asternic.net/wkhtmltopdf/wkhtmltox_0.12.6.1-2.bullseye_amd64.deb -O /tmp/wkhtmltopdf.deb; \
				dpkg -i /tmp/wkhtmltopdf.deb 2>/dev/null; \
				apt-get -y -f install; \
			elif [ "$$osversion" = "10" ]; then \
				wget download.asternic.net/wkhtmltopdf/wkhtmltox_0.12.6-1.buster_amd64.deb -O /tmp/wkhtmltopdf.deb; \
				dpkg -i /tmp/wkhtmltopdf.deb 2>/dev/null; \
				apt-get -y -f install; \
			elif [ "$$osversion" = "9" ]; then \
				wget download.asternic.net/wkhtmltopdf/wkhtmltox_0.12.6-1.stretch_amd64.deb -O /tmp/wkhtmltopdf.deb; \
				dpkg -i /tmp/wkhtmltopdf.deb 2>/dev/null; \
				apt-get -y -f install; \
                        else \
                                echo "I do not have wkhtmltopdf available for $$osvendor $$osversion"; \
                        fi; \
		fi; \
		fi; \
        elif [ -f /etc/redhat-release ] || [ -f /etc/os-release ]; then \
		rpm -q wkhtmltox &>/dev/null; \
		if [ $$? -eq 0 ]; then \
			echo "wkhtmltopdf already installed"; \
		else \
			osvendor='centos';	\
        	        if [ "$$osversion" = "8" ]; then \
				wget download.asternic.net/wkhtmltopdf/wkhtmltox-0.12.6-1.centos8.x86_64.rpm -O /tmp/wkhtmltopdf.rpm; \
				yum -y localinstall /tmp/wkhtmltopdf.rpm; \
	                elif [ "$$osversion" = "7" ]; then \
				wget download.asternic.net/wkhtmltopdf/wkhtmltox-0.12.6-1.centos7.x86_64.rpm -O /tmp/wkhtmltopdf.rpm; \
				yum -y localinstall /tmp/wkhtmltopdf.rpm; \
	                elif [ "$$osversion" = "6" ]; then \
				wget download.asternic.net/wkhtmltopdf/wkhtmltox-0.12.6-1.centos6.x86_64.rpm -O /tmp/wkhtmltopdf.rpm; \
				yum -y localinstall /tmp/wkhtmltopdf.rpm; \
	                else \
        	                 echo "I do not have wkhtmltopdf available for $$osvendor $$osversion"; \
                	fi; \
                fi; \
	fi

