#!/bin/bash
#
# Copyright (c) 2005-2006 by Userful Corporation
#
# Name: MXsetup
# Author: Jason Kim
# Description:
# configure the systems after installation
#

OSNAME=
OSVER=
GDMVER=
KDMVER=

# check Display Manager version, called only when the current OS is Fedora.
check_dm()
{
	if rpm -q gdm > /dev/null 2>&1 ; then
		GDMVER=`rpm -q --qf %{VERSION} gdm`
	fi

	if rpm -q kdebase > /dev/null 2>&1 ; then
		KDMVER=`rpm -q --qf %{VERSION} kdebase`
	fi
}

# Configure Display Mangager, Hotplug agents and Udev rules
configurefiles()
{
	# Update grub.conf to disable rhgb in FC
	if [ -f /boot/grub/grub.conf ]; then
		if [ ! -e /boot/grub/grub.conf.multi-save ]; then
			cp /boot/grub/grub.conf /boot/grub/grub.conf.multi-save
		fi
		sed -i -e "s/rhgb[[:space:]]*quiet//g" /boot/grub/grub.conf
		sed -i -e "s/rhgb//g" /boot/grub/grub.conf
	fi

	if [ -e /opt/userful/bin/umx-start -a ! -e /etc/X11/umx-start ]; then
		ln -s /opt/userful/bin/umx-start /etc/X11/umx-start
    fi

    ####################################
    # Use /tmp/distro.conf if it exists.
    # - Any new distros will use umx-distro.conf to set their specific configuration for UMx
    # - umx-distro-conf.py will create distro.conf based on the current OSNAME and OSVER.
    ####################################
	if [ -e /tmp/distro.conf ]; then
        source /tmp/distro.conf

        ##
        ## Backup the configuration of Display Manager
        ## DMCONFIG is from distro.conf (umx-distro.conf)
        if [ -n "$DMCONFIG" ]; then
            if [ -e $DMCONFIG ]; then
                # If $DMCONFIG exists, make a backup file.
                if [ ! -e $DMCONFIG.multi-save -a ! -e $DMCONFIG.multi-remove ]; then
                    cp -f $DMCONFIG  $DMCONFIG.multi-save
                fi
            else
                # If $DMCONFIG NOT exists, copy one from /etc/userful/setup/ directory.
                DMCONFIG_NAME=`basename $DMCONFIG`
                cp -f /etc/userful/setup/$DMCONFIG_NAME.$OSNAME-$OSVER ${DMCONFIG/DMCONFIG_NAME/}
                cp -f $DMCONFIG $DMCONFIG.multi-remove
            fi
        fi

        ## 
        ## Set umx-init and umx-start based on the "STARTUP" type.
        ## STARTUP is either inittab or upstart.
        ## inittab: SLED 11
        ## upstart: Ubuntu 9.10
        if [ "$STARTUP" == "inittab" ]; then

            ## Set umx-init
            if [ -n "$UMXINIT" ]; then

                # Get the file name
                UMXINIT_NAME=`basename $UMXINIT`

                if [ ! -e $UMXINIT ]; then
                    cp -f /etc/userful/setup/$UMXINIT_NAME.$OSNAME-$OSVER ${UMXINIT/UMXINIT_NAME/}
                fi

                ## Register umx-init script
                if which chkconfig > /dev/null ; then
                    chkconfig --add $UMXINIT_NAME > /dev/null
                fi
            fi

            ## Set umx-start
            if [ -n "$RUNLEVEL" ]; then
                if [ ! -e /etc/inittab.multi-save ]; then
                    cp -f /etc/inittab  /etc/inittab.multi-save
                fi
			    sed -i -e "/^x:.*/d" /etc/inittab
                # Append umx-start into inittab
			    echo "x:$RUNLEVEL:respawn:/etc/X11/umx-start" >> /etc/inittab
            fi

        elif [ "$STARTUP" == "upstart" ]; then

            ## Set umx-init
            if [ -n "$UMXINIT" -a ! -e $UMXINIT ]; then
                ## Check UMXINIT and copy it to the correct directory.
                UMXINIT_NAME=`basename $UMXINIT`
                cp -f /etc/userful/setup/$UMXINIT_NAME.$OSNAME-$OSVER ${UMXINIT/UMXINIT_NAME/}
            fi

            ## Set umx-start
            if [ -n "$UMXSTART" -a ! -e $UMXSTART ]; then
                ## Check UMXSTART and copy it to the correct directory.
                UMXSTART_NAME=`basename $UMXSTART`
                cp -f /etc/userful/setup/$UMXSTART_NAME.$OSNAME-$OSVER ${UMXSTART/UMXSTART_NAME/}
            fi


        fi

	# Mandrake linux
    elif [ "$OSNAME" == "mandrake" ]; then
		if [ ! -e /etc/X11/umx-start ]; then
			ln -sf /opt/userful/bin/umx-start /etc/X11/umx-start
			sed -i -e "/^x:.*/d" /etc/inittab
			echo "x:5:respawn:/etc/X11/umx-start" >> /etc/inittab
			mv -f /etc/init.d/dm /etc/init.d/dm.multi-save
		fi

	# Fedora Core
	elif [ "$OSNAME" == "fedora" ]; then 
		if [ ! -e /etc/X11/umx-start ]; then
			ln -s /opt/userful/bin/umx-start /etc/X11/umx-start
			sed -i -e "s/^x:.*/x:5:respawn:\/etc\/X11\/umx-start/g" /etc/inittab
		fi
		mkdir -p /etc/X11/gdm

		check_dm

		if [ "$GDMVER" == "2.6.0.8" ]; then
			cp -f /etc/userful/setup/gdm.conf.multi.fc-2.6.0.8  /etc/X11/gdm/gdm.conf.multi
		else
			cp -f /etc/userful/setup/gdm.conf.multi.fc  /etc/X11/gdm/gdm.conf.multi
		fi

		if [ "$KDMVER" == "3.4.2" ]; then
			cp -f /etc/userful/setup/kdmrc.multi.fc-3.4.2 /etc/X11/xdm/kdmrc.multi
		else
			cp -f /etc/userful/setup/kdmrc.multi.fc  /etc/X11/xdm/kdmrc.multi
		fi

	# RHEL, CentOS
	elif [ "$OSNAME" == "redhat" -o "$OSNAME" == "centos" ]; then 
		if [ ! -e /etc/X11/umx-start ]; then
			ln -s /opt/userful/bin/umx-start /etc/X11/umx-start
			sed -i -e "s/^x:.*/x:5:respawn:\/etc\/X11\/umx-start/g" /etc/inittab
		fi
		mkdir -p /etc/X11/gdm
		cp -f /etc/userful/setup/gdm.conf.multi.fc  /etc/X11/gdm/gdm.conf.multi
		cp -f /etc/userful/setup/kdmrc.multi.fc  /etc/X11/xdm/kdmrc.multi

	# SuSE, Novell and JDS
	elif [ "$OSNAME" == "suse" -o "$OSNAME" == "novell" -o "$OSNAME" == "jds" ]; then
		if [ ! -e /etc/X11/umx-start ]; then
			ln -s /opt/userful/bin/umx-start /etc/X11/umx-start
			sed -i -e "/^x:.*/d" /etc/inittab
			echo "x:5:respawn:/etc/X11/umx-start" >> /etc/inittab

            if [[ "$OSVER" == "10.2" ]]; then
                rm -f /etc/init.d/rc5.d/S10xdm
                rm -f /etc/init.d/rc5.d/K12xdm
            elif [ "$OSVER" == "10.3" -o "$OSVER" == "11.0" ]; then
                chkconfig -d xdm 5
                chkconfig -d earlyxdm 5
            else
			    mv -f /etc/init.d/xdm /etc/init.d/xdm.multi-save
            fi
		fi

		if [ "$OSNAME" == "jds" ]; then #JDS
			if [ -e /etc/X11/gdm ]; then
				cp -f /etc/userful/setup/gdm.conf.multi.sun  /etc/X11/gdm/gdm.conf.multi
			fi
		elif [ "$OSNAME" == "novell" ]; then # Novell
			if [ -e /etc/opt/gnome/gdm ]; then
    			cp -f /etc/userful/setup/gdm.conf.multi.novell  /etc/opt/gnome/gdm/gdm.conf.multi
			fi
			if [ -e /etc/opt/kde3/share/config/kdm ]; then
    			cp -f /etc/userful/setup/kdmrc.multi.novell  /etc/opt/kde3/share/config/kdm/kdmrc.multi
			fi
		else # SuSE
			if [ "$OSVER" == "9.3" ]; then
				if [ -e /etc/opt/gnome/gdm ]; then
    				cp -f /etc/userful/setup/gdm.conf.multi.suse-9.3 /etc/opt/gnome/gdm/gdm.conf.multi
				fi
				if [ -e /etc/opt/kde3/share/config/kdm ]; then
    				cp -f /etc/userful/setup/kdmrc.multi.suse-9.3  /etc/opt/kde3/share/config/kdm/kdmrc.multi 
				fi
			else # lower version than 9.3
				if [ -e /etc/opt/gnome/gdm ]; then
    				cp -f /etc/userful/setup/gdm.conf.multi.suse  /etc/opt/gnome/gdm/gdm.conf.multi
				fi
				if [ -e /etc/opt/kde3/share/config/kdm ]; then
    				cp -f /etc/userful/setup/kdmrc.multi.suse  /etc/opt/kde3/share/config/kdm/kdmrc.multi
				fi
			fi
		fi

	# Xandros, Mepis
	elif [ "$OSNAME" == "xandros" -o "$OSNAME" == "mepis" ]; then
		if [ ! -e /etc/X11/umx-start ]; then
			ln -s /opt/userful/bin/umx-start /etc/X11/umx-start
			sed -i -e "/^x:.*/d" /etc/inittab

			if [ "$OSNAME" == "xandros" ]; then #xandros
				echo "x:2:respawn:/etc/X11/umx-start" >> /etc/inittab

				if [ "$OSVER" == "3.0" ]; then
					if [ -e /etc/kde3/kdm ]; then
						cp -f /etc/userful/setup/kdmrc.multi.xandros  /etc/kde3/kdm/kdmrc.multi
						cp -f /etc/X11/xdm/Xservers.multi  /etc/kde3/kdm/Xservers.multi
					fi
				fi
			else #mepis
				echo "x:5:respawn:/etc/X11/umx-start" >> /etc/inittab
				if [ -e /etc/kde3/kdm ]; then
					cp -f /etc/userful/setup/kdmrc.multi.mepis  /etc/kde3/kdm/kdmrc.multi
					cp -f /etc/X11/xdm/Xservers.multi  /etc/kde3/kdm/Xservers.multi
				fi
			fi
			mv -f /etc/init.d/kdm /etc/init.d/kdm.multi-save
		fi

	# Ubuntu, Linspire ,Debian, freespire, Dreamlinux
    # Mepis 6 uses /etc/lsb-release, and it has "Ubuntu" string in it.
	elif [ "$OSNAME" == "ubuntu" -o "$OSNAME" == "linspire" -o \
		   "$OSNAME" == "debian" -o "$OSNAME" == "freespire" -o \
		   "$OSNAME" == "morphix" ]; then 
		if [ ! -e /etc/X11/umx-start ]; then
			ln -s /opt/userful/bin/umx-start /etc/X11/umx-start

			if [ -e /etc/init.d/gdm ]; then
                mv -f /etc/init.d/gdm /etc/init.d/gdm.multi-save
			elif [ -e /etc/init.d/kdm ]; then
                mv -f /etc/init.d/kdm /etc/init.d/kdm.multi-save
            fi

			if [ -e /etc/inittab ]; then
				sed -i -e "/^x:.*/d" /etc/inittab
                # Ubuntu uses runlevel 2, and Mepis 6 uses runlevel 5 for the graphical boot.
				echo "x:25:respawn:/etc/X11/umx-start" >> /etc/inittab
			elif [ -d /etc/event.d ]; then # For the new startup method.
                if [ "$OSNAME" == "ubuntu" -a "$OSVER" == "6.10" ]; then
                    # Ubuntu 6.10 has old version of Upstart. 0.2.7-7
                    cp -f /etc/userful/setup/dm-start.upstart.obsolete /etc/event.d/dm-start
                else
                    # Version of upstart in Ubunut 7.04 -- 0.3.8.1
                    cp -f /etc/userful/setup/dm-start.upstart /etc/event.d/dm-start
                    cp -f /etc/userful/setup/umx-init.upstart /etc/event.d/umx-init
                fi
			fi
		fi
		if [ -e /etc/gdm -a "$OSNAME" == "ubuntu" ]; then
			cp -f /etc/userful/setup/gdm.conf.multi.ubuntu  /etc/gdm/gdm.conf.multi
		elif [ -e /etc/gdm -a "$OSNAME" == "linspire" ]; then
			cp -f /etc/userful/setup/gdm.conf.multi.linspire  /etc/gdm/gdm.conf.multi
		elif [ -e /etc/gdm -a "$OSNAME" == "debian" ]; then
			cp -f /etc/userful/setup/gdm.conf.multi.debian  /etc/gdm/gdm.conf.multi
		fi

	elif [ "$OSNAME" == "freedows" ]; then
		if [ ! -e /etc/X11/umx-start ]; then
			ln -s /opt/userful/bin/umx-start /etc/X11/umx-start
			sed -i -e "s/^x:.*/x:5:respawn:\/etc\/X11\/umx-start/g" /etc/inittab
		fi

	else
		if [ ! -e /etc/X11/umx-start ]; then
			ln -s /opt/userful/bin/umx-start /etc/X11/umx-start
		fi

		echo ""
		echo "This Linux system is not supported for auto-configuration"
		echo "You may need to configure following area:"
		echo "  1. Start-Up Procedure"
		echo "  2. Display Manager: XDM"
		echo "  3. Hotplug"
		echo "  4. ScreenSaver: blank screen"
		echo ""
	fi

    #
    # Hotplug agents are obsolete in the new distros.
    #
	# backup the original input.agent with .multi-save appendix
	if [ -e /etc/hotplug.d/input_device/50-input_device.hotplug ]; then # SuSE-9.3
		if [ ! -e /etc/hotplug.d/input_device/50-input_device.hotplug.multi-save ]; then
			cp -f /etc/hotplug.d/input_device/50-input_device.hotplug \
		      	/etc/hotplug.d/input_device/50-input_device.hotplug.multi-save 2> /dev/null
		fi
	elif [ -e /etc/hotplug/input.agent ]; then
		if [ ! -e /etc/hotplug/input.agent.multi-save ]; then
			cp -f /etc/hotplug/input.agent /etc/hotplug/input.agent.multi-save 2> /dev/null
		fi
	fi
	# backup the original usb.agent with .multi-save appendix
	if [ -e /etc/hotplug.d/usb/50-usb.hotplug ]; then # SuSE-9.3
		if [ ! -e /etc/hotplug.d/usb/50-usb.hotplug.multi-save ]; then
			cp -f /etc/hotplug.d/usb/50-usb.hotplug \
		      	/etc/hotplug.d/usb/50-usb.hotplug.multi-save 2> /dev/null
		fi
	elif [ -e /etc/hotplug/usb.agent ]; then
		if [ ! -e /etc/hotplug/usb.agent.multi-save ]; then
			cp -f /etc/hotplug/usb.agent /etc/hotplug/usb.agent.multi-save 2> /dev/null
		fi
	fi

	# update hotplug agent or add Udev rules
	if [ "$OSNAME" == "suse" -a "$OSVER" == "9.3" ] &&
	   [ -e /etc/hotplug.d/usb/50-usb.hotplug.multi-save ]; then
		pushd /etc/hotplug.d/ > /dev/null
	   	cp -f ./input_device/50-input_device.hotplug.multi-save ./input_device/50-input_device.hotplug.multi
	   	cp -f ./usb/50-usb.hotplug.multi-save ./usb/50-usb.hotplug.multi

		cat /etc/userful/setup/hotplug.userful-patch.suse-9.3 | patch -p0 > /dev/null 2>&1
		popd > /dev/null

	elif [ "$OSNAME" == "fedora" -a "$OSVER" == "2" ] ||
	     [ "$OSNAME" == "fedora" -a "$OSVER" == "3" ] ||
	     [ "$OSNAME" == "fedora" -a "$OSVER" == "4" ] ||
	     [ "$OSNAME" == "redhat" ] ||
	     [ "$OSNAME" == "centos" -a "$OSVER" == "4" ] ||
	     [ "$OSNAME" == "xandros" -a "$OSVER" == "3.0" ] ||
	     [ "$OSNAME" == "ubuntu" -a "$OSVER" == "5.04" ] ||
	     [ "$OSNAME" == "linspire" ] ||
	     [ "$OSNAME" == "debian" -a "$OSVER" == "3.1" ] ||
	     [ "$OSNAME" == "freedows" ] ||
		 [ "$OSNAME" == "mepis" ]; then
		pushd /etc/hotplug/ > /dev/null
	   	cp -f usb.agent.multi-save usb.agent.multi
	   	cp -f input.agent.multi-save input.agent.multi
		cat /etc/userful/setup/hotplug.userful-patch | patch -p0 > /dev/null 2>&1
		popd > /dev/null

	elif [ "$OSNAME" == "novell" -a "$OSVER" == "9" ] ||
	     [ "$OSNAME" == "suse" -a "$OSVER" == "9.1" ] ||
		 [ "$OSNAME" == "jds" ]; then

		pushd /etc/hotplug/ > /dev/null
	   	cp -f usb.agent.multi-save usb.agent.multi
	   	cp -f input.agent.multi-save input.agent.multi
		cat /etc/userful/setup/hotplug.userful-patch.suse | patch -p0 > /dev/null 2>&1
		popd > /dev/null

	else # use udev rule for the relatively new Linux ditros
		 
		cp -f /etc/userful/setup/99-userful-hotplug.rules /etc/udev/rules.d/

	fi

    # Create softlink dm.conf for the backward compatibility
    if [ -e /etc/userful/umx.conf ]; then
        ln -sf /etc/userful/umx.conf /etc/userful/dm.conf
    fi
}

