#!/bin/sh
#
# This version of the dtlogin.rc script can be used on the Solaris(TM) 
# operating system to initiate CDE tasks such as starting the dtlogin 
# process. 
#
#  Common Desktop Environment
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#  (c) Copyright 1993, 1994 Hewlett-Packard Company
#  (c) Copyright 1993, 1994 International Business Machines Corp.
#  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
#  (c) Copyright 1993, 1994 Novell, Inc.
#
#pragma ident	"@(#)dtlogin.rc.src	1.20	12/03/07 SMI"
#


mode=$1

usage_error() {
      echo "  $0 start             (start dtlogin process)"
      echo "  $0 stop              (stop dtlogin process)"
      echo "  $0 reset             (reset dtlogin process)"
      echo "  $0 update_printers   (update print actions)"
      echo " "
}

if [ ! -d /usr/bin ]
then                    # /usr not mounted
        exit 1
fi

set `/usr/bin/id`
if [ $1 != "uid=0(root)" ]; then
        echo "$0: must be run as root"
        exit 1
fi


# update_printers() 
#
# Add print actions to workstation's database for all printer's known
# to this workstation if action is not already present in the database.

update_printers() {
   if [ -x /usr/dt/bin/dtprintinfo ] ; then
            /usr/dt/bin/dtprintinfo -populate &
   fi
}

#
# Find login server pid from the process tree
#
login_server_pid()
{
  grep=/usr/bin/grep
  ps=/usr/bin/ps
  cut=/usr/bin/cut
  awk=/usr/bin/awk

# In following grep for "dtlogin" processes, explictly exclude any matches 
# on this shell file named "dtlogin.rc"

  $ps -z `zonename` -u 0 -l | $grep -v dtlogin. | $grep dtlogin | $cut -c1-24 | $awk '{print $4 " " $5}' |
  while read pid ppid; do
    parent_login_ps=`$ps -p $ppid | $grep dtlogin`
    if [ -z "$parent_login_ps" ]; then
      echo "$pid"
      break
    fi
  done
}



if [ `zonename` != "global" ] ; then
	parent_pid=`pgrep -u root -x zsched`
else
	parent_pid=1
fi

get_login_server_pid()
{
        if [ -x /usr/bin/pgrep ]
        then
                /usr/bin/pgrep -z `zonename` -u 0 -P 1 -x dtlogin
        else
                login_server_pid
        fi
}

kill_dtlogin()
{
        if [ "$1" != "" ]
        then
                SIGNAL=-$1
        fi

        if [ -x /usr/bin/pkill ]
        then
                /usr/bin/pkill $SIGNAL -u 0 -P $parent_pid -z `zonename` -x dtlogin
        else
#              get dtlogin pid

                dtlogin_pid=`login_server_pid`

#              kill dtlogin process

                if [ "$dtlogin_pid" != "" ] ; then
                        /usr/bin/kill $SIGNAL $dtlogin_pid
                fi
        fi
}

case "$mode" in

'start')
        svcadm enable -rt application/graphical-login/cde-login:default
        ;;


'stop')         
        svcadm disable -t application/graphical-login/cde-login:default
        ;;

'reset')        
        kill_dtlogin HUP
        ;;

'update_printers')      
        update_printers
        ;;

'get_server_pid')       
        get_login_server_pid
        ;;

*)
        usage_error
        exit 1
        ;;
esac

exit 0
