#!/bin/bash
#
# prefdm_multi
# 
#    Initial start-up script for Desktop Multiplier. 
#
#	Copyright (c) 2005-2006 Usreful Corporation Ltd.
#
#   Jason Kim <jason@userful.com>   
#
# This is obsoleted. User umx-start instead.
logfile="/var/log/userful-start.log"

trap "" 1 16 17 # ignore HUP USR1 USR2 signals


# Check OS name and version
/opt/userful/bin/dm-check-os.sh
/opt/userful/bin/dm-supported-os.sh
RET=$?

if [ $RET -ne 0 ]; then # Unsupported Linux distro
	grep "^#" /etc/userful/userful-supported-distros >> $logfile
	echo -e "\n   The Desktop Multiplier install script does not currently
support your Linux distribution. To install Desktop Multiplier
manually, please follow the detailed installation instructions at:
http://www.userful.com/products/dm-config" >> $logfile
	chvt 1
	exit 1
fi

# Check current X type
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

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

#############################################################
# configure and start the system based on the current X type.
#############################################################

# Single X 
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 pam
	/opt/userful/bin/dm-config-pam.sh $CURRENTDM

	#  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 X 
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

	# 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
	/opt/userful/bin/dm-start-mx.sh

	# configure Hotplug agent
	/opt/userful/bin/dm-config-hotplug.sh $CURRENTXTYPE

	# configure multistation devices
	/opt/userful/bin/multistation-devices-config.sh
	
	# configure pam
	/opt/userful/bin/dm-config-pam.sh $CURRENTDM

	# Start Daemons (USBConfigDaemon, kbd-daemon, mse-daemon, MXSigDaemon)
	/opt/userful/bin/dm-start-daemon.sh $CURRENTDM

	# finalize before start Display Manager
	/opt/userful/bin/dm-finalize.sh

	#  Configure Display Manager
	#  Configuring display manager is different based on the 
	#+ current X type (single or multi), current display manager type ( GDM,
	#+ KDM or XDM) and distro type (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

else
	echo "(EE) Unsupported X type"
	echo "(EE) Script can't be reached here"
fi
