#!/usr/bin/env beesh

# BEE_VERSION lightdm-1.30.0-0

SRCURL[0]="https://github.com/CanonicalLtd/lightdm/releases/download/${PKGVERSION}/lightdm-${PKGVERSION}.tar.xz"

# PATCHURL+=()

# EXCLUDE+=()

# build_in_sourcedir

# mee_extract() {
#    bee_extract "${@}"
# }

# mee_patch() {
#    bee_patch "${@}"
# }

mee_configure() {
  DMUSER=gdm
  ITSTOOL=/bin/true                                 \
  bee_configure                                     \
    --prefix=/usr                                   \
    --libexecdir=/usr/lib/lightdm                   \
    --localstatedir=/var                            \
    --sbindir=/usr/bin                              \
    --sysconfdir=/etc                               \
    --docdir=/usr/share/doc/lightdm-${PKGVERSION}   \
    --disable-static                                \
    --disable-tests                                 \
    --enable-liblightdm-qt=no                       \
    --with-greeter-user=${DMUSER}                   \
    --with-greeter-session=slick-greeter
}

# mee_build() {
#    bee_build
# }

# mee_install() {
#    bee_install
# }
## by default this may be 'make install DESTDIR="${D}"'

mee_install_post() {

  cat > ${D}/usr/bin/lightdm-session <<- 'EOF'
	#! /bin/bash
	
	# First argument is full command, ´for backwards compatibility'.
	
	exec $1
EOF

  chmod 755 ${D}/usr/bin/lightdm-session
  rm -rf ${D}/etc/init
  install -v -dm755 -o gdm -g gdm ${D}/var/lib/lightdm
  install -v -dm755 -o gdm -g gdm ${D}/var/lib/lightdm-data
  install -v -dm755 -o gdm -g gdm ${D}/var/cache/lightdm
  install -v -dm770 -o gdm -g gdm ${D}/var/log/lightdm

  ###############################################
  # adjust settings in /etc/lightdm/lightdm.conf

  sed -i -e '/^#greeter-hide-users=false/ c greeter-hide-users=true' \
         -e '/^#sessions-directory/ c sessions-directory=/usr/share/xsessions' \
         -e '/^#greeter-session/ c greeter-session=slick-greeter' \
         ${D}/etc/lightdm/lightdm.conf

  sed -i -e '/^minimum-uid=/ c minimum-uid=100' \
         -e '/^hidden-users=/ c hidden-users=nobody nobodyV' \
         ${D}/etc/lightdm/users.conf


  ######################
  # create systemd unit

  mkdir -p ${D}/etc/systemd/system
  cat <<-UNIT > ${D}/etc/systemd/system/lightdm.service
	[Unit]
	Description=LightDM
	Wants=nvidia.service startup-tags.service
	After=systemd-user-sessions.service nvidia.service startup-tags.service
	ConditionPathExists=/node/tags/lightdm

	[Service]
	ExecStart=/usr/bin/lightdm
	Restart=always
	BusName=org.freedesktop.DisplayManager
	UMask=007

	[Install]
	WantedBy=graphical.target
	UNIT

  chmod 644 ${D}/etc/systemd/system/lightdm.service

}