#!/bin/sh
### BEGIN INIT INFO
# Provides:          fly-dm
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Should-Start:      console-screen kbd acpid dbus
# Should-Stop:       console-screen kbd
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: X display manager for Fly
# Description:       Fly-dm manages a collection of X servers, which may be on the local host or remote machines.
### END INIT INFO
# /etc/init.d/fly-dm: start or stop the X display manager
# Script originally stolen from the xdm package
#
# description: Fly Display Manager
#

tpm_awk_script='
!/pam_tpm\.so/ {
	next
}

/^($|[[:space:]]*#)/ {
	next
}

/pam_tpm\.so/ {
	if (match($0,/\s*tpm\s*=\s*\S+/)) {
		print gensub(/[^#]*\s*tpm\s*=\s*(\S+)\s*.*/,"\\1","")
		exit # Find the first of TPM plugins
	}
}
'
set -e

#alex: try to set locale to get localized PAM messages
if [ -f /etc/default/locale ]; then
. /etc/default/locale
if [ -n $LANG ]; then
export LANG
fi
fi

# Import the LSB init functions
. /lib/lsb/init-functions

# To start fly-dm even if it is not the default display manager, change
# HEED_DEFAULT_DISPLAY_MANAGER to "false."
HEED_DEFAULT_DISPLAY_MANAGER=true
DEFAULT_DISPLAY_MANAGER_FILE=/etc/X11/default-display-manager

PATH=/usr/lib/parsec/bin:/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/usr/bin/fly-dm
PIDFILE=/var/run/fly-dm.pid
UPGRADEFILE=/var/run/fly-dm.upgrade

# parameters to support fly-dm customization
KDMRC=/etc/X11/fly-dm/fly-dmrc
BACKGROUNDRC=/etc/X11//fly-dm/backgroundrc

# If we upgraded the daemon, we can't use the --exec argument to
# start-stop-daemon since the inode will have changed.  The risk here is that
# in a situation where the daemon died, its pidfile was not cleaned up, and
# some other process is now running under that pid, start-stop-daemon will send
# signals to an innocent process.  However, this seems like a corner case.
# C'est la vie!
if [ -e $UPGRADEFILE ]; then
  SSD_ARGS="--pidfile $PIDFILE --startas $DAEMON"
else
  SSD_ARGS="--pidfile $PIDFILE --exec $DAEMON"
fi


stillrunning () {
  if expr "$(cat /proc/$DAEMONPID/cmdline 2> /dev/null)" : "$DAEMON" > /dev/null 2>&1; then
    true
  else
    # if the daemon does not remove its own pidfile, we will
    rm -f $PIDFILE $UPGRADEFILE
    false
  fi;
}

#alex:
if [ ! -s $KDMRC ]; then
    echo "Файл  ${KDMRC} не существует ли нулевого размера. Будет создан сейчас."
    /usr/bin/genfly-dmconf
fi

case "$1" in
  start)
      tpm_plugin=`awk "$tpm_awk_script" < /etc/pam.d/common-auth`
      if [ ! -z $tpm_plugin ]; then
        export TPM_PLUGIN=$tpm_plugin
      fi
#alex: hack - fly-dm is always default!
#    if [ -e $DEFAULT_DISPLAY_MANAGER_FILE ] &&
#       [ "$HEED_DEFAULT_DISPLAY_MANAGER" = "true" ] &&
#       [ "$(cat $DEFAULT_DISPLAY_MANAGER_FILE)" != "$DAEMON" ]; then
#      echo "Not starting Fly Display Manager (fly-dm); it is not the default display manager."
#    else
      echo -n "Starting Fly Display Manager: fly-dm"
      start-stop-daemon --start --quiet $SSD_ARGS -- $ARG || echo -n " already running"
      echo "."
#    fi
  ;;

  restart)
    /etc/init.d/fly-dm stop
    if [ -f $PIDFILE ]; then
      if stillrunning; then
        exit 1
      fi
    fi
    /etc/init.d/fly-dm start
  ;;

  reload)
    echo -n "Reloading Fly Display Manager configuration..."
    if start-stop-daemon --stop --signal 1 --quiet $SSD_ARGS; then
      echo "done."
    else
      echo "fly-dm not running."
    fi
  ;;

  force-reload)
    /etc/init.d/fly-dm reload
  ;;

  stop)
    echo -n "Stopping Fly Display Manager: fly-dm"
    if [ ! -f $PIDFILE ]; then
      echo " not running ($PIDFILE not found)."
      exit 0
    else
      DAEMONPID=$(cat $PIDFILE | tr -d '[:blank:]')
      KILLCOUNT=1
      if [ ! -e $UPGRADEFILE ]; then
        if start-stop-daemon --stop --quiet $SSD_ARGS; then
          # give fly-dm's signal handler a second to catch its breath
          sleep 1
        else
          echo -n " not running"
        fi
      fi
      while [ $KILLCOUNT -le 5 ]; do
        if stillrunning; then
          kill $DAEMONPID
        else
          break
        fi
        sleep 1
        KILLCOUNT=$(( $KILLCOUNT + 1 ))
      done
      if stillrunning; then
        echo -n " not responding to TERM signal (pid $DAEMONPID)"
      else
        rm -f $UPGRADEFILE
      fi
    fi
    echo "."
  ;;
  status)
    status_of_proc -p "$PIDFILE" "$DAEMON" fly-dm && exit 0 || exit $?
  ;;

  *)
    echo "Usage: /etc/init.d/fly-dm {start|stop|restart|reload|force-reload|status}"
    exit 1
    ;;
esac

exit 0
