#!/bin/bash
# -----
# greetd: A more universal display manager for PCLinuxOS
# Author: Hunter Ellett
# Updated: Wed May 28 11:23:00 PM CDT 2025
# -----
# chkconfig: 5 33 09
# description: greetd is a minimal and flexible login manager daemon that makes no assumptions about what you want to launch.
# -----
#
### BEGIN INIT INFO
# Provides: greetd
# Should-Start: network-auth acpid lircmd xfs
# Required-Start: seatd
# Required-Stop: seatd
# Default-Start: 5
# Short-Description: Launches the greetd display manager
# Description: This startup script launches the greetd display manager.
### END INIT INFO

. /etc/rc.d/init.d/functions

# Allow the user to pick a greeter.
. /etc/sysconfig/greetd

prog=greetd
lockfile=/var/lock/subsys/$prog
pidfile=/var/run/$prog
ret=0

# If a greeter is for whatever reason not found then set it to agreety.
GREETER=${GREETER:-agreety}

case $1 in
    start)
    if pgrep -x plymouthd >/dev/null; then
     # shut down boot splash
     [ -x /bin/plymouth ] && /bin/plymouth quit
    fi
    gprintf "Starting $prog: "
    /usr/bin/$prog -c /etc/$prog/$GREETER.toml &
    success "%s started" "$prog"
    ret=$?
    echo
     if [ $ret = 0 ]; then
    	touch $lockfile
     fi
     # ensure we have the X11/xwayland socket available for greeters
     if [ -d /tmp/.X11-unix ]; then
        rm -rf /tmp/.X11-unix
        mkdir -p /tmp/.X11-unix
     	chown root:root /tmp/.X11-unix
     	chmod 1777 /tmp/.X11-unix
     else 
     	mkdir -p /tmp/.X11-unix
     	chown root:root /tmp/.X11-unix
     	chmod 1777 /tmp/.X11-unix
     fi

    ;;
    stop)
    gprintf "Stopping $prog: "
     if [ -e /media/* ]; then
     umount /media/*
     fi
    killproc $prog
    ret=$?
     if [ $ret = 0 ]; then
    	success "%s shutdown" "$prog"
     	rm -f $lockfile
     else
    	failure "%s shutdown" "$prog"
     fi
     echo
    ;;
    status)
    status $prog
    ;;
    reload)
    ;;
    restart)
    $0 stop
    # give a chance to stop gracefully
    sleep 5
    $0 start
    ret=$?
    ;;
    *)
    gprintf "Usage: %s\n" "$(basename $0) {start|stop|restart|status}"
    exit 0
    ;;
esac

exit $ret