#############################################################
# pkginstall() - backup files before configuring the system 
#############################################################
pkginstall()
{
	# Make backups
	# /etc/inittab
	if [ -e /etc/inittab -a ! -e /etc/inittab.multi-save ]; then
		cp -f /etc/inittab /etc/inittab.multi-save 2> /dev/null
	fi

    ###################
    # These will be obsolete in the future.
	# Hotplug agents
    # These are for old distros. With the new distro, DM uses udev rules and hal fdi for hotplug.
    ###################
	if [ -e /etc/hotplug.d/input_device/50-input_device.hotplug ]; then # SuSE-9.3
		if [ ! -e /etc/hotplug.d/input_device/50-input_device.hotplug.multi-save ]; then
			cp -f /etc/hotplug.d/input_device/50-input_device.hotplug \
			      /etc/hotplug.d/input_device/50-input_device.hotplug.multi-save 2> /dev/null
		fi
	elif [ -e /etc/hotplug/input.agent ]; then
		if [ ! -e /etc/hotplug/input.agent.multi-save ]; then
			cp -f /etc/hotplug/input.agent /etc/hotplug/input.agent.multi-save 2> /dev/null
		fi
	fi
	if [ -e /etc/hotplug.d/usb/50-usb.hotplug ]; then # SuSE-9.3
		if [ ! -e /etc/hotplug.d/usb/50-usb.hotplug.multi-save ]; then
			cp -f /etc/hotplug.d/usb/50-usb.hotplug \
			      /etc/hotplug.d/usb/50-usb.hotplug.multi-save 2> /dev/null
		fi
	elif [ -e /etc/hotplug/usb.agent ]; then
		if [ ! -e /etc/hotplug/usb.agent.multi-save ]; then
			cp -f /etc/hotplug/usb.agent /etc/hotplug/usb.agent.multi-save 2> /dev/null
		fi
	fi
    ########################3

    #######
	# PAM #
    #######
    # Update pam module for Display Managers if it exists.
	if [ -e /etc/pam.d/gdm -a ! -e /etc/pam.d/gdm.multi-save ]; then
	    cp -f /etc/pam.d/gdm /etc/pam.d/gdm.multi-save 2> /dev/null
	fi
	if [ -e /etc/pam.d/xdm -a ! -e /etc/pam.d/xdm.multi-save ]; then
	    cp -f /etc/pam.d/xdm /etc/pam.d/xdm.multi-save 2> /dev/null
	fi
	if [ -e /etc/pam.d/kde -a ! -e /etc/pam.d/kde.multi-save ]; then
	    cp -f /etc/pam.d/kde /etc/pam.d/kde.multi-save 2> /dev/null
	fi
	if [ -e /etc/pam.d/kdm -a ! -e /etc/pam.d/kdm.multi-save ]; then
	    cp -f /etc/pam.d/kdm /etc/pam.d/kdm.multi-save 2> /dev/null
	fi

    #######
	# XDM #
    #######
    # Update XDM config files.
	if [ -e /etc/X11/xdm/xdm-config -a ! -e /etc/X11/xdm/xdm-config.multi-save ]; then
		cp -f /etc/X11/xdm/xdm-config /etc/X11/xdm/xdm-config.multi-save 2> /dev/null
	fi
	if [ -e /etc/X11/xdm/Xservers -a ! -e /etc/X11/xdm/Xservers.multi-save ]; then
		cp -f /etc/X11/xdm/Xservers /etc/X11/xdm/Xservers.multi-save 2> /dev/null
	fi

    #######
	# GDM #
    #######
    # Update GDM config files.
	if [ -e /etc/X11/gdm/gdm.conf -a ! -e /etc/X11/gdm/gdm.conf.multi-save ]; then #FC 234, RHEL, JDS
		cp -f /etc/X11/gdm/gdm.conf /etc/X11/gdm/gdm.conf.multi-save 2> /dev/null
	fi
	if [ -e /etc/gdm/custom.conf -a ! -e /etc/gdm/custom.conf.multi-save ]; then # FC6, SuSE10.3,11.0
		cp -f /etc/gdm/custom.conf /etc/gdm/custom.conf.multi-save 2> /dev/null
    fi
	if [ -e /etc/gdm/gdm.conf -a ! -e /etc/gdm/gdm.conf.multi-save ]; then # Ubuntu, Linspire, Debian, Freespire, Dreamlinux
		cp -f /etc/gdm/gdm.conf /etc/gdm/gdm.conf.multi-save 2> /dev/null
	fi
	if [ -e /etc/gdm/gdm.conf-custom -a ! -e /etc/gdm/gdm.conf-custom.multi-save ]; then # Ubuntu 7.10
		cp -f /etc/gdm/gdm.conf-custom /etc/gdm/gdm.conf-custom.multi-save 2> /dev/null
	fi
	if [ -e /etc/opt/gnome/gdm/gdm.conf -a ! -e /etc/opt/gnome/gdm.multi-save ]; then # SuSE, Novell
		cp -f /etc/opt/gnome/gdm/gdm.conf /etc/opt/gnome/gdm/gdm.conf.multi-save 2> /dev/null
    fi
	if [ -e /etc/opt/gnome/gdm/custom.conf -a ! -e /etc/opt/gnome/custom.multi-save ]; then # openSuSE 10.2
		cp -f /etc/opt/gnome/gdm/custom.conf /etc/opt/gnome/gdm/custom.conf.multi-save 2> /dev/null
    fi
	if [ -e /etc/gdm/gdm-cdd.conf -a ! -e /etc/gdm/gdm-cdd.conf.multi-save ]; then # Edubuntu 6.10
		cp -f /etc/gdm/gdm-cdd.conf /etc/gdm/gdm-cdd.conf.multi-save 2> /dev/null
    fi

    #######
	# KDM #
    #######
	if [ -e /etc/X11/xdm/kdmrc -a ! -e /etc/X11/xdm/kdmrc.multi-save ]; then # Fedora, RHEL
		cp -f /etc/X11/xdm/kdmrc /etc/X11/xdm/kdmrc.multi-save 2> /dev/null
	fi
	if [ -e /usr/share/config/kdm/kdmrc -a ! -e /usr/share/config/kdm/kdmrc.multi-save ]; then # Mandrake
		cp -f /usr/share/config/kdm/kdmrc /usr/share/config/kdm/kdmrc.multi-save 2> /dev/null
	fi
	if [ -e /etc/opt/kde3/share/config/kdm/kdmrc -a ! -e /etc/opt/kde3/share/config/kdm/kdmrc.multi-save ]; then # SuSE, Novell
		cp -f /etc/opt/kde3/share/config/kdm/kdmrc /etc/opt/kde3/share/config/kdm/kdmrc.multi-save 2> /dev/null
	fi
	if [ -e /etc/kde3/kdm/kdmrc -a ! -e /etc/kde3/kdm/kdmrc.multi-save ]; then # xandros, mepis, Ubuntu
		cp -f /etc/kde3/kdm/kdmrc /etc/kde3/kdm/kdmrc.multi-save 2> /dev/null
	fi
	if [ -e /etc/kde3/kdm/Xservers -a ! -e /etc/kde3/kdm/Xservers.multi-save ]; then # Xandros
		cp -f /etc/kde3/kdm/Xservers /etc/kde3/kdm/Xservers.multi-save 2> /dev/null
	fi

}

