SENDMAIL_VER=8.12.0
PROCMAIL_VER=3.15.1
VERSION=$SENDMAIL_VER
BUILD=3
ARCH=i386
TAG=REC

PKGNAME=sendmail
MAINTAINER="tg <tg@linuxmafia.org>"

IGNOREPATH=/tmp:/proc:/dev:/root:/var:/home
STRIPLIB=y
STRIPBIN=y

PROGNAME="BSD sendmail"
DESC="\
sendmail $VERSION.\n\
\n\
Eric Allman's mail transport agent. The _Unix System Administration\n\
Handbook_ calls sendmail 'The most complex and complete mail delivery\n\
system in common use...'\n\
\n\
Ready-made configuration files are included for systems connected by\n\
TCP/IP (with or without a nameserver) and for systems using UUCP.\n\
\n\
procmail is included as a local mail agent."

compile() {
   # build procmail
   cd $TMP
   tar xvzf $CWD/procmail-$PROCMAIL_VER.tar.gz
   cd procmail-$PROCMAIL_VER
   echo "
" | make

   # build sendmail
   OSCPU="`uname -srm | tr ' ' '.'`"
   cd $TMP
   tar xvzf $CWD/sendmail.$VERSION.tar.gz
   cd sendmail-$VERSION

   # Jan Rafaj's patches to allow non-suid sendmail.  If you're
   # interested in this, uncomment and recompile.
   ## Allow sendmail to open file in "mqueue" dir, even if its sgid != 0
   #zcat $CWD/sendmail-mqueue_notworldwritable.diff.gz | patch -p0
   #zcat $CWD/sendmail-no_stupid_ugid_check.diff.gz | patch -p0

   # Build .cf files for Linux:
   zcat $CWD/sendmail.mc.diff.gz | patch -p1 --backup
   cd $TMP/sendmail-$VERSION/cf/cf
   for MODE in linux.uucp linux.smtp linux.smtp-rbl linux.smtp-rbl+
   do
      cp $MODE.mc config.mc
      sh Build config.cf
      cp config.cf $MODE.cf
   done
   cd $TMP/sendmail-$VERSION
   # make
   sh Build
   wget -N -k -r http://www.sendmail.org/faq/faq.txt
}

install() {
   # install procmail
   cd $TMP/procmail-$PROCMAIL_VER/src
   cp formail lockfile mailstat procmail /usr/bin
   cd ../man
   for f in formail.1 lockfile.1 procmail.1
   do
      gzip -9c $f > /usr/man/man1/$f.gz
   done
   for f in procmailex.5 procmailrc.5 procmailsc.5
   do
      gzip -9c $f > /usr/man/man5/$f.gz
   done
   mkdir -p /usr/doc/procmail-$PROCMAIL_VER
   cd ..
   cp -r Artistic COPYING FAQ FEATURES HISTORY INSTALL KNOWN_BUGS \
      README examples /usr/doc/procmail-$PROCMAIL_VER

   # install sendmail
   cd $TMP/sendmail-$VERSION

   # install configuration file examples
   mkdir -p /usr/src/sendmail
   ( cd $TMP/sendmail-$VERSION/cf/cf
     cp linux.smtp.cf linux.uucp.cf linux.smtp-rbl.cf linux.smtp-rbl+.cf \
        /usr/src/sendmail )

   # install documentation
   cat sendmail/mailq.1 | gzip -9c > /usr/man/man1/mailq.1.gz
   cat sendmail/newaliases.1 | gzip -9c > /usr/man/man1/newaliases.1.gz
   mkdir -p /usr/doc/sendmail
   cp www.sendmail.org/faq/faq.txt /usr/doc/sendmail
   cp FAQ INSTALL KNOWNBUGS LICENSE PGPKEYS README RELEASE_NOTES \
      /usr/doc/sendmail
   ( cd doc ; cp -r op /usr/doc/sendmail )

   # install components
   cd obj.$OSCPU
   ( cd sendmail
     cat aliases.5 | gzip -9c > /usr/man/man5/aliases.5.gz
     cat sendmail.8 | gzip -9c > /usr/man/man8/sendmail.8.gz )
   ( cd makemap
     cp makemap /usr/sbin/makemap
     cat makemap.8 | gzip -9c > /usr/man/man8/makemap.8.gz )
   ( cd mailstats
     cp mailstats /usr/sbin/mailstats
     cat mailstats.8 | gzip -9c > /usr/man/man8/mailstats.8.gz )
   ( cd praliases
     cp praliases /usr/bin/praliases
     cat praliases.8 | gzip -9c > /usr/man/man8/praliases.8.gz )
   ( cd rmail
     cp rmail /usr/bin/rmail
     cat rmail.8 | gzip -9c > /usr/man/man8/rmail.8.gz )
   ( cd smrsh
     cp smrsh /usr/sbin/smrsh
     cat smrsh.8 | gzip -9c > /usr/man/man8/smrsh.8.gz )
   ( cd mail.local
     cp mail.local /usr/sbin/mail.local
     cat mail.local.8 | gzip -9c > /usr/man/man8/mail.local.8.gz )
   ( cd vacation
     cp vacation /usr/bin/vacation
     cat vacation.1 | gzip -9c > /usr/man/man1/vacation.1.gz )

   # make symlinks
   ( cd /usr/bin
     rm -rf newaliases ; ln -sf /usr/sbin/sendmail newaliases
     rm -rf mailq      ; ln -sf /usr/sbin/sendmail mailq
     rm -rf hoststat   ; ln -sf /usr/sbin/sendmail hoststat
     rm -rf purgestat  ; ln -sf /usr/sbin/sendmail purgestat
     rm -rf sendmail   ; ln -sf /usr/sbin/sendmail sendmail )
   ( cd /usr/lib
     rm -rf sendmail   ; ln -sf /usr/sbin/sendmail sendmail )
}

attributes() {
   chown root.mail $PKG/usr/bin/procmail
   chown root.mail $PKG/usr/bin/lockfile
   chmod 6755 $PKG/usr/bin/procmail
   chmod 2755 $PKG/usr/bin/lockfile
   chmod 755 $PKG/usr/doc/procmail-$PROCMAIL_VER/examples/dirname
   chmod 755 $PKG/usr/doc/procmail-$PROCMAIL_VER/examples/mailstat
}

special() {
   # add the sendmail package components
   ( cd $PKG ; tar xvzf $CWD/_sendmail.tar.gz )

   # add sendmail as an incoming daemon
   mkdir -p $PKG/usr/sbin
   cp $TMP/sendmail-$VERSION/obj.$OSCPU/sendmail/sendmail \
      $PKG/usr/sbin/sendmail.new
   chown root.bin $PKG/usr/sbin/sendmail.new
   chmod 6555 $PKG/usr/sbin/sendmail.new
   strip $PKG/usr/sbin/sendmail.new

   # add two more config files
   ( cd $TMP/sendmail-$VERSION/obj.$OSCPU/sendmail
     cp statistics ../../sendmail/helpfile $PKG/etc/mail )

   # we need the lib directory
   mkdir -p $PKG/usr/lib
   chown root.root $PKG/usr/lib
   chmod 755 $PKG/usr/lib

   # add to the doinst.sh script
   cat << EOF >> $CTL/doinst.sh
# Move the new sendmail in place
rm -f usr/sbin/sendmail
mv usr/sbin/sendmail.new usr/sbin/sendmail

# Run newaliases for our new aliases file
chroot . /usr/bin/newaliases 1> /dev/null
EOF
}

subpacks() {
   repack smailcfg
   repack procmail
}