#!/bin/sh
#


function interrupt {

	stty echo
	clear
	echo
	echo "${bold}Installation script was aborted. The process was NOT completed successfully.${norm}"
	echo
	exit 1
}

case `uname -r|cut -d. -f1` in
	8)
		OS_TYPE="TIGER"
		;;
	7)
		OS_TYPE="PANTHER"
		;;
	*)
		OS_TYPE="UNKNOWN"
		;;
esac


case $TERM in
	#   for the most important terminal types we directly know the sequences
	xterm|xterm*|vt220|vt220*)
		bold=`awk 'BEGIN { printf("%c%c%c%c", 27, 91, 49, 109); }' </dev/null 2>/dev/null`
		norm=`awk 'BEGIN { printf("%c%c%c", 27, 91, 109); }' </dev/null 2>/dev/null`
		;;
	vt100|vt100*|cygwin)
		bold=`awk 'BEGIN { printf("%c%c%c%c%c%c", 27, 91, 49, 109, 0, 0); }' </dev/null 2>/dev/null`
		norm=`awk 'BEGIN { printf("%c%c%c%c%c", 27, 91, 109, 0, 0); }' </dev/null 2>/dev/null`
		;;
esac

if [ `whoami` != "root" ]; then
cat <<X

$0 must be run as ${bold}root${norm} user.

X
exit
fi

color_1=`echo -en "\e[37;40m"`
color_2=`echo -en "\e[37;41m"`
trap interrupt 2 15

Project=SpamAV
CUR_PATH=`pwd`
BS_PATH=/var/db/.BuildSmartDone
method=""

BS_VER="0.1.8"
file_ext=".tar.gz"
clamav_file="clamav"
db_file="db-4.3.28"
gmp_file="gmp-4.1.4"


make_group1=true
make_group2=true
make_user1=true
make_user2=true
cmd_group1="making"
cmd_group2="making"
cmd_user1="making"
cmd_user2="making"
HAVE_BUILD=false
HAVE_UG=false
HAVE_ENV=false
HAVE_CLAMAV=false

clear
cat <<X
${color_1}                                                              
  +--------------------------------------------------------+  
  |                                                        |  
  |            ${color_2} Welcome to BuildSmart-${Project} ${color_1}              |  
  |                                                        |  
  |     Your automated building environment generator.     |  
  |                                                        |  
  |                     Version ${BS_VER}                      |  
  |                                                        |  
  | Copyright 2005 Dale R. Walsh <dale@daleenterprise.com> |  
  |                                                        |  
  +--------------------------------------------------------+  
X
tput sgr0

if [ ! -d ${BS_PATH} ]; then mkdir -p ${BS_PATH};fi

if [ -f ${BS_PATH}/.${Project}Done ]; then
eval `grep 'HAVE_' ${BS_PATH}/.${Project}Done`
fi
case "$HAVE_BUILD" in
  *done)
    do_build=false
    HAVE_BUILD=false
    method="(re)"
    ;;
  *)
    do_build=true
    HAVE_BUILD=true
    ;;
esac
case "$HAVE_ENV" in
  *done)
    do_env=false
    HAVE_ENV=false
    method="(re)"
    ;;
  *)
    do_env=true
    HAVE_ENV=true
    ;;
esac
case "$HAVE_UG" in
  *done)
    do_ug=false
    HAVE_UG=false
    method="(re)"
    ;;
  *)
    do_ug=true
    HAVE_UG=true
    ;;
esac
case "$HAVE_CLAMAV" in
  *done)
    do_clamav=false
    HAVE_CLAMAV=false
    method="(re)"
    ;;
  *)
    do_clamav=true
    HAVE_CLAMAV=true
    ;;
esac

while test $# -gt 0; do
  if test "$1" = "--build" -o "$1" = "-b"; then
    rm -rf /SourceCache/${Project}
    do_build=true
    do_clamav=true
    method="(re)"
  fi
  if test "$1" = "--clamav" -o "$1" = "-c"; then
    rm -rf /SourceCache/${Project}/ClamAV-1
    do_clamav=true
    method="(re)"
  fi
  if test "$1" = "--env" -o "$1" = "-e"; then
    do_env=true
    method="(re)"
  fi
  if test "$1" = "--user" -o "$1" = "-u"; then
    do_ug=true
    method="(re)"
  fi
  if test "$1" = "--force" -o "$1" = "-f"; then
    rm -rf ${BS_PATH}/.${Project}Done
    rm -rf /SourceCache/${Project}
    rm -rf /DIST_ROOT/Release
    do_build=true
    HAVE_BUILD=true
    do_clamav=true
    HAVE_CLAMAV=true
    do_env=true
    HAVE_ENV=true
    do_ug=true
    HAVE_UG=true
    method="(re)"
    echo "Forcing Install"
  fi
  if test "$1" = "--help" -o "$1" = "-h"; then