#################
# pkgupgrade()
#################
pkgupgrade()
{
	#echo "pkgupgrade function"
	touch /etc/upgrade-MX-utils
	pkginstall
}

##################
# texteula()
##################
texteula()
{
	clear
	more /tmp/userful-msg.txt
	INPUT=
	while [ "$INPUT" != "y" -a "$INPUT" != "n" ];
	do
		echo
		echo "Do you accept the terms of this license? [y/n]"
		read -n 1 INPUT
	done
	if [ "$INPUT" = "y" ]; then
		return 0;
	else
		echo
    	echo " License declined."
    	echo " Please run /opt/userful/bin/MXsetup to try again."
    	echo
		echo "1" > /opt/userful/bin/installvalue
    	exit 1
	fi
}

##################
# textwarn()
##################
textwarn()
{
	clear
	more /tmp/userful-msg.txt
	INPUT=
	while [ "$INPUT" != "y" ];
	do
		echo
		echo "Press \"y\" to continue...."
		read -n 1 INPUT
	done
}

#################
# licensecheck()
#################
licensecheck()
{
	# Accepted before?  Skip EULA
	if [ -f /etc/userful-MX.license ]; then
		echo "0" > /opt/userful/bin/installvalue
    	return 0
	fi
    # Is it during UD installation?
    # Process anaconda is running..
    if pgrep anaconda > /dev/null 2>&1; then
    	touch /etc/userful-MX.license
		echo "0" > /opt/userful/bin/installvalue
    	return 0
    fi

	if which lspci > /dev/null 2>&1; then
		GREP_VID=`lspci -n | grep -c 1002`
	else
		GREP_VID=`/opt/userful/bin/listpci -n | grep -c 1002`
	fi
	uname -r | tr "[a-z]" "[A-Z]" | grep SMP > /dev/null
	SMP_KERNEL=$?

	if [ $GREP_VID -gt 1 -a $SMP_KERNEL -eq 0 ]; then # multi radeon cards, smp kernel
		# create warning msg file
		echo "System Information" > /tmp/userful-msg.txt 
		echo "=====================" >> /tmp/userful-msg.txt
		echo "" >> /tmp/userful-msg.txt
		echo "Kernel Version :   `uname -r`" >> /tmp/userful-msg.txt
		echo "" >> /tmp/userful-msg.txt
		if which lspci > /dev/null 2>&1; then
			CARDNUM=`lspci -n | grep -c "0300: "`
		else
			CARDNUM=`/opt/userful/bin/listpci -n | grep -c "0300: "`
		fi
		echo "Number of Graphic Cards:  $CARDNUM" >> /tmp/userful-msg.txt

		echo "" >> /tmp/userful-msg.txt
		echo "" >> /tmp/userful-msg.txt
		echo "Certain Linux-distro / motherboard combinations have known issues with
multiple Radeon graphics cards using the SMP (Symmetric Multi-Processing)
Linux Kernel. If you are using SMP with the Radeon chipset and encounter
problems, we recommend switching to a non SMP kernel. This is a bug with the
Radeon driver and is not specific to Userful's software.

Note: Intel P4 Hyper-threaded systems also use the SMP kernel and the
Novell/SuSE Linux will install the SMP kernel by default if a Hyper-threaded
P4 processor is detected. In this case you must manually install the standard
(non-SMP) Kernel should issues arise using the SMP kernel." >> /tmp/userful-msg.txt

		/opt/userful/bin/show-msg -warn 
		E=$?
		# 2    X not available
		# 127  gtk+ or other library dependency not available
		if [ $E = 2 ] || [ $E = 127 ]; then
    		# GUI mode failure and manual invokation.
    		if [ "$1" = "--manual" ]; then
        		# Text Mode Information
        		textwarn
    		fi
		fi
	fi
	
	cp /opt/userful/bin/Userful-EULA.txt /tmp/userful-msg.txt

	/opt/userful/bin/show-msg -eula 
	E=$?
	# 1    Decline
	# 2    X not available
	# 127  gtk+ or other library dependency not available
	if [ $E = 2 ] || [ $E = 127 ]; then
    	# GUI mode failure and manual invokation.
    	if [ "$1" = "--manual" ]; then
       	    # Text Mode EULA
        	texteula
        	if [ $? = 0 ]; then 
				E=0; 
			fi
    	fi
	fi

	rm -f /tmp/userful-msg.txt

	if [ $E = 1 ]; then
		echo
    	echo " License declined."
    	echo " Please run /opt/userful/bin/MXsetup to try again."
    	echo
		echo "1" > /opt/userful/bin/installvalue
    	return 1

	elif [ $E = 2 ] || [ $E = 127 ]; then
		# General Failure. User hasn't seen EULA yet.
		echo
    	echo " NOTICE:"
    	echo " Please run /opt/userful/bin/MXsetup "
    	echo " to enable Userful 1-Box system."
    	echo
		echo "2" > /opt/userful/bin/installvalue
    	return 1
	fi

	if [ $E = 0 ]; then
		# Accept
    	touch /etc/userful-MX.license
		echo "0" > /opt/userful/bin/installvalue
    	return 0
	fi
	echo "ERROR: Script should never reach here.  Please collect as much "
	exit 0
}


