#!/bin/sh
#
#
#    Scalix Public License
#
#
#    The contents of this file are subject to the Scalix Public License
#    Version 1.1 ("License"); you may not use this file except in
#    compliance with the License. You may obtain a copy of the License at
#
# 
#    http://www.scalix.com/community/licensing 
#
# 
#    Software distributed under the License is distributed on an "AS IS"
#    basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
#    the License for the specific language governing rights and limitations
#    under the License.
#
# 
#    The Original Code is Scalix Server. 
#    The Initial Developer of the Original Code is Scalix Corporation. 
#    Copyright (C) 2006 Scalix Corporation. 
#    All Rights Reserved.
#    Additional Copyrights may apply - see the notice in each file
#
#
#
#
#

# (c) Copyright Hewlett-Packard Company 2001
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Support Notice
# ---------------------
# Scalix Corporation will not support the Installation and Configuration
# Wizard where any file has been modified without prior permission.


#
# This script does some initial configuration of the Scalix
# installation after we have created the primary mailnode and the admin
# user.  This is a posix shell script.
#
# It is safe to re-run this script since it will not re-do any work
# that has already been done. Running it against a system that has been
# previously configured, however, is not recommended since it may alter
# some settings.
#

# If we need to restart any services as a result of changes, we add
# the service name to this list.  Note that this script doesn't start
# the services itself, it leaves that to the wizard.
restart=""


# Turn off the Item Structure Server and stop it being started
# automatically. Don't disable displaying it with omstat in case someone
# runs this script against a system that uses ISS.
omoff -d0 -w iss
omsetsvc -a iss n

# Disable the Application Link Server if we don't have a universal
# listener user configured. Again, however, don't remove it from the
# omstat list so that people can see it.
if [ -z "$(omshowapp -m all)" ]
then
    omsetsvc -a applink n
    restart="$restart applink"
fi

# Configure the CDA server for the system directory if we haven't
# already configured it any directories. Kick off an omexeccda to
# to force the CDA server to process the directory now. For a newly
# configured system this will be very quick since we won't have many
# users in the system directory.
if omshowcda >/dev/null 2>&1
then :
else
    omaddcda -d SYSTEM
    omexeccda
    restart="$restart cda"
fi

# Create the FREEBUSY directory for Outlook users if it doesn't already
# exist. You have to be root to do this, it isn't enough to just be an
# admin user.
# omaddfb # removed - bug 5281 - as it is done in ommakeom

# Configure the Internet gateway if it isn't already configured.
# We assume that if there are any routes on the MIME and TNEF queues
# that we don't need to configure the Internet gateway. Note that
# omshowrt exits normally even if there are no routes for the named
# queue which is why we need grep. We don't bother with the SHAR and
# UUENCODE mailnodes since they are only needed for interoperation with
# old Scalix installations.
if omshowrt -q UNIX 2>/dev/null | grep MIME >/dev/null
then :
else
    omconfux -m internet
    restart="$restart internet"
fi
if omshowrt -q UNIX 2>/dev/null | grep TNEF >/dev/null
then :
else
    omconfux -t internet,tnef
    restart="$restart internet"
fi

# If we haven't got a pool of userIDs then we need to allocate some.
# Actually this is slightly imperfect -- we might have run out of
# entries in the pool so creating some new ones will tend to overlap the
# existing ones. However, we can live with this since we're unlikely
# to be creating users with the wizard on a well-established Scalix
# system.
#
# The awk script here looks for the largest gap between adjacent UIDs in
# the password file and allocates the space for the Scalix IDs from
# that range. Typically, however, we'll find that the biggest range
# is up against the end of the password file and we allocate the UIDs
# 59000..59999.
if [ "$(omadmidp -j)" -eq 0 ]
then
    eval $(omeval pwget | awk -F: '{print $3}' | sort -nu | awk '
	BEGIN {
	    uid = 0; max = 0
	}
	/^[0-9]+$/ {
	    if ($0-uid-1 > max) {
		max = $0-uid-1;
		maxuid = $0;
	    }
	    uid = $0;
	}
	END {
	    if (uid < 60000 && 60000 - uid > max) {
		max = 60000 - uid;
		maxuid = 60000;
	    }
	    if (max > 1000) {
		max = 1000;
	    }
	    print "omadmidp", "-a", "-s", maxuid-max, "-n", max;
	}')
fi

# We're done now, so make sure any changed services are stopped and
# so that they will be restarted by the wizard.
if [ -n "$restart" ]
then
    omoff -d0 -sw $(echo $restart | tr ' ' '\n' | sort -u)
fi

# Make sure web config is set up
omwebconf -il C
