#!/bin/bash

# Copyright (C) 2004-2007 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.

# This is the script that is called by init.d/mdm, it is the one that actually
# starts the "hard work".

# Do this first:
MDM_PREFIX=/
MDM_SCRIPTS=${MDM_PREFIX}/usr/sbin
MDM_INCLUDE=${MDM_SCRIPTS}/mdm-common
source $MDM_INCLUDE

MY_LOG=${MDM_LOGS}/mdm.log
MDM_LOCK_FILE=/var/lock/mdm.lock

CREATE_XORG_CONF=${MDM_SCRIPTS}/create-xorg-conf
START_SEAT=${MDM_SCRIPTS}/mdm-start-seat

function exec_start() {
	
    log --log-file-only "Multiseat Display Manager version $MDM_VERSION"
    log --log-file-only "Today is `date`."

    if [ -f "$MDM_LOCK_FILE" ]; then
        log --log-file-only "Error: mdm is already running"
	exit 1
    else
        touch $MDM_LOCK_FILE
    fi

    # /var/run is re-created at every reboot
    mkdir -p ${MDM_RUN}

    if ([ "$RECREATE_XORG_CONF" = 'yes' ] || [ ! -f "$MDM_XORG_CONF" ]); then
	$CREATE_XORG_CONF
    fi
	
    if [ "$RECONFIGURE_INPUT" = 'yes' ]; then
	# Removing links AND locks
	for i in `ls $MDM_DEVICES`; do
	    rm -f $MDM_DEVICES/$i
	done
    fi

    VIDEO_CARDS=`$DISCOVER_DEVICES driver | wc -l`

    log --log-file-only "$VIDEO_CARDS video cards detected"

    # Other scripts assume we do this:
    export DISPLAY=:0

    # erase links, locks, logs, close stuff, whatever needed
    display_manager_init

    # find out how many seats we actually have
    OUTPUTS_PER_CARD=$(grep "SCREEN_AMOUNT" $XRANDR_INFO_FILE | cut -d'=' -f2)
    SEATS=$(echo $OUTPUTS_PER_CARD | sed 's/\s/+/g' | bc)

    log --log-file-only "$SEATS seats detected"

    if [ "$USE_MONOSEAT" = 'yes' ]; then
	SEATS=1
    fi

    if [ "$SEATS" = "1" ]; then
	log --log-file-only "Only 1 video card: not using multiseat"
	display_manager_start_monoseat >> $MY_LOG 2>&1

    else
	display_manager_start_underneath_xserver >> $MY_LOG 2>&1

        for ((i=0; i < $VIDEO_CARDS; i++)); do
            export DISPLAY=:0.$i
	    xrandr_configure_layout
	done

	# Finding how many screens on each card
	SEATS_ON_CARD=(`grep "SCREEN_AMOUNT" $XRANDR_INFO_FILE | cut -d'=' -f2`)

	SEAT_DISPLAY=1
	for ((i = 0; i < $VIDEO_CARDS; i++)); do
	    export DISPLAY=:0.$i

	    # START_SEAT has 2 parameters:
	    # - the first is the video card number
	    # - the second is the seat display
	    for (( seat=0; seat < "${SEATS_ON_CARD[i]}"; seat++ )); do
		START_SEAT_LOG="${MDM_LOGS}/mdm-start-seat.${SEAT_DISPLAY}.log"
		rotate_log ${START_SEAT_LOG}

		log --log-file-only "Starting seat $((i+1)) ${SEAT_DISPLAY}"
		${START_SEAT} $((i+1)) $SEAT_DISPLAY &> $START_SEAT_LOG &
		PID=$!
		echo $PID > ${MDM_PIDS}/start-seat.${SEAT_DISPLAY}.pid
		((SEAT_DISPLAY++))
	    done
	done
    fi
}

function exec_stop() {

    display_manager_stop

    for i in `ls $MDM_PIDS/*.pid 2> /dev/null`; do
	kill `cat $i` 2> /dev/null
	rm -f $i
    done

    rm -f $MDM_LOCK_FILE
}

function exec_reconfigure() {
    RECREATE_XORG_CONF='yes'
    RECONFIGURE_INPUT='yes'
    exec_stop
    exec_start
}

#####

rotate_log $MY_LOG

# Check if we're root
if [ ! "`id -u`" -eq  "0" ]; then
    log "ERROR: not running as root user"
    exit 1
fi

case "$1" in
    start)
        exec_start
        ;;
    stop)
        exec_stop
        ;;
    restart)
        exec_stop
	exec_start
        ;;
    reconfigure)
        exec_reconfigure
        ;;
    *)
        ;;
esac

exit 0
