#!/bin/bash
#
#  umx-start
#
#  Start Userful Multiplier
# 
#  Copyright (c) 2002-2009 Userful Corporation Ltd.
#
logfile="/var/log/userful-start.log"
XINITRC="/opt/userful/umx.xinitrc"

export LANG=C
export PATH=$PATH:/opt/userful/bin:/usr/bin:/usr/bin/X11:/usr/local/bin

##
## Check Operating System
/opt/userful/bin/dm-check-os.sh
/opt/userful/bin/dm-supported-os.sh
RET=$?
if [ $RET -ne 0 ]; then # Unsupported Linux distro
	echo -e "\n  Userful Multiplier doesn't currently support your Linux
distribution. To install Desktop Multiplier manually, 
please follow the detailed installation instructions at:
http://www.userful.com/products/dm-config"

        # TODO:
        #  If exits, it will be respawn very quickly by init process
	exit 1
fi

##
## Check X type (single or multi)
if [ -f /etc/sysconfig/xtype ]; then
	source /etc/sysconfig/xtype

    if [ "$XTYPE" = MULTI -o "$XTYPE" = multi ]; then
        CURRENTXTYPE=multi
    elif [ "$XTYPE" = SINGLE -o "$XTYPE" = single ]; then
        CURRENTXTYPE=single
	fi
fi
if [ -z "$CURRENTXTYPE" ]; then
    CURRENTXTYPE=single
fi
export CURRENTXTYPE

##
## Check Display Manager
if [ -e /etc/userful/currentdm ]; then
    source /etc/userful/currentdm
    if [ -z "$CURRENTDM" ]; then
	    CURRENTDM=gdm
    fi
    export CURRENTDM
fi

#################
# Single Mode
#################
if [ "$CURRENTXTYPE" = "single" ]; then
	# Restore system for single X Mode
    if [ -x /opt/userful/bin/umx-restore-system.sh ]; then
        /opt/userful/bin/umx-restore-system.sh
    fi

	# Configure mga driver
	#/opt/userful/bin/dm-config-mga-driver.sh $CURRENTXTYPE

	# Configure hotplug agents 
	/opt/userful/bin/dm-config-hotplug.sh $CURRENTXTYPE

	# configure multistation devices
	/opt/userful/bin/multistation-devices-config.sh

	#  Configure Display Manager
	#  Configuring display manager is different based on the 
	#+ current X type (single or multi), current display manager ( GDM,
	#+ KDM or XDM) and distro  (Fedora, SuSE, Debian, Ubuntu etc.).
	#  Need to update dm-config-display-manager to support each distro.
	/opt/userful/bin/dm-config-display-manager.sh $CURRENTXTYPE $CURRENTDM

	#  Start Display Manager
	#  This is the last step of startup script.  Each distro has different way 
	#+ to start system graphically.
	#  Need to update dm-start-display-manager to support each distro.	
	source /opt/userful/bin/dm-start-display-manager.sh

#################
# Multi Mode
#################
elif [ "$CURRENTXTYPE" = "multi" ]; then  # In case of "multi" X

	# Initailization
    /opt/userful/bin/dm-initialize.sh

    if [ -z "$CURRENTDM" ]; then
        source /etc/userful/currentdm
        export CURRENTDM
    fi

	# make backup for license key if exist
	/opt/userful/bin/dm-backup-key.sh

    ##
    ## Change VT to hide debug messages.
    if which chvt > /dev/null; then
        chvt 7
    fi
	# Create X config file (userful.Mxorg.conf) 
	/opt/userful/bin/dm-config-mx.sh

	# delete first install file
	rm -f /etc/userful/DM-install

	# configure mga driver
	#/opt/userful/bin/dm-config-mga-driver.sh $CURRENTXTYPE

	## Start Multi-X server
	#xinit $XINITRC -- /opt/MX11R7/bin/X :0 -br -logfile /var/log/1box-server.log -nolisten tcp -config userful.Mxorg.conf
    /opt/userful/bin/umx-start-x.sh &

    ## TODO:
    ## Check X start here and send debug info if required.
    /opt/userful/bin/waiting-for.sh --xorg mx

    #sleep 999d
    wait

else
    # Shouldn't be reach here
	echo "XTYPE=MULTI" >  /etc/sysconfig/xtype
fi
