#!/bin/sh
 
# Copyright 2017  Ali Ahmadi, <idnux09@gmail.com>
# All rights reserved.
#
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOW4EVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# modified for Slint by Didier Spaier <didier~at~slint~dot~fr>

# If we get here and don't have a lightdm user/group, add one.
# Otherwise a few directories in the package will have wrong permissions.

# If using fakeroot, create the group and user before running this
# script else it will fail.

if ! grep -q "^lightdm:" /etc/passwd ; then
  /usr/sbin/groupadd -fg 620 lightdm
  /usr/sbin/useradd -c "Light Display Manager" -d /var/lib/lightdm -u 620 -g lightdm -s /sbin/nologin lightdm
fi


PRGNAM=lightdm
VERSION=1.24.0
BUILD=4
TAG=slint
NUMJOBS=${NUMJOBS:--j7}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
WRK=$CWD
PKG=$WRK/package-$PRGNAM
OUTPUT=$CWD

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -pipe -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -pipe -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -m64 -pipe -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $WRK $PKG $OUTPUT
cd $WRK
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.?z*
cd $PRGNAM-$VERSION

cat $CWD/lightdm-default-config.patch | patch -p1 || exit 1

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libexecdir=/usr/lib/lightdm \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --datarootdir=/usr/share \
  --localstatedir=/var \
  --sbindir=/usr/bin \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --with-greeter-user=lightdm \
  --with-greeter-session=lightdm-gtk-greeter \
  --disable-liblightdm-qt \
  --disable-liblightdm-qt5 \
  --disable-static \
  --disable-test \
  --build=$ARCH-slackware-linux

make $NUMJOBS
make install DESTDIR=$PKG

cat $CWD/Xsession > $PKG/etc/lightdm/Xsession
chmod +x $PKG/etc/lightdm/Xsession
rm -rf $PKG/etc/init

mkdir -p -m=755 $PKG/var/cache/lightdm
mkdir -p -m=770 $PKG/var/lib/lightdm{,-data}
mkdir -p -m=711 $PKG/var/log/lightdm
chmod +t $PKG/var/{cache/lightdm,lib/lightdm{,-data}}
chown lightdm:lightdm -R $PKG/var/lib/lightdm{,-data}
chgrp lightdm $PKG/var/log/lightdm

# apparmor package. Bug #494426
 if [[ -d $PKG/etc/apparmor.d ]]; then
   rm -r "$PKG/etc/apparmor.d" || die \
			"Failed to remove apparmor profiles"
 fi


# PAM
cat $CWD/lightdm.pam > $PKG/etc/pam.d/lightdm
cat $CWD/lightdm-autologin.pam > $PKG/etc/pam.d/lightdm-autologin

# PolicyKit
mkdir -p -m750 $PKG/usr/share/polkit-1/rules.d
chgrp polkitd $PKG/usr/share/polkit-1/rules.d
cat $CWD/lightdm.rules > $PKG/usr/share/polkit-1/rules.d/lightdm.rules

cp  $CWD/{lightdm.conf,Xsession} $PKG/etc/lightdm/
chmod 644 $PKG/etc/lightdm/lightdm.conf
chmod 755 $PKG/etc/lightdm/Xsession

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

find $PKG/usr/man -type f -exec gzip -9 {} \;
for i in $( find $PKG/usr/man -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
mv $PKG/etc/lightdm/lightdm.conf $PKG/etc/lightdm/lightdm.conf.new
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a COPYING* NEWS $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild


mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -p -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}
