#!/bin/sh
#
# $FreeBSD: ports/mail/p5-Mail-SpamAssassin/files/pkg-install.in,v 1.2 2012/01/02 13:27:09 scheidell Exp $
#

USER=spamd
GROUP=spamd
INSTALL="install"
PREFIX=/usr/local

if [ "$2" = "PRE-INSTALL" ];then exit; fi

if [ -z "${INSTALL}" ];then
echo "report this problem to scheidell@freebsd.org, $*"
exit 1
fi

if [ "$2" = "POST-INSTALL" ];then 
ask() {
        local question default answer

        question=$1
        default=$2
        if [ -z "${PACKAGE_BUILDING}" -a -z "${BATCH}" ]; then
                read -t120 -p "${question} [${default}]? " answer
        fi
        echo ${answer:-${default}}
}

yesno() {
        local question default answer

        question=$1
        default=$2
        while :; do
                answer=$(ask "${question}" "${default}")
                case "${answer}" in
                [Yy]*)  return 0;;
                [Nn]*)  return 1;;
                esac
                echo "Please answer yes or no."
        done
}

    # Create pid directory

    ${INSTALL} -d -o ${USER} -g ${GROUP} /var/run/spamd
    ${PREFIX}/bin/spamassassin -x -L --lint
    if [ ${?} -ne 0 ];then
        echo "
*******************************************************
* _  _  _ _______  ______ __   _ _____ __   _  ______ *
* |  |  | |_____| |_____/ | \  |   |   | \  | |  ____ *
* |__|__| |     | |    \_ |  \_| __|__ |  \_| |_____| *
*                                                     *
*******************************************************
*    You must install rules before starting spamd!    *
*******************************************************"
        if [ -z "${PACKAGE_BUILDING}" -a -z "${BATCH}" ]; then
           if yesno "Do you wish to run sa-update to fetch new rules" "N";then
              ${PREFIX}/bin/sa-update || true
	   else
              echo ""
           fi
           ${PREFIX}/bin/spamassassin -x -L --lint
           if [ ${?} -eq 0 ] && grep '^load.*Rule2XSBody' ${PREFIX}/etc/mail/spamassassin/v320.pre > /dev/null ;then
              if yesno "Do you wish to compile rules with re2c (will take a long time)" "N";then
                ${PREFIX}/bin/sa-compile || true
        fi
    fi
       fi
    fi

    exit 0
fi # post-install

exit 0
