#!/bin/sh
# $FreeBSD: branches/RELENG_9_2_0/x11/gdm/files/gdm.in 314632 2013-03-19 10:40:22Z kwm $
#    $MCom: ports/x11/gdm/files/gdm.in,v 1.50 2012/01/15 18:18:36 kwm Exp $

# PROVIDE: gdm
# REQUIRE: LOGIN cleanvar moused syscons dbus
#
# Add the following to /etc/rc.conf to start GDM at boot time:
#
# gdm_enable="YES"
#

. /etc/rc.subr
. /usr/local/etc/gnome.subr

gdm_enable=${gdm_enable-${gnome_enable}}
gdm_preserve_base_pam_conf=${gdm_preserve_base_pam_conf-NO}
gdm_lang=${gdm_lang-${LANG}}

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

name="gdm"
rcvar=gdm_enable
command="/usr/local/sbin/${name}"
pidfile="/var/run/${name}.pid"
procname="/usr/local/sbin/gdm-binary"
start_cmd="gdm_start"

gdm_start()
{
    if ! checkyesno gdm_enable ; then
	return 0
    fi
    echo "Starting ${name}."

    # make sure there is no pam configuration for gdm service in base system
    if ! checkyesno gdm_preserve_base_pam_conf && [ -f /etc/pam.d/gdm ]; then
	cp -p /etc/pam.d/gdm /etc/pam.d/gdm_disabled
    	rm -f /etc/pam.d/gdm
    fi

    ( iter=0
    while ! ps -axoargs | grep "^/usr/libexec/getty " | grep -qv grep >/dev/null 2>&1; do
	if [ ${iter} -eq 60 ]; then
	    break
	fi
	sleep 1
	iter=$(expr ${iter} + 1)
    done
    iter=0
    while ! /usr/local/bin/lshal >/dev/null 2>&1 ; do
	if [ ${iter} -eq 60 ]; then
	    break
	fi
	sleep 1
	iter=$(expr ${iter} + 1)
    done
    LANG=${gdm_lang} ${command} ${gdm_flags} ) &
}

load_rc_config ${name}
run_rc_command "$1"
