File pihole-FTL-service of Package pihole

#!/bin/bash

# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# Copyright (C) 2018 Malcolm Lewis <malcolmlewis@opensuse.org>

# Description: Script to start pihole-FTL daemon
# Version: 0.0.2
# Date: 18th September, 2018

# NOTE: Debug (1) is only used with this script, not with the systemd service.
MY_DEBUG=0

case "$1" in
    start)
	test ! -f /run/pihole-FTL.pid || { echo "pihole-FTL is already running...";
	   if [ "$1" = "stop" ]; then exit 0;
	   else exit 6; fi; }
	# Only used for initial pihole FTL configuration.
	if [[ ! -f /etc/pihole/.pihole_configured ]]; then
	   echo "Initial pihole-FTL configuration"
	   touch /var/log/pihole.log /var/log/pihole-FTL.log
	   chown pihole:pihole /var/log/pihole.log /var/log/pihole-FTL.log /etc/pihole/pihole-FTL.conf
	   ln -s /etc/sysconfig/setupVars /etc/pihole/setupVars.conf
	   # Source the setupVars.conf for the DNS servers and interface to use
	   # as configured by YaST System -> pihole -> setupVars.conf
	   . /etc/pihole/setupVars.conf
	   # Add Ethernet interface and DNS servers to use sourced from setupVars.conf.
   	   sed -i "s/@INT@/$PIHOLE_INTERFACE/" /etc/dnsmasq.d/01-pihole.conf
	   sed -i "s/@DNS1@/$PIHOLE_DNS_1/" /etc/dnsmasq.d/01-pihole.conf
	   sed -i "s/@DNS2@/$PIHOLE_DNS_2/" /etc/dnsmasq.d/01-pihole.conf
	   # Need to be root to create database and let is run for a bit.
	   /usr/bin/pihole-FTL
	   sleep 3
	   PIHOLE_PID=`pidof pihole-FTL`
	   /bin/kill -n 15 $PIHOLE_PID
	   chown pihole:pihole /etc/pihole/pihole-FTL.db
       touch /etc/pihole/.pihole_configured
	   echo "Initial pihole-FTL configuration completed..."
	fi
	# Some runtime configuration required.
	mkdir -p /run/pihole
	touch /run/pihole-FTL.pid /run/pihole-FTL.port
	# Double check /etc/pihole ownership as database will go read only if not!
	# Error seen: "save_to_DB() - SQL error (8): attempt to write a readonly database"
	chown pihole:pihole /etc/pihole /run/pihole /run/pihole-FTL.pid /run/pihole-FTL.port /var/log/pihole.log
	# Need to remove FTL.sock on start as program doesn't clean up.
	rm /run/pihole/FTL.sock 2> /dev/null
	echo "nameserver 127.0.0.1" | /usr/sbin/resolvconf -a lo.piholeFTL
	if [ $MY_DEBUG == 1 ]; then
	   su -s /bin/sh -c "/usr/bin/pihole-FTL debug" pihole
	else
	   su -s /bin/sh -c "/usr/bin/pihole-FTL" pihole
	fi
        ;;
    stop)
	test -f /run/pihole-FTL.pid || { echo "pihole-FTL is already stopped...";
	if [ "$1" = "stop" ]; then exit 0;
	   else exit 6; fi; }
	PIHOLE_PID=`pidof pihole-FTL`
	su -s /bin/sh -c "/bin/kill -s SIGTERM $PIHOLE_PID" pihole
	/usr/sbin/resolvconf -d lo.piholeFTL
	rm /run/pihole-FTL.pid
	echo "FTL stopped!"
	;;
    reconfigure)
	if [[ -f /etc/pihole/.pihole_configured ]]; then
	   echo "Removing pihole-FTL configuration...(Experimental)"
	   rm /etc/pihole/pihole-FTL.db
	   rm /etc/pihole/setupVars.conf
	   rm /etc/pihole/.pihole_configured
	fi
	;;
       *)
	echo "Usage: $0 {start|stop|reconfigure}"
	exit 1
	;;
esac
openSUSE Build Service is sponsored by