cat  <<X
${color_1}  |                                                        |  ${norm}
  |                                                        |
  | ${bold}NO${norm} options performs first time installs                |
  |  with safety checks.                                   |
  |                                                        |
  |  Recommended for first time installs and installs that |
  |  were done through other install processes.            |
  |                                                        |
  |  Reconfigures existing installs to be Apple compliant. |
  |                                                        |
  |  Stores all software in '/usr/local' so it does not    |
  |  interfere with any Apple installed software.          |
  |                                                        |
  |  Provides an way to easily create a package installer  |
  |  which will include all dependant packages.            |
  |                                                        |
  |  Updating any of the packages is a simple process of   |
  |  replacing any existing source code tree and issuing   |
  |  a 'make' command to generate the updated binaries.    |
  |                                                        |
  |  Please do not browse the distribution directory with  |
  |  the finder to avoid corrupting the environment.       |
  |                                                        |
  |   The following tree's have been generated to assist   |
  |   in the easy building process of your SPAM/ANTI-VIRUS |
  |   services, do not delete them:                        |
  |                                                        |
  |    /SourceCache/${Project}/BerkeleyDB-1                    |
  |    /SourceCache/${Project}/ClamAV-1                        |
  |    /SourceCache/${Project}/GMP-1                           |
  |    /DIST_ROOT/Release                                  |
  |    ${BS_PATH}                             |
  |                                                        |
  +--------------------------------------------------------+

  -b | --build  : (re)create building environment.
     |          :
  -c | --clamav : (re)create the ClamAV buildi environment
     |          : and get the latest ClamAV distribution.
     |          :
  -e | --env    : (re)create users environment (re)set
     |          : permissions and ownership for users homes
     |          :
  -f | --force  : force install (re)creates everything.
     |          :
  -h | --help   : prints this help menu.
     |          :
  -v | --verify : verify.
     |          :
  -u | --user   : (re)create users and groups, (re)sets
     |          : permissions / ownership for users homes.
     |          :


X
tput sgr0
  exit
  fi

  shift
done
cat <<X
${color_1}                                                              

X
tput sgr0
msg0="${bold}BuildSmart-${Project}${norm}"
msg1="According to ${msg0},  you already creaated your"
msg2="If you want to force the ${method}creation use:"

if [ ! -f ${BS_PATH}/.${Project}Done ]; then touch ${BS_PATH}/.${Project}Done; fi

cat <<X

Checking users and groups.

X

if [ $do_ug == true ]; then
if [ `nidump group .| cut -d: -f1 -f3 | egrep amavisd | cut -d: -f1` ]; then
if [ `nidump group .| cut -d: -f1 -f3 | egrep amavisd | cut -d: -f2` == "83" ]; then
echo "amavisd group already exists"
make_group1=false
else
niutil -destroy . /groups/amavisd
cmd_group1="fixing"
fi
fi
if [ `nidump group .| cut -d: -f1 -f3 | egrep clamav | cut -d: -f1` ]; then
if [ `nidump group .| cut -d: -f1 -f3 | egrep clamav | cut -d: -f2` == "82" ]; then
echo "clamav group already exists"
make_group2=false
else
niutil -destroy . /groups/amavisd
cmd_group2="fixing"
fi
fi
if [ `nidump passwd .| cut -d: -f1 -f3 | egrep amavisd | cut -d: -f1` ]; then
if [ `nidump passwd .| cut -d: -f1 -f3 | egrep amavisd | cut -d: -f2` == "83" ]; then
echo "amavisd user already exists"
cmd_user1="fixing"
make_user1=false
else
niutil -destroy . /users/amavisd
cmd_user1="fixing"
fi
fi
if [ `nidump passwd .| cut -d: -f1 -f3 | egrep clamav | cut -d: -f1` ]; then
if [ `nidump passwd .| cut -d: -f1 -f3 | egrep clamav | cut -d: -f2` == "82" ]; then
echo "clamav user already exists"
make_user2=false
cmd_user2="fixing"
else
niutil -destroy . /users/amavisd
cmd_user2="fixing"
fi
fi

