#!/bin/bash

# esetsefs-wrapper --	invoke esets efs for use with mailscanner
#
#   MailScanner - SMTP Email Processor
#   Copyright (C) 2021 MailScanner Team <https://mailscanner.info>
#
#   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.
#
#   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#      https://www.mailscanner.info
#
#

# Date Format (localization)
# If your locale does not define date as MM/DD/YYYY, then change the following to suit
DATEORDER='%m %d %Y'
DATESEPARATOR='/'

PackageDir=$1
shift
Prog=odscan
Log=lslog
Epoch=$(date +%s)

if [ "x$1" = "x-IsItInstalled" ]; then
  [ -x ${PackageDir}/$Prog ] && exit 0
  exit 1
fi

sudo ${PackageDir}/$Prog "$@"
if [ $? -eq 50 -o $? -eq 1 ]; then
  # Threat(s) found
  LogFile=$(mktemp) || { echo "$0: Cannot create temporary file" >&2; exit 1; }
  umask 077
  rm -f $LogFile
  # Grab just the end of the log to save on parsing
  sudo ${PackageDir}/$Log -c -s --with-log-name | tail -n1000 >$LogFile 2>&1
  # Output detections in current path on or after timestamp
  Dir=$@
  oldIFS="$IFS"
  IFS=''
  while read -r p || [ -n "$p" ]
  do
    if [[ $p =~ ^[0-9] ]]; then
      Date=$(echo $p | awk -F',' '{print $1}')
      # Localization
      if [[ "$DATEORDER" != "%m %d %Y" && "$DATESEPARATOR" != "/" ]]; then
        DATEONLY=$(echo $Date | awk -F' ' '{print $1}')
        TIMEONLY=$(echo $Date | awk -F' ' '{print $2}')
        pos=1
        # Reset IFS for this section
        IFS="$oldIFS"
        for i in $DATEORDER; do
          if [[ "$i" == "%m" ]]; then
            MONTH=$(echo $DATEONLY | awk -F"$DATESEPARATOR" "{print \$$pos}")
          elif [[ "$i" == "%d" ]]; then
            DAY=$(echo $DATEONLY | awk -F"$DATESEPARATOR" "{print \$$pos}")
          elif [[ "$i" == "%Y" ]]; then
            YEAR=$(echo $DATEONLY | awk -F"$DATESEPARATOR" "{print \$$pos}")
          fi
          pos=$(($pos + 1))
        done
        IFS=''
        Date="$MONTH/$DAY/$YEAR $TIMEONLY"
      fi

      Epoch2=$(date --date="$Date" +%s)
      if [ $Epoch2 -ge $Epoch ]; then
        # Grab detections and filter to scan directory
        logID=$(echo ${p##*,} | tr -d '\r')
        sudo ${PackageDir}/$Log -c --ods-detections=$logID | grep ${Dir##*\ } 2>&1
      fi
    fi
  done < $LogFile
  IFS=$oldIFS
  rm -f $LogFile
elif [ $? -eq 100 ]; then
  # Scan failed
  exit 1
fi

exit 0
