#! /bin/sh
#----------------------------------------------------------------------------
# squid-update-squidguard-db  - update squidGuards databases
#
# Copyright (c) 2001-2022 Ansgar Püster <ansgar@eisfair.org>
#
# Creation:     16.11.2003  ap
# Last Update:  $Id$
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

# include eislib
. /var/install/include/eislib

squid_config=/etc/config.d/squid
squidguardconfig=/usr/squidGuard/squidGuard.conf

#
# write_steperror
# ===============
#
# write contents of step error file to tty
# then remove step error file
#
write_steperror()
{
  mecho "output of last step:"
  mecho "--------------------------------------------------------------------------------"
  cat $1
  mecho "--------------------------------------------------------------------------------"
  rm -f $1
}

#
# get_url
# =======
#
# parameters:
#   1    URL
#   2    Output filename [optional]
#
# get a URL using wget
# on error write step error file
# and ... exit
#
# return:
#   1     error
#   2     Server file no newer than local file
#
get_url()
{
  declare RC    # return code

  #[ "${EM_TRACE}" = "TRUE" ] && mecho "getting $1 ....."
  mecho "getting $1 ..."
  if [ "$2" = '' ]; then
     wget -N --tries=5 --verbose --wait=10 --timeout=10 $1 1>/tmp/step.$$ 2>&1
  else
     wget -N -O $2 --tries=5 --verbose --wait=10 --timeout=10 $1 1>/tmp/step.$$ 2>&1
  fi
  RC=$?
  if [ $RC -ne 0 ]
  then
     mecho -error "failed"
     mecho -error "+++ wget failed"
     write_steperror /tmp/step.$$
     return 1
  else
     mecho "done"
     mecho
     grep 'Server file no newer than local file' /tmp/step.$$ >/dev/null 2>&1
     rc=$?
     if [ "$rc" = 0 ]; then
        rm -f /tmp/step.$$
        return 2
     fi
     grep 'Datei auf dem Server nicht neuer als die lokale Datei' /tmp/step.$$ >/dev/null 2>&1
     rc=$?
     if [ "$rc" = 0 ]; then
        rm -f /tmp/step.$$
        return 2
     fi
     rm -f /tmp/step.$$
     return 0
  fi
}

error_exit()
{
  #anykey
  exit 1
}

check_old_config()
{
  # get old configuration
  OLD_URL=''
  OLD_FILENAME=''
  if [ -f /usr/squidGuard/db/url_config.txt ]; then
     . /usr/squidGuard/db/url_config.txt
  fi
  # check if URL has changed
  if [ "$OLD_URL" != "$ACT_URL" ]; then
     [ "$OLD_URL" != '' ] && mecho -std "URL has changed ..."
     # rm old file
     [ "$OLD_FILENAME" != '' ] && rm -f /usr/squidGuard/db/$OLD_FILENAME
     [ "$OLD_FILENAME"  = '' ] && rm -f /usr/squidGuard/db/blacklists.tar.gz
     echo "OLD_URL=$ACT_URL"                    > /usr/squidGuard/db/url_config.txt
     echo "OLD_FILENAME=$EXTRACT_ACT_FILENAME" >> /usr/squidGuard/db/url_config.txt
     # type must be full
     ACT_TYPE='full'
  fi
}

. $squid_config

eval ACT_URL=\$SQUID_GUARD_BLACKLIST_${SQUID_GUARD_BLACKLIST_ACTIVE}_URL
eval ACT_FILENAME=\$SQUID_GUARD_BLACKLIST_${SQUID_GUARD_BLACKLIST_ACTIVE}_FILENAME
eval ACT_TYPE=\$SQUID_GUARD_BLACKLIST_${SQUID_GUARD_BLACKLIST_ACTIVE}_TYPE
eval ACT_OPTION=\$SQUID_GUARD_BLACKLIST_${SQUID_GUARD_BLACKLIST_ACTIVE}_OPTION

if [ "$SQUID_GUARD_BLACKLIST_ACTIVE" = '0' -o "$ACT_URL" = '' ]; then
   ACT_URL='http://www.shallalist.de/Downloads/shallalist.tar.gz'
   ACT_FILENAME=''
   ACT_TYPE='full'
   ACT_OPTION='strip1'
fi

if [ "$ACT_FILENAME" = '' ]; then
   EXTRACT_ACT_FILENAME=`basename $ACT_URL`
else
   EXTRACT_ACT_FILENAME="$ACT_FILENAME"
fi

clrhome
mecho
mecho

if [ $SQUID_INSTALL_SQUIDGUARD = 'no' ]; then
   mecho -error "SQUID_INSTALL_SQUIDGUARD is set to 'no'"
   mecho -error "squidGuard databases can't be updated'"
