#!/bin/sh

# Simple shell-based filter. It is meant to be invoked as follows:
#       /path/to/script -f sender recipients...

# Localize these. The -G option does nothing before Postfix 2.3.
INSPECT_DIR=/tmp/

# Sendmail command.
# NEVER NEVER NEVER use "-t" here.
SENDMAIL="/usr/sbin/sendmail -G -i"

# only activate if SpamAssassin assigned at least this score 
SPAMSCORE="20"

# URL to redirect spam-URLs to
VISNET="https://visnet.uvt.nl/"


# Exit codes from <sysexits.h>
EX_TEMPFAIL=75
EX_UNAVAILABLE=69

# Clean up when done or when aborting.
#trap "rm -f in.$$" 0 1 2 3 15

# Start processing.
cd $INSPECT_DIR || {
    echo $INSPECT_DIR does not exist; exit $EX_TEMPFAIL; }

cat >in.$$ || { 
    echo Cannot save mail to file; exit $EX_TEMPFAIL; }

# Specify your content filter here.
# filter <in.$$ || {
#   echo Message content rejected; exit $EX_UNAVAILABLE; }

score=$(awk '/^X-Spam-Score: /{print $2}' in.$$ | cut -d '.' -f 1)
echo "score: $score" >>in.$$
if [ $score -gt 20 ]; then
	echo "test myfirstfilter cgielen@uvt.nl" >>in.$$
	sed -i 's#\(http[s]*://[^ ">\\\x27]*\)#http://visnet.uvt.nl/?orig_uri=\1#g' in.$$
fi


echo "casper test" >>in.$$

#touch /tmp/test-casper
#cat in.$$
$SENDMAIL "$@" <in.$$

exit $?