if [ $make_group1 == true ]; then
echo -n "$cmd_group1 group '${bold}amavisd${norm}'"
niutil -create . /groups/amavisd
niutil -createprop . /groups/amavisd gid 83
niutil -createprop . /groups/amavisd realname 'SPAM Assassin Group 2'
niutil -createprop . /groups/amavisd passwd "*"
sleep 1
echo "... done"
fi

if [ $make_group2 == true ]; then
echo -n "$cmd_group2 group '${bold}clamav${norm}'"
niutil -create . /groups/clamav
niutil -createprop . /groups/clamav gid 82
niutil -createprop . /groups/clamav realname 'SPAM Assassin Group 1'
niutil -createprop . /groups/clamav passwd "*"
sleep 1
echo "... done"
fi
if [ $make_user1 == true ]; then
echo -n "$cmd_user1 user '${bold}amavisd${norm}'"
niutil -create . /users/amavisd
niutil -createprop . /users/amavisd uid 83
niutil -createprop . /users/amavisd gid 83
niutil -createprop . /users/amavisd passwd "*"
niutil -createprop . /users/amavisd realname 'Amavisd User'
niutil -createprop . /users/amavisd shell /bin/tcsh
niutil -createprop . /users/amavisd home /var/virusmails
sleep 1
echo "... done"
fi

if [ $make_user2 == true ]; then
echo -n "$cmd_user2 user '${bold}clamav${norm}'"
niutil -create . /users/clamav
niutil -createprop . /users/clamav uid 82
niutil -createprop . /users/clamav gid 82
niutil -createprop . /users/clamav passwd '*'
niutil -createprop . /users/clamav realname 'Clamav User'
niutil -createprop . /users/clamav shell /bin/tcsh
niutil -createprop . /users/clamav home /var/clamav
sleep 1
echo "... done"
fi
if [ $do_ug == true ] && [ $HAVE_UG == true ]; then
	echo "HAVE_UG=done">> ${BS_PATH}/.${Project}Done
fi
else
cat <<X

${msg1} users and groups.

${msg2}
	'${bold}$0 -u${norm}'
  or
	'${bold}$0 --user${norm}'

X
sleep 1
fi

cat <<X

Checking users / groups homes and log files.

X
if [ $do_env == true ]; then

echo -n "$cmd_user1 home for '${bold}amavisd${norm}'"
mkdir -p /var/amavis/db
mkdir -p /var/amavis/tmp
mkdir -p /var/virusmails
chown -R clamav:clamav /var/virusmails
chown -R clamav:clamav /var/amavis
chmod -R 0750 /var/virusmails
chmod -R 0750  /var/amavis
sleep 1
echo "... done"
echo -n "$cmd_user2 home for '${bold}clamav${norm}'"
mkdir -p /var/clamav/tmp
chown -R clamav:clamav /var/clamav
chmod -R 0750 /var/clamav
sleep 1
echo "... done"
echo -n "$cmd_user2 log files for '${bold}amavisd-new${norm} and ${bold}ClamAV${norm}'"
touch /var/log/amavis.log
touch /var/log/clamav.log
touch /var/log/freshclam.log
chown -R root:clamav /var/log/amavis.log
chown -R root:clamav /var/log/clamav.log
chown -R root:clamav /var/log/freshclam.log
chmod -R 0664 /var/log/amavis.log
chmod -R 0664 /var/log/clamav.log
chmod -R 0664 /var/log/freshclam.log
echo "... done"
if [ $do_env == true ] && [ $HAVE_ENV == true ]; then
	echo "HAVE_ENV=done">> ${BS_PATH}/.${Project}Done
fi
else
cat <<X

${msg1} users environment.

${msg2}
	'${bold}$0 -e${norm}'
  or
	'${bold}$0 --env${norm}'

X
sleep 1
fi

cat <<X

Checking general build environment for software packages

X

if [ $do_build == true ]; then
echo -n "making build environment for '${bold}GMP${norm}'"
mkdir -p /SourceCache/${Project}/GMP-1
echo "##
# Makefile for Projects
##

# Project info
Project			= gmp
UserType		= Developer
ToolType		= Libraries
GnuAfterInstall	=
Extra_CC_FLAGS	=
Install_Prefix	= /usr/local

#It's a GNU Source Project
include \$(MAKEFILEPATH)/CoreOS/ReleaseControl/GNUSource.make