##################
# deleteall()
##################
deleteall()
{
	# back to normal system
	if [ -e /etc/upgrade-MX-utils ]; then
		# part of the upgrade procedure
		rm -f /etc/upgrade-MX-utils
		return 0
	fi

	# find path for default.session
	if [ -f /usr/share/gnome/default.session ]; then # FC4
		default_session="/usr/share/gnome/default.session"
	elif [ -f /opt/gnome/share/gnome/default.session ]; then #SLED10
		default_session="/opt/gnome/share/gnome/default.session"
	fi
	# Resotre $default_session 
	if [ -f $default_session.multi-save ]; then
		cp -f $default_session.multi-save $default_session
		rm -f $default_session.multi-save
	fi

	# restore gconf properties for users profile
	WHO=` who -u | cut -d" " -f1 | uniq`
	for user in "$WHO"
	do
		su -c '/opt/userful/bin/gconf.sh --restore_all' $user		
	done
	# restore gconf properties for default profile
	/opt/userful/bin/gconf.sh --restore_all

	# delete commnon config files for DM
	rm -f /opt/userful/bin/installvalue
	rm -f /etc/X11/umx-start
	rm -f /etc/X11/Xinput/*
	#rm -f /etc/X11/userful.*
	#rm -f /etc/X11/stations
	rm -f /etc/userful-MX.license
	
	# restore hotplug agents, or Udev rules
	rm -f /etc/udev/rules.d/99-userful-hotplug.rules

	
	rm -f /etc/hotplug/input.agent.multi
	mv -f /etc/hotplug/input.agent.multi-save /etc/hotplug/input.agent 2> /dev/null

	rm -f /etc/hotplug/usb.agent.multi
	mv -f /etc/hotplug/usb.agent.multi-save /etc/hotplug/usb.agent 2> /dev/null

	rm -f /etc/hotplug.d/input_device/50-input_device.hotplug.multi
	mv -f /etc/hotplug.d/input_device/50-input_device.hotplug.multi-save \
	  	  /etc/hotplug.d/input_device/50-input_device.hotplug 2> /dev/null	

	rm -f /etc/hotplug.d/usb/50-usb.hotplug.multi
	mv -f /etc/hotplug.d/usb/50-usb.hotplug.multi-save \
	  	  /etc/hotplug.d/usb/50-usb.hotplug 2> /dev/null
	

	rm -f /etc/X11/xdm/xdm-config
	rm -f /etc/X11/xdm/xdm-config.multi
	mv -f /etc/X11/xdm/xdm-config.multi-save /etc/X11/xdm/xdm-config 2> /dev/null
	rm -f /etc/X11/xdm/Xservers
	rm -f /etc/X11/xdm/Xservers.multi
	mv -f /etc/X11/xdm/Xservers.multi-save /etc/X11/xdm/Xservers 2> /dev/null

	# restore PAM configuration of Display Manager
	if [ -e /etc/pam.d/gdm ]; then
	    mv -f /etc/pam.d/gdm.multi-save /etc/pam.d/gdm 2> /dev/null
	fi
	if [ -e /etc/pam.d/xdm ]; then
	    mv -f /etc/pam.d/xdm.multi-save /etc/pam.d/xdm 2> /dev/null
	fi
	if [ -e /etc/pam.d/kde ]; then
	    mv -f /etc/pam.d/kde.multi-save /etc/pam.d/kde 2> /dev/null
	fi
	if [ -e /etc/pam.d/kdm ]; then
	    mv -f /etc/pam.d/kdm.multi-save /etc/pam.d/kdm 2> /dev/null
	fi

	# Remove any left over driver
	#if [[ -d /opt/MX11R7/lib/xorg/modules/drivers ]]; then
	#	rm -rf /opt/MX11R7/lib/xorg/modules/drivers
	#fi
	#if [[ -d /opt/MX11R6/lib/modules/drivers ]]; then
	#	rm -rf /opt/MX11R6/lib/modules/drivers
	#fi

	#
	# Distro specific uninstall procedure.
    #  - use distro.conf if it exists.
	if [ -e /tmp/distro.conf ]; then
        source /tmp/distro.conf

        ## 
        ## Restore umx-init and umx-start based on the "STARTUP" type.
        ## STARTUP is either inittab or upstart.
        ## inittab: SLED 11
        ## upstart: Ubuntu 9.10
        if [ "$STARTUP" == "inittab" ]; then

            ## Restore umx-init
            if [ -n "$UMXINIT" -a -e $UMXINIT ]; then

                # Get the file name
                UMXINIT_NAME=`basename $UMXINIT`

                ## Unregister umx-init script
                if which chkconfig > /dev/null ; then
                    chkconfig --del $UMXINIT_NAME > /dev/null
                fi
                rm -f $UMXINIT
            fi

            ## Restore umx-start
            if [ -n "$RUNLEVEL" ]; then
                if [ -e /etc/inittab.multi-save ]; then
		            mv -f /etc/inittab.multi-save /etc/inittab 2> /dev/null
                fi
            fi

        elif [ "$STARTUP" == "upstart" ]; then
            ##
            ## Remove UPSTART jobs
            if [ -n "$UMXSTART" -a -e $UMXSTART ]; then
                rm -f $UMXSTART
            fi
            if [ -n "$UMXINIT" -a -e $UMXINIT ]; then
                rm -f $UMXINIT
            fi
        fi

        ##
        ## restore Configuration of Display Manager - DMCONFIG
        if [ -n "$DMCONFIG" ]; then
            if [ -e $DMCONFIG.multi-save ]; then
                mv -f $DMCONFIG.multi-save  $DMCONFIG
            elif [ -e $DMCONFIG.multi-remove ]; then
                rm -f $DMCONFIG  $DMCONFIG.multi-remove
            fi
        fi

        ##
        ## Restore DisplayManager if requried. - DMINIT
        if [ -n "$DMSTART" -a "$DMSTART" == "disable" ]; then
            # Display Manager init script was disabled by UMx

            if which chkconfig > /dev/null ; then
                chkconfig --add $DMINIT > /dev/null
            elif which update-rc.d > /dev/null ; then
                update-rc.d $DMINIT defaults $DMSEQUENCE > /dev/null
            fi
        fi

	elif [ "$OSNAME" == "mandrake" ]; then
		sed -i -e "/^x:.*/d" /etc/inittab
		mv -f /etc/init.d/dm.multi-save /etc/init.d/dm 2> /dev/null
		mv -f /usr/share/config/kdm/kdmrc.multi-save /usr/share/config/kdm/kdmrc 2> /dev/null

	elif [ "$OSNAME" == "suse" -o "$OSNAME" == "novell" -o "$OSNAME" == "jds" ]; then

		sed -i -e "/^x:.*/d" /etc/inittab
		mv -f /etc/init.d/xdm.multi-save /etc/init.d/xdm 2> /dev/null
		rm -f /etc/opt/gnome/gdm/gdm.conf.multi
		mv -f /etc/opt/gnome/gdm/gdm.conf.multi-save /etc/opt/gnome/gdm/gdm.conf 2> /dev/null
		mv -f /etc/opt/gnome/gdm/custom.conf.multi-save /etc/opt/gnome/gdm/custom.conf 2> /dev/null
		rm -f /etc/X11/gdm/gdm.conf.multi
		mv -f /etc/X11/gdm/gdm.conf.multi-save /etc/X11/gdm/gdm.conf 2> /dev/null

		mv -f /etc/gdm/custom.conf.multi-save /etc/gdm/custom.conf 2> /dev/null

		rm -f /etc/opt/kde3/share/config/kdm/kdmrc.multi
		mv -f /etc/opt/kde3/share/config/kdm/kdmrc.multi-save /etc/opt/kde3/share/config/kdm/kdmrc 2> /dev/null
		cp -f /etc/X11/xorg.conf /etc/X11/XF86Config-4 2> /dev/null

        #TODO:
        #   - Need to restore xdm soft links in /etc/init.d/rc5.d/ 
		if [ "$OSNAME" == "novell" -a "$OSVER" == "10" ] ||
           [ "$OSNAME" == "suse" -a "$OSVER" == "10.2" ] ; then
			if [ ! -e /etc/init.d/rc5.d/S10xdm ]; then
				ln -s /etc/init.d/xdm /etc/init.d/rc5.d/S10xdm
			fi
			if [ ! -e /etc/init.d/rc5.d/K12xdm ]; then
				ln -s /etc/init.d/xdm /etc/init.d/rc5.d/K12xdm
			fi
		fi
        if [[ "$OSNAME" == "suse" ]]; then # openSuSE 10.3,11.0
            if [ "$OSVER" == "10.3" -o "$OSVER" == "11.0" ] ; then
                chkconfig -a xdm 5
                chkconfig -a earlyxdm 5
            fi
        fi

	elif [ "$OSNAME" == "fedora" -o "$OSNAME" == "redhat" -o "$OSNAME" == "centos" -o "$OSNAME" == "freedows" ]; then
		#sed -i -e "s/^x:.*/x:5:respawn:\/etc\/X11\/prefdm -nodaemon/g" /etc/inittab
		mv -f /etc/inittab.multi-save /etc/inittab 2> /dev/null
		rm -f /etc/X11/gdm/gdm.conf.multi 2> /dev/null
		mv -f /etc/X11/gdm/gdm.conf.multi-save /etc/X11/gdm/gdm.conf 2> /dev/null
		rm -f /etc/X11/xdm/kdmrc.multi
		mv -f /etc/X11/xdm/kdmrc.multi-save /etc/X11/xdm/kdmrc 2> /dev/null
		mv -f /etc/gdm/custom.conf.multi-save /etc/gdm/custom.conf 2> /dev/null
	elif [ "$OSNAME" == "xandros" -o "$OSNAME" == "mepis" ]; then
		sed -i -e "/^x:.*/d" /etc/inittab
		mv -f /etc/init.d/kdm.multi-save /etc/init.d/kdm 2> /dev/null
		rm -f /etc/kde3/kdm/kdmrc.multi
		mv -f /etc/kde3/kdm/kdmrc.multi-save /etc/kde3/kdm/kdmrc 2> /dev/null
		rm -f /etc/kde3/kdm/Xservers.multi
		mv -f /etc/kde3/kdm/Xservers.multi-save /etc/kde3/kdm/Xservers 2> /dev/null
	elif [ "$OSNAME" == "ubuntu" -o "$OSNAME" == "linspire" -o "$OSNAME" == "debian" -o \
		   "$OSNAME" == "freespire" -o "$OSNAME" == "morphix" ]; then

		if [ -e /etc/inittab ]; then
			sed -i -e "/^x:.*/d" /etc/inittab
        fi

		if [ -d /etc/event.d ]; then
			rm -f /etc/event.d/dm-start
			rm -f /etc/event.d/umx-init
		fi

        # resotre GDM related files for Ubuntu or Ubuntu based system
        if [ -e /etc/init.d/gdm.multi-save ]; then
		    mv -f /etc/init.d/gdm.multi-save /etc/init.d/gdm 2> /dev/null

            if [ "$OSNAME" == "ubuntu" -a "$OSVER" == "9.04" ]; then
                update-rc.d gdm start 30 2 .
            fi
        fi
		rm -f /etc/gdm/gdm.conf.multi
		mv -f /etc/gdm/gdm.conf.multi-save /etc/gdm/gdm.conf 2> /dev/null
		mv -f /etc/gdm/gdm.conf-custom.multi-save /etc/gdm/gdm.conf-custom 2> /dev/null
		mv -f /etc/gdm/gdm-cdd.conf.multi-save /etc/gdm/gdm-cdd.conf 2> /dev/null

        # resotre KDM related files for Kubuntu or Kubuntu based system
        if [ -e /etc/init.d/kdm.multi-save ]; then
		    mv -f /etc/init.d/kdm.multi-save /etc/init.d/kdm 2> /dev/null

            if [ "$OSNAME" == "ubuntu" -a "$OSVER" == "9.04" ]; then
                update-rc.d kdm start 30 2 .
            fi
        fi
		rm -f /etc/kde3/kdm/kdmrc.multi
		mv -f /etc/kde3/kdm/kdmrc.multi-save /etc/kde3/kdm/kdmrc

	fi

    # delete dm.conf
    if [ -e /etc/userful/dm.conf ]; then
        rm -f /etc/userful/dm.conf
    fi

    ##
    ## TODO
    ##  - GDM(2.27.4) on Ubuntu 9.10 can't be started without local display.
    ##  - It will be reimplemented again in the future gdm version with ConsoleKit support
    ##  - "--no-console" is used in SLED 11 to start GDM without local display.
    ##  - Fix for Ubuntu 9.10 for now:
    ##    : rename /usr/bin/Xorg to /usr/bin/Xorg.umx-save
    ##    : create symbolic link /usr/bin/Xorg to /opt/userful/bin/login-server

    # Restore /usr/bin/Xorg if required (if it is a symbolic link)
    if [ -h /usr/bin/Xorg ]; then
        rm -f /usr/bin/Xorg
        mv -f /usr/bin/Xorg.umx-save /usr/bin/Xorg
    fi
}

