#!/bin/sh
#
#       add-filter-to-squid  -  add squidGuard filter to squid config.
#
#       Copyright 2011-2024 Joachim Wiedorn <ad_debian at joonet.de>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License version 2 as
#       published by the Free Software Foundation.
#       
#       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 General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

set -e

GUARDCONF=/etc/squidguard/squidGuard.conf
if test ! -f ${GUARDCONF}; then
    echo "File squidGuard.conf not found - abort!"
    exit 1
fi

if test -f /etc/squid/squid.conf; then
    SQUIDCONF=/etc/squid/squid.conf
else
    echo "File squid.conf not found - abort!" 
    exit 1
fi

if [ $(grep -c 'FILTER START' ${SQUIDCONF}) -gt 0 ]; then
    echo "Lines for squidguard already inside squid.conf - exiting."
    exit 0
fi

cp -p ${SQUIDCONF} ${SQUIDCONF}_tmp

echo "
### FILTER START ###
url_rewrite_program  /usr/bin/squidGuard -c ${GUARDCONF}
### FILTER END ###
"       >> ${SQUIDCONF}

echo "Lines for squidguard filter added to squid.conf."