Configure		= rm -rf /tmp/\$(Project)/Release && mkdir -p /tmp/\$(Project)/Debug && ln -sf /DIST_ROOT/Release /tmp/\$(Project)/Release && CFLAGS=\"-DFD_SETSIZE=2048\" ./configure
Configure_Flags	= --prefix=\"\$(Install_Prefix)\" --mandir=/usr/share/man --enable-devel --enable-mpbsd --enable-mpfr

lazy_install_source:: shadow_source

Target_Install	= install

it-work:
	cd \$(OBJROOT) && make install

.PHONY: it-work
" > /SourceCache/${Project}/GMP-1/Makefile
sleep 1
echo "... done"
echo "Downloading ${bold}${gmp_file}${norm}"
curl -s ftp://ftp.gnu.org/gnu/gmp/${gmp_file}${file_ext} -o /SourceCache/${Project}/GMP-1/${gmp_file}${file_ext}
cd /SourceCache/${Project}/GMP-1
if [ -f ${gmp_file}${file_ext} ]; then
rm -rf gmp
tar xzf ${gmp_file}${file_ext}
mv ${gmp_file} gmp && rm -rf ${gmp_file}${file_ext}
echo "Download ${bold}successful${norm}"
else
echo "${bold}There was a problem with the download${norm}"
fi
cat <<X

Your build environment has been successfully ${method}created, now cleaning up.

X
sleep 2
else
cat <<X

${msg1} build environment.

${msg2}
	'${bold}$0 -b${norm}'
  or
	'${bold}$0 --build${norm}'

X
sleep 1
fi
if [ $HAVE_BUILD == true ]; then
	echo "HAVE_BUILD=done">> ${BS_PATH}/.${Project}Done
fi

cat <<X

Checking ClamAV build environment for software packages

X

if [ $do_clamav == "true" ]; then
echo -n "making build environment for '${bold}ClamAV${norm}'"
if [ -f /System/Library/StartupItems/AMAVISCLAMAV ];then rm -rf /System/Library/StartupItems/AMAVISCLAMAV; fi
if [ -f /System/Library/StartupItems/MAILTRANSPORT ];then rm -rf /System/Library/StartupItems/MAILTRANSPORT; fi
if [ -f /Library/StartupItems/AMAVISCLAMAV ];then rm -rf /Library/StartupItems/AMAVISCLAMAV; fi
if [ -f /Library/StartupItems/MAILTRANSPORT ];then rm -rf /Library/StartupItems/MAILTRANSPORT; fi
mkdir -p /SourceCache/${Project}/ClamAV-1/patches
mkdir -p /DIST_ROOT/Release/System/Library
echo "diff -Naur clamd/clamd.c.orig clamd/clamd.c
--- clamd/clamd.c.orig	2005-06-19 15:22:05.000000000 -0400
+++ clamd/clamd.c	2005-06-23 08:32:21.000000000 -0400
@@ -277,8 +277,11 @@
     }
 
     /* fork into background */
