#!/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
#
#
#
#
#
#
# Copyright (c) 2007 Scalix Corporation. All Rights Reserved.
#
# Author: Nick Atkins
# Created: Oct 30 2004
#

# Scalix Installer README
#
# If you are reading this but you are expecting to be downloading or executing the installer please follow
# these instructions:
#
# (a) Downloading.  With your browser, please *right-click* the file-name, choose the "Save As" option and specify a
#                   download location.  Then execute the script as root using "bash" or "sh".
#
# (b) Executing.    For some reason the script is not actually executing but is showing its contents.  Make sure
#                   you execute the script as root using "bash" or "sh".
#
# If you are looking at this file for any other reason please stop it at once as that is not allowed.

# Everything below this line controls the self-extracting installer and should not be tampered with or ideally even
# looked at.  This script source code remains Copyright (c) 2006 Scalix Corporation. All Rights Reserved..

OUR_NAME="Scalix Installer"

# check for --version and --run-under-debugger options

VERSION_ONLY=false
RUN_UNDER_DEBUGGER=false

scalix_args=""
for p in $*; do
    case "$p" in
        --run-under-debugger )
            RUN_UNDER_DEBUGGER=true
        ;;
        --version )
            VERSION_ONLY=true
            scalix_args="$scalix_args $p"
        ;;
        * )
            scalix_args="$scalix_args $p"
        ;;
    esac
done

echo ""

# delete the temp dir
function cleanup {
    if [ -d "$WRKDIR" ]; then
        my_echo "$OUR_NAME - cleaning up..."
        rm -rf "$WRKDIR"
    fi
}

# die a horrible death (with message)
function die {
    if [ -n "$1" ]; then
        echo $1
    fi
    cleanup
    exit 1
}

# don't echo if we are only displaying the version
function my_echo {
    if [ $VERSION_ONLY == false ]; then
        echo $1
    fi
}

# make sure which is available independent of /usr/bin/which (Bug #14020)
alias which='type -p'

# determine Python executable
PYTHON_BIN=`which python 2> /dev/null`
if [ $? -ne 0 ]; then
    die "Error: Can't find Python in $PATH. Aborting."
fi

# check we have Perl somewhere
PERL_BIN=`which perl 2> /dev/null`
if [ $? -ne 0 ]; then
    die "Error: Can't find perl. It must be installed to use the Scalix Installer. Aborting."
fi

# fix Perl headers we need - not required -- see bug 5173
#pushd /usr/include &> /dev/null
#h2ph sys/syscall.h &> /dev/null
#h2ph asm/unistd.h &> /dev/null
#popd &> /dev/null

# determine Python version and build prefix and check minimal version:
PYTHON_VERSION=`$PYTHON_BIN -V 2>&1 | sed -e 's/Python //g'`
PY_SUBDIR="py"

MIN_VERSION="2.2.2"

if [[ $PYTHON_VERSION < $MIN_VERSION ]]; then
    die "Error: Python $PYTHON_VERSION is not supported, need at least version $MIN_VERSION. Aborting."    
fi

# check we are root
WHOAMI=`whoami`
if [ $WHOAMI != 'root' ]; then
    die "Error: Must be root to run $OUR_NAME"
fi

# create a temp directory to extract to.
WRKDIR=`mktemp -d /tmp/scalix-wizard.XXXXXX`

SKIP=`awk '/^__ARCHIVE_FOLLOWS__/ { print NR + 1; exit 0; }' $0`

my_echo "$OUR_NAME - extracting archive, please wait..."

# Take the TGZ portion of this file and pipe it to tar.
tail -n +$SKIP $0 | tar xzm -C "$WRKDIR"

ORIG_DIR=`pwd`

export PYTHONPATH=$WRKDIR/$PY_SUBDIR/lib:$WRKDIR/$PY_SUBDIR/logging:$PYTHONPATH

cd $WRKDIR/$PY_SUBDIR

# determine version
VERSION=`$PYTHON_BIN "$WRKDIR/$PY_SUBDIR/sxversion.py"`
my_echo "$OUR_NAME - starting version $VERSION..."
my_echo "$OUR_NAME - using Python $PYTHON_VERSION ($PYTHON_BIN)."

# determine if we should run under debugger
if [ $RUN_UNDER_DEBUGGER == true ]; then
    echo "run" >$WRKDIR/gdb_commands
    EXEC_WRAPPER="gdb --command=$WRKDIR/gdb_commands --args"
else
    EXEC_WRAPPER=""
fi

# call merlin with options
$EXEC_WRAPPER $PYTHON_BIN -W ignore $WRKDIR/$PY_SUBDIR/sxmerlin.py --cwd=$ORIG_DIR --exec=$0 --logdir=/var/log $scalix_args

cd "$ORIG_DIR"

cleanup

my_echo "$OUR_NAME - done."

exit

__ARCHIVE_FOLLOWS__