#--------------------------------
# Start here
#--------------------------------
/opt/userful/bin/dm-check-os.sh
source /etc/userful/os-info

## Create /tmp/distro.conf 
if [ -e /etc/userful/umx-distro.conf -a -e /opt/userful/bin/umx-distro-conf.py ]; then
    rm -f /tmp/distro.conf
    /opt/userful/bin/umx-distro-conf.py --osname $OSNAME --osver $OSVER
fi

# Pre-Uninstall
if [ "$1" = "preun" ]; then
	deleteall
	exit 0
fi

/opt/userful/bin/dm-supported-os.sh
RET=$?

##
## Skip eula check during the package installation.
## Show and check EULA in install script
touch /etc/userful-MX.license
echo "0" > /opt/userful/bin/installvalue

#if [ "$1" = "install" ] || [ "$1" = "upgrade" ]; then
#	# run by rpm command
#	licensecheck --auto
#else
#	# Manual setup
#	licensecheck --manual
#fi

if [ ! -e /etc/X11/Xinput ]; then
	mkdir -p /etc/X11/Xinput
fi

if [ $RET -ne 0 ]; then
	# Not supported distro
	# don't configure Display Manager and Hotplug agents
	exit 1
fi

# configure Display Manager and hotplug agents
if [ "$1" = "install" ]; then
	pkginstall
	configurefiles
elif [ "$1" = "upgrade" ]; then
	pkgupgrade
	configurefiles
fi

# Generate sys-id during installation.
if [ -x /opt/userful/bin/dm-gen-info ]; then
    /opt/userful/bin/dm-gen-info -sysid
fi

#
# Disable SELINUX
#
if [ -f /etc/selinux/config ]; then
	sed -i -e "s/^SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config
fi