-    if(!cfgopt(copt, \"Foreground\"))
+    if(optc(opt, 'D') || cfgopt(copt, \"Foreground\")) {
+	logg(\"freshclam running in foreground\");
+	} else {
 	daemonize();
+	}
 
     if(tcpsock)
 	ret = tcpserver(opt, copt, root);
diff -Naur clamd/options.c.orig clamd/options.c
--- clamd/options.c.orig	2005-06-19 15:22:05.000000000 -0400
+++ clamd/options.c	2005-06-23 08:28:32.000000000 -0400
@@ -45,11 +45,12 @@
 	int ret, opt_index, i, len;
 	struct optstruct *opt;
 
-	const char *getopt_parameters = \"hc:V\";
+	const char *getopt_parameters = \"hc:VD\";
 
 	static struct option long_options[] = {
 	    {\"help\", 0, 0, 'h'},
 	    {\"config-file\", 1, 0, 'c'},
+	    {\"foreground\", 0, 0, 'D'},
 	    {\"version\", 0, 0, 'V'},
 	    {\"debug\", 0, 0, 0},
 	    {0, 0, 0, 0}
diff -Naur freshclam/freshclam.c.orig freshclam/freshclam.c
--- freshclam/freshclam.c.orig	2005-06-19 15:22:08.000000000 -0400
+++ freshclam/freshclam.c	2005-06-23 08:20:47.000000000 -0400
@@ -286,8 +286,11 @@
 
 	bigsleep = 24 * 3600 / checks;
 
-	if(!cfgopt(copt, \"Foreground\"))
+    if(optc(opt, 'D') || cfgopt(copt, \"Foreground\")) {
+	logg(\"freshclam running in foreground\");
+	} else {
 	    daemonize();
+	}
 
 	if (optc(opt, 'p')) {
 	    pidfile = getargc(opt, 'p');
diff -Naur freshclam/options.c.orig freshclam/options.c
--- freshclam/options.c.orig	2005-06-19 15:22:08.000000000 -0400
+++ freshclam/options.c	2005-06-23 07:43:48.000000000 -0400
@@ -38,7 +38,7 @@
 	int ret, opt_index, i, len;
 	struct optstruct *opt;
 
-	const char *getopt_parameters = \"hvdp:Vl:c:u:a:\";
+	const char *getopt_parameters = \"hvdp:VDl:c:u:a:\";
 
 	static struct option long_options[] = {
 	    /* 
@@ -55,6 +55,7 @@
 	    {\"log-verbose\", 0, 0, 0}, /* not used */
 	    {\"stdout\", 0, 0, 0},
 	    {\"daemon\", 0, 0, 'd'},
+	    {\"foreground\", 0, 0, 'D'},
 	    {\"pid\", 1, 0, 'p'},
 	    {\"user\", 1, 0, 'u'}, /* not used */
 	    {\"config-file\", 1, 0, 0},
" > /SourceCache/${Project}/ClamAV-1/patches/d.patch
echo "##
# Makefile for Projects
##

# Project info
Project			= clamav
UserType		= Administration
ToolType		= Services
GnuAfterInstall	=
Extra_CC_FLAGS	=
Install_Prefix	= /usr/local

#It's a GNU Source Project
include \$(MAKEFILEPATH)/CoreOS/ReleaseControl/GNUSource.make

Configure		= rm -rf /tmp/\$(Project)/Release && mkdir -p /tmp/\$(Project)/Debug && ln -sf /DIST_ROOT/Release /tmp/\$(Project)/Release && CFLAGS=\"-DFD_SETSIZE=2048\" ./configure
Configure_Flags	= --prefix=\"\$(Install_Prefix)\" --mandir=/usr/share/man --sysconfdir=/private/etc/spam/clamav --disable-shared --enable-bigstack --with-user=clamav --enable-static --with-group=clamav --with-dbdir=/var/clamav --with-datadir=/var/clamav

AEP				= YES
AEP_Patches		= d.patch

lazy_install_source:: shadow_source patch_source

Target_Install	= install

it-work:
	@echo 
	@echo -n \"Do you really want to install this build?  Type \\\"y\\\" or \\\"n\\\" please: \"
	@\$(SH) -c 'read x; case "\$\$x" in y|Y) exit 0;; *) exit 1;; esac'
	@\$(SH) -c 'spamctl stop' 
	cd \$(OBJROOT) && make install
	@\$(SH) -c 'spamctl start' 

patch_source::
ifeq (\$(AEP),YES)
	if [ ! -f \$(OBJROOT)/patched ]; then \\
	touch \$(OBJROOT)/patched; \\
	for patchfile in \$(AEP_Patches); do \\
		cd \$(OBJROOT) && patch -p0 < \$(SRCROOT)/patches/\$\$patchfile; \\
	done \\
	fi
endif

.PHONY: it-work
" > /SourceCache/${Project}/ClamAV-1/Makefile
sleep 1
echo "... done"
echo -n "Creating helper app (spamctl)"
printf '#!/bin/sh
#

. /etc/rc.common

syslog_name="spamctl"

VER="0.1.1"


case `uname -r|cut -d. -f1` in
\t8)
\t\tOS_TYPE="TIGER"
\t\t;;
\t7)
\t\tOS_TYPE="PANTHER"
\t\t;;
\t6)
\t\tOS_TYPE="UNKNOWN"
\t\t;;
esac


case $TERM in
\t#   for the most important terminal types we directly know the sequences
\txterm|xterm*|vt220|vt220*)\n' > /usr/sbin/spamctl
printf "\t\tnorm=\`awk 'BEGIN { printf("%c%c%c%c", 27, 91, 49, 109); }' </dev/null 2>/dev/null\`
\t\tnorm=\`awk 'BEGIN { printf("%c%c%c", 27, 91, 109); }' </dev/null 2>/dev/null\`
\t\t;;
\tvt100|vt100*|cygwin)
\t\tbold=\`awk 'BEGIN { printf("%c%c%c%c%c%c", 27, 91, 49, 109, 0, 0); }' </dev/null 2>/dev/null\`
\t\tnorm=\`awk 'BEGIN { printf("%c%c%c%c%c", 27, 91, 109, 0, 0); }' </dev/null 2>/dev/null\`
\t\t;;
esac

if [ \`whoami\` != \"root\" ]; then
cat <<X

\$0 must be run as \${bold}root\${norm} user.

X
exit
fi

function startit {
\tif [ \"\${OS_TYPE}\" = \"TIGER\" ]; then
\t\t\`launchctl load -w /System/Library/LaunchDaemons/net.clamav.clamd.plist\`
\t\t\`launchctl load -w /System/Library/LaunchDaemons/net.clamav.freshclam.plist\`
\t\t\`launchctl load -w /System/Library/LaunchDaemons/org.amavis.amavisd.plist\`
\tfi
\tif [ \"\${OS_TYPE} = \"PANTHER\" ]; then
\t\tSystemStarter start AMAVISCLAMAV
\tfi
\tif [ \"\${OS_TYPE} = \"UNKNOWN\" ]; then
\t\t echo \"Unsupported OS\"
\tfi
}

function stopit {
\tif [ \"\${OS_TYPE}\" = \"TIGER\" ]; then
\t\t\`launchctl stop net.clamav.clamd\`
\t\t\`launchctl unload -w /System/Library/LaunchDaemons/net.clamav.clamd.plist\`
\t\t\`launchctl stop net.clamav.freshclam\`
\t\t\`launchctl unload -w /System/Library/LaunchDaemons/net.clamav.freshclam.plist\`
\t\t\`launchctl stop org.amavis.amavisd\`
\t\t\`launchctl unload -w /System/Library/LaunchDaemons/org.amavis.amavisd.plist\`
\tfi
\tif [ \"\${OS_TYPE} = \"PANTHER\" ]; then
\t\tSystemStarter stop AMAVISCLAMAV
\tfi
\tif [ \"\${OS_TYPE} = \"UNKNOWN\" ]; then
\t\t echo \"Unsupported OS\"
\tfi
}


while test \$# -gt 0; do
  if test \$1 = \"start\"; then
  \tConsoleMessage \"Starting amavisd-new/ClamAV\"
\tstartit
\texit
  fi
  if test \$1 = \"stop\"; then
  \tConsoleMessage \"Stoping amavisd-new/ClamAV\"
\tstopit
\texit
  fi
  if test \$1 = \"reload\"; then
  \tConsoleMessage \"Reloading amavisd-new/ClamAV\"
\tstopit
\tsleep 1
\tstartit
\texit
  fi
  shift
done
ConsoleMessage \"fatal: usage: spamctl start (or stop, reload)\"" >> /usr/sbin/spamctl
chmod 0755 /usr/sbin/spamctl
sleep 1
echo "... done"
if [ ${OS_TYPE} == "TIGER" ]; then
mkdir -p /DIST_ROOT/Release/System/Library/LaunchDaemons/
printf '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">\n<dict>\n\t<key>Iterations</key>\n\t<integer>3</integer>
\t<key>Label</key>\n\t<string>net.clamav.freshclam</string>\n\t<key>OnDemand</key>\n\t<false/>
\t<key>Nice</key>\n\t<integer>1</integer>\n\t<key>LowPriorityIO</key>\n\t<true/>\n\t<key>Program</key>\n\t<string>/usr/local/bin/freshclam</string>
\t<key>ProgramArguments</key>\n\t<array>\n\t\t<string>freshclam</string>\n\t\t<string>-D</string>\n\t\t<string>-d</string>
\t\t<string>-p</string>\n\t\t<string>/var/clamav/freshclam.pid</string>\n\t</array>\n\t<key>ServiceIPC</key>\n\t<false/>
\t<key>UserName</key>\n\t<string>clamav</string>\n</dict>\n</plist>\n' > /System/Library/LaunchDaemons/net.clamav.freshclam.plist
printf '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version=\"1.0\">\n<dict>\n\t<key>Iterations</key>\n\t<integer>3</integer>
\t<key>Nice</key>\n\t<integer>1</integer>\n\t<key>LowPriorityIO</key>\n\t<true/>
\t<key>Label</key>\n\t<string>net.clamav.clamd</string>\n\t<key>OnDemand</key>
\t<false/>\n\t<key>Program</key>\n\t<string>/usr/local/sbin/clamd</string>
\t<key>ProgramArguments</key>\n\t<array>\n\t\t<string>clamd</string>\n\t\t<string>-D</string>\n\t</array>
\t<key>ServiceIPC</key>\n\t<false/>\n</dict>\n</plist>\n' > /System/Library/LaunchDaemons/net.clamav.clamd.plist
cp -rp /System/Library/LaunchDaemons/net.clamav.clamd.plist /DIST_ROOT/Release/System/Library/LaunchDaemons/net.clamav.clamd.plist
cp -rp /System/Library/LaunchDaemons/net.clamav.freshclam.plist /DIST_ROOT/Release/System/Library/LaunchDaemons/net.clamav.freshclam.plist
echo "
You can ignore warnings about '${bold}No such process${norm}'.
"

if [ -d /System/Library/LaunchDaemons ]; then
	is_run=`launchctl list|grep clam|grep org|cut -d"." -f1-3`
	for app in $is_run; do
	printf "Stopping : ${app}\n"
	launchctl stop ${app}
	launchctl unload -w /System/Library/LaunchDaemons/$app.plist
	done
fi
fi
if [ ${OS_TYPE} == "PANTHER" ]; then
echo -n "making build environment for '${bold}BerkeleyDB${norm}'"
mkdir -p /SourceCache/${Project}/BerkeleyDB-1
echo "##
# Makefile for Projects
##

# Project info
Project         = db
UserType		= Developer
ToolType		= Tool
GnuAfterInstall	=
Extra_CFLAGS	= -no-cpp-precomp
Extra_LDFLAGS	= -force_flat_namespace -bind_at_load

include \$(MAKEFILEPATH)/CoreOS/ReleaseControl/Common.make

CC_Optimize = -Os 

build::
	mkdir -p \$(OBJROOT) && rm -rf /tmp/\$(Project)/Release && mkdir -p /tmp/\$(Project)/Debug && ln -sf /DIST_ROOT/Release /tmp/\$(Project)/Release
	cd \$(OBJROOT) && \$(Environment) \$(SRCROOT)/\$(Project)/dist/configure --disable-java --disable-shared --prefix=/usr/local docdir=/usr/docs
	cd \$(OBJROOT) && make
	cd \$(OBJROOT) && make prefix=\$(DSTROOT)/usr/local bindir=\$(DSTROOT)/usr/local/bin install

install_headers::
	mkdir -p \$(OBJROOT)
	cd \$(OBJROOT) && \$(Environment) \$(SRCROOT)/\$(Project)/dist/configure --disable-java --disable-shared --prefix=/usr/local docdir=/usr/docs
	mkdir -p \$(DSTROOT)/usr/local/include
	\$(INSTALL) -c -m 444 \$(OBJROOT)/db.h \$(DSTROOT)/usr/local/include
	\$(INSTALL) -c -m 444 \$(OBJROOT)/db_cxx.h \$(DSTROOT)/usr/local/include

it-work:
	cd \$(OBJROOT) && make install

.PHONY: it-work
" > /SourceCache/${Project}/BerkeleyDB-1/Makefile
sleep 1
echo "... done"
echo "Downloading ${bold}${db_file}${norm}"
curl -s ftp://ftp.sleepycat.com/releases/${db_file}${file_ext} -o /SourceCache/${Project}/BerkeleyDB-1/${db_file}${file_ext}
cd /SourceCache/${Project}/BerkeleyDB-1
if [ -f ${db_file}${file_ext} ]; then
rm -rf db
tar xzf ${db_file}${file_ext}
mv ${db_file} db && rm -rf ${db_file}${file_ext}
echo "Download ${bold}successful${norm}"
else
echo "${bold}There was a problem with the download${norm}"
fi
mkdir -p /DIST_ROOT/Release/System/Library/StartupItems
mkdir -p /System/Library/StartupItems/AMAVISCLAMAV
printf "#!/bin/sh

. /etc/rc.common

StartService ()
{
\tif [ \"\${AMAVISCLAMAV:=-NO-}\" = \"-YES-\" ]; then
\t\tConsoleMessage \"Starting AMAVISCLAMAV\"
\t\t/usr/local/bin/freshclam -d
\t\t/usr/local/sbin/clamd
\t\tsu clamav -c /usr/bin/amavisd
\t\telse
\t\tNoService
\t\tfi
}

StopService ()
{
\t\tConsoleMessage \"Stoping AMAVISCLAMAV\"
\t\tamavisd stop
\t\tkillall -u clamav

}

RestartService ()
{
\tif [ \"\${AMAVISCLAMAV:=-NO-}\" = \"-YES-\" ]; then
\t\tConsoleMessage \"Reloading AMAVISCLAMAV\"
\t\tStopService
\t\tStartService
\telse
\t\tStopService
\t\tNoService
\t\tfi
}

NoService ()
{
\t\tConsoleMessage \"Service is disabled in /etc/hostconfig
\t\t
\t\tUnable to start
\t\t\"
}

RunService \"\$1\"
" > /System/Library/StartupItems/AMAVISCLAMAV/AMAVISCLAMAV
printf "{
\tDescription\t= \"AMAVISCLAMAV\";
\tProvides\t\t= (\"AMAVISCLAMAV\");
\tRequires\t\t= (\"Resolver\");
\tOrderPreference\t= \"NONE\";
\tMessages =
\t{
\t\tstart\t= \"Starting AMAVISCLAMAV\";
\t\tstop\t= \"Stopping AMAVISCLAMAV\";
\t\trestart\t= \"Reloading AMAVISCLAMAV\";
\t};
}
" > /System/Library/StartupItems/AMAVISCLAMAV/StartupParameters.plist
chmod -R 0755 /System/Library/StartupItems/AMAVISCLAMAV
chown -R root:wheel /System/Library/StartupItems/AMAVISCLAMAV
cp -rp /System/Library/StartupItems/AMAVISCLAMAV /DIST_ROOT/Release/System/Library/StartupItems/
fi
sleep 1
echo "Downloading ${bold}${clamav_file}${norm}"
echo -n "Getting latest version of ${clamav_file}"
latest_ver=`curl -s http://www.clamav.net| egrep is: | cut -d: -f2 | cut -d">" -f2 | cut -d"<" -f1 | xargs -n 1 echo`
echo "... ${latest_ver}"
curl -s http://unc.dl.sourceforge.net/sourceforge/clamav/${clamav_file}-${latest_ver}${file_ext} -o /SourceCache/${Project}/ClamAV-1/${clamav_file}-${latest_ver}${file_ext}
cd /SourceCache/${Project}/ClamAV-1
if [ -f ${clamav_file}-${latest_ver}${file_ext} ]; then
rm -rf clamav
tar xzf ${clamav_file}-${latest_ver}${file_ext}
mv ${clamav_file}-${latest_ver} clamav && rm -rf ${clamav_file}-${latest_ver}${file_ext}
echo "Download ${bold}successful${norm}"
else
echo "${bold}There was a problem with the download${norm}"
fi
if [ $HAVE_CLAMAV == true ]; then
	echo "HAVE_CLAMAV=done">> ${BS_PATH}/.${Project}Done
fi
cat <<X

Your ClamAV build environment has been successfully ${method}created, now cleaning up.

X
sleep 2
else
cat <<X

${msg1} ClamAV build environment.

${msg2}
	'${bold}$0 -c${norm}'
  or
	'${bold}$0 --clamav${norm}'

X
sleep 1
fi
sleep 1
cat <<X

  +--------------------------------------------------------+
  |                                                        |
  |                   ${msg0}                    |
  |                                                        |
  +--------------------------------------------------------+
  | License:                                               |
  | This software is subject to the GNU License version 2. |
  | By continuing this installation process, you are bound |
  | by the terms and conditions of this license agreement. |
  | If you do not agree with the terms of this license,    |
  | you must abort the installation process at this point. |
  +--------------------------------------------------------+
  |                   ${bold} *** NOTE *** ${norm}                       |
  | After issuing the 'make' command, the project will be  |
  |  built and installed automatically without requiring   |
  | any additional userinteraction.                        |
  |                                                        |
  | Please do not browse the distribution directory with   |
  | the finder to avoid corrupting the disribution         |
  | environment.                                           |
  |                                                        |
  | The following tree's have been generated to assist in  |
  | the building process of your SPAM/ANTI-VIRUS services, |
  | do not delete them:                                    |
  | /SourceCache/${Project}/BerkeleyDB-1                       |
  | /SourceCache/${Project}/ClamAV-1                           |
  | /SourceCache/${Project}/GMP-1                              |
  | /DIST_ROOT/Release                                     |
  | /usr/sbin/spamctl                                      |
  |                                                        |
  |                                                        |
  +--------------------------------------------------------+

System configuration complete.

After Building ClamAV, please check the config files located in
/etc/spam/clamav for proper settings and restart the server.

Thank you for using ${msg0}.

X

