#!/bin/sh
#
# aryzhov@spasu.net
#
# Install and configure OpenWebMail
# Normally, must be started from add_zones for a specific machine
#
#
[ -z "$SID" ] && SID=/tmp/install_config
[ X$SubrIsRead = XYes ] || . $SID/Scripts/Misc/!Includes/Subroutines
[ X$VarsAreSet = XYes ] || SetJVars
SetRootOpts ; [ X$ROOT != X/ ] && exec Chroot_Script $0 $*

SRCDIR=$JS_MEDIA_DIR/Packages/OpenWebMail; cd $SRCDIR/. || StartShell "No source directory $SRCDIR"

echo "\n\n=== Installing OpenWebMail ===\n"

[ x$1 != x ]        || StartShell "Arg1 must be target dir"
[ x$ZONESDIR != x ] || StartShell "$0: ZONESDIR undefined"

APACHE_DIR=$1
TDIR=$APACHE_DIR/openwebmail
ZONEROOT=`echo $APACHE_DIR | sed "s:/root/.*$:/root:"`
APACHE_ZPH=`echo $APACHE_DIR | sed "s:^.*/root/:/:"`
PERL=/usr/local/bin/perl

#ERLMOD_DIR=`chroot $ZONEROOT $PERL -V | awk '/@INC:/,/@EOF@/' | awk '/site_perl\/[0-9]*[.][0-9]*[.][0-9]*$/' | head -1`
#ERLMOD_DIR=`echo $PERLMOD_DIR`
PERLVERS=`pkginfo -R $ZONEROOT  -l SMCperl | awk '$1=="VERSION:" {print $2}'`
PERLMOD_DIR=/usr/local/lib/perl5/site_perl/$PERLVERS
OWM_PASSWD_FILE=/usr/local/www/cgi-bin/openwebmail/etc/master.passwd

[ x$PERLMOD_DIR != x   -a   -d $ZONEROOT/$PERLMOD_DIR/. ] || StartShell "$0: Failed to find perl modules directory for $PERL"
cd $SRCDIR/PerlModules || StartShell
find . | cpio -dump $ZONEROOT/$PERLMOD_DIR/.

for i in modules auth quota shares; do
  cd $ZONEROOT/$PERLMOD_DIR/. && ln -s $APACHE_ZPH/cgi-bin/openwebmail/$i || StartShell "Cannot link perl/openwebmail $i"
done

mkdir -p $TDIR || StartShell
cd $SRCDIR || StartShell
gzip -d <openwebmail-current.tar.gz | (cd $TDIR && tar xf -) || StartShell
cd $APACHE_DIR
mv openwebmail/cgi-bin/openwebmail  cgi-bin/.  || StartShell openwebmail/cgi-bin/openwebmail
mv openwebmail/data/openwebmail     htdocs/.   || StartShell openwebmail/data/openwebmail
ln -s ../cgi-bin/openwebmail/etc openwebmail/. || StartShell openwebmail/etc
ln -s ../cgi-bin/openwebmail/auth openwebmail/. || StartShell openwebmail/auth

ln -s /export/www users
ln -s ./htdocs/. data
echo $APACHE_DIR/openwebmail | sed "s:^$ZONEROOT/:/:" >$ZONEROOT/etc/openwebmail_path.conf



cp $ZONESDIR/inet/root/usr/local/bin/perl $ZONESDIR/inet/root/usr/bin/suidperl
chmod 4555 $ZONESDIR/inet/root/usr/bin/suidperl

cd $APACHE_DIR/.. && ln -s ./`basename $APACHE_DIR`/. www

DBM_CONF=$APACHE_DIR/cgi-bin/openwebmail/etc/defaults/dbm.conf
cp $DBM_CONF $DBM_CONF.std || StartShell "File missing: $DBM_CONF"
printf "1\n/dbm_ext.*[.]db/\ns/[.]db/.pag/\nw\nq\n" | ed -s $DBM_CONF
diff $DBM_CONF.std $DBM_CONF
chroot $ZONEROOT /bin/sh -c "echo n | /usr/local/www/cgi-bin/openwebmail/openwebmail-tool.pl --init"


#
# Correct the system-wide defaults
#
OWMCONF=$APACHE_DIR/cgi-bin/openwebmail/etc/openwebmail.conf
echo "
  domainnames                     spasu.net
  smtpserver                      mail.ggaweb.ch
  default_language                en
  default_fontsize                6pt
  default_hourformat              24
  default_msgsperpage             5000
  default_webdisk_dirnumitems     1000
  webdisk_uploadlimit             5000000
" | sed 's/^ *//' >$OWMCONF

#
# Correct the authentication method
#
AUTHCONF=$APACHE_DIR/cgi-bin/openwebmail/etc/auth_unix.conf
echo "
  passwdfile_plaintext    $OWM_PASSWD_FILE
  passwdfile_encrypted    $OWM_PASSWD_FILE
  passwdmkdb              none
  check_expire            no
  check_nologin           no
  check_shell             no
  check_cobaltuser        no
  change_smbpasswd        no
" | sed 's/^ *//' >$AUTHCONF

touch $ZONEROOT/$OWM_PASSWD_FILE
chown nobody:nobody $ZONEROOT/$OWM_PASSWD_FILE
chmod 600 $ZONEROOT/$OWM_PASSWD_FILE

#
# Add FTP template - will be needed for creating new users
#
cd $SRCDIR/FTPTemplate && find . -print | cpio -dump /space/export/.
find $ZONESDIR/ftpd/root/export/.
egrep -s '^guftp:' $ZONESDIR/ftpd/root/etc/group || echo "guftp::1001:aryzhov" >>$ZONESDIR/ftpd/root/etc/group
echo "#!AddUsersHere" >>$ZONESDIR/ftpd/root/etc/ftpaccess