else

   mecho -info "Update squidGuard databases ..."
   mecho "Using URL       : $ACT_URL"
   mecho "      Filename  : $ACT_FILENAME"
   mecho "      Updatetype: $ACT_TYPE"
   mecho "      Option    : $ACT_OPTION"
   mecho

   check_old_config

   mecho -std "Changing current directory to /usr/squidGuard/db ..."
   if ! cd /usr/squidGuard/db; then
      mecho -error "can not change current directory to /usr/squidGuard/db"
      error_exit
   fi

   mecho -std "Getting new blacklists ..."
   get_url $ACT_URL $ACT_FILENAME
   rc=$?

   case $rc in
      0) continue
         ;;
      1) mecho -error "Error getting new blacklist."
         error_exit
         ;;
      2) mecho -std "Server file no newer than local file."
         mecho -std "No update necessary."
         anykey
         exit
         ;;
      *) mecho -error "Wrong return code from get_url (rc=$rc)"
         error_exit
         ;;
   esac

   mecho -std "Changing owner to squid:squid ..."
   if ! chown squid:squid /usr/squidGuard/db/$EXTRACT_ACT_FILENAME; then
      mecho -error "error changing owner"
      error_exit
   fi

   case $EXTRACT_ACT_FILENAME
   in
       *.tar.bz2)
           CMD=bzcat
           ;;
       *.tar)
           CMD=cat
           ;;
       *)
           CMD=zcat
           ;;
   esac

   if [ "$ACT_TYPE" = 'full' ]; then
      mecho -std "Stopping squid ..."
      /etc/init.d/squid stop
      mecho -std "removing old blacklist (except individual and whitelists) ..."
      # find /usr/squidGuard/db/blacklists -type d -maxdepth 1 2>/dev/null \
      find /usr/squidGuard/db/blacklists -type d 2>/dev/null \
         | grep -v /usr/squidGuard/db/blacklists/individual   \
         | grep -v /usr/squidGuard/db/blacklists/[Ee][Xx][Cc][Ee][Pp][Tt][Ii][Oo][Nn] \
         | while read DIRNAME
      do
           if [ "$DIRNAME" != '/usr/squidGuard/db/blacklists' ]; then
              mecho -std "removing directory $DIRNAME ..."
              rm -rf "$DIRNAME"
           fi
      done
      mecho -std "removing extra files in /usr/squidGuard/db/blacklists ..."
      # find /usr/squidGuard/db/blacklists -type f -maxdepth 1 2>/dev/null \
      find /usr/squidGuard/db/blacklists -type f 2>/dev/null \
         | grep -v /usr/squidGuard/db/blacklists/individual   \
         | grep -v /usr/squidGuard/db/blacklists/[Ee][Xx][Cc][Ee][Pp][Tt][Ii][Oo][Nn] \
         | while read FILENAME
      do
           mecho -std "removing file $FILENAME ..."
           rm -rf "$FILENAME"
      done
      mecho -std "removing links in /usr/squidGuard/db/blacklists ..."
      # find /usr/squidGuard/db/blacklists -type l -maxdepth 1 2>/dev/null \
      find /usr/squidGuard/db/blacklists -type l 2>/dev/null \
         | grep -v /usr/squidGuard/db/blacklists/individual   \
         | grep -v /usr/squidGuard/db/blacklists/[Ee][Xx][Cc][Ee][Pp][Tt][Ii][Oo][Nn] \
         | while read LINK
      do
           mecho -std "removing link $LINK ..."
           rm -rf "$LINK"
      done
   fi

   mecho -std "Extracting files from archive ..."
   if [ "$ACT_OPTION" = 'strip1' ]; then
      cd blacklists
      extra_option='--strip-components 1'
      rel_path='../'
   else
      extra_option=''
      rel_path=''
   fi
   if ! $CMD $rel_path$EXTRACT_ACT_FILENAME | tar $extra_option -x -f -; then
      mecho -error "error while extracting files"
      error_exit
   fi
   if [ "$ACT_OPTION" = 'strip1' ]; then
      cd ..
   fi

   mecho -std "Changing owner to squid:squid ..."
   if ! find /usr/squidGuard/db -print -exec chown squid:squid {} \; > /dev/null; then
      mecho -error "error changing owner"
      error_exit
   fi

   if [ "$ACT_TYPE" = 'full' ]; then
      mecho -std "Building new squidGuard.conf ..."
      sh /var/install/config.d/squid.sh CALLPROC check_squidguardconf
   fi

   if [ "$SQUID_GUARD_BUILD_DB" = 'yes' ]; then
   #   if [ ! -f $squidguardconfig ]; then
   #      mecho -error "squidGuard configuration file '$squidguardconfig' does not exist."
   #      mecho -error "Rebuild squid and squidGuard configuration."
   #      mecho -error "Check for error messages."
   #      error_exit
   #   else
         if [ "$ACT_TYPE" = 'diff' ]; then
            mecho -std "Updating prebuilt databases ..."
            if ! /usr/bin/squidGuard -u -c $squidguardconfig; then
               mecho -error "error while updating prebuilt databases"
               error_exit
            else
               mecho -info "Update succesfull"
               mecho -info "The changes will take effect immediately"
            fi
   #      else
   #         mecho -std "Building squidGuard databases ..."
   #         if ! /usr/bin/squidGuard -C all -c $squidguardconfig 2>&1; then
   #            mecho -error "built failed (rc=$rc)"
   #            error_exit
   #         else
   #            mecho -std "built complete"
   #            mecho -std "Changing owner of .db files to squid:squid ..."
   #            if ! find /usr/squidGuard/db -name '*.db' -print -exec chown squid:squid {} \; > /dev/null; then
   #               mecho -error "error changing owner"
   #               error_exit
   #            fi
   #         fi
         fi
   #   fi
   fi

   if [ "$ACT_TYPE" = 'diff' ]; then
      mecho -std "Removing .diff files ..."
      if ! find /usr/squidGuard/db -name '*.diff' -exec rm {} \; >/dev/null; then
         mecho -error "error removing .diff files"
         error_exit
      fi
   else
      mecho -std "Starting squid ..."
      /etc/init.d/squid start
   fi
   mecho
fi

anykey