#!/bin/sh -e
### 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 krb5-kdc
# 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
#

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

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

# set the locale
if [ -r /etc/default/locale ]; then
  . /etc/default/locale
  export LANG LANGUAGE
fi

# 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

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

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

# 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


still_running () {
  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;
}

case "$1" in
  start)
      setup_config
      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
#      log_action_msg "Not starting Fly Display Manager (fly-dm); it is not the default display manager."
#    else
      log_daemon_msg "Starting Fly Display Manager: fly-dm"
      if start-stop-daemon --start --quiet $SSD_ARGS -- $ARG ; then
        log_end_msg 0
      else
        log_action_end_msg 1 "already running"
      fi
#    fi
  ;;

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

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

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

  stop)
    log_action_begin_msg "Stopping Fly Display Manager: fly-dm"
    if [ ! -f $PIDFILE ]; then
      log_action_end_msg 0 " 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
          log_action_cont_msg " not running"
        fi
      fi
      while [ $KILLCOUNT -le 5 ]; do
        if still_running; then
          kill $DAEMONPID
        else
          break
        fi
        sleep 1
        KILLCOUNT=$(( $KILLCOUNT + 1 ))
      done
      if still_running; then
        log_action_cont_msg " not responding to TERM signal (pid $DAEMONPID)"
      else
        rm -f $UPGRADEFILE
      fi
    fi
    log_action_end_msg 0
  ;;
  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
