#!/bin/bash

CWD=$(pwd)

NAME="lightdm"
VERSION=${VERSION:-$(echo $NAME-*.tar.?z* | cut -d - -f2 | rev | cut -d . -f3- | rev)}
BUILD=${BUILD:-1}
PACKAGER=${PACKAGER:-mara}

NUMJOBS=${NUMJOBS:-" -j$(grep -c ^processor /proc/cpuinfo 2>/dev/null) "}


# Automatically determine the architecture we're building on:
MARCH=$( uname -m )
if [ -z "$ARCH" ]; then
  case "$MARCH" in
    i?86)    export ARCH=i586 ;;
    armv7hl) export ARCH=$MARCH ;;
    arm*)    export ARCH=arm ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
    *)       export ARCH=$MARCH ;;
  esac
fi

if [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O3 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O3 -fPIC"
  LIBDIRSUFFIX="64"
elif [ "$ARCH" = "armv7hl" ]; then
  SLKCFLAGS="-O3 -march=armv7-a -mfpu=vfpv3-d16"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "arm" ]; then
  SLKCFLAGS="-O3 -march=armv7-a -mtune=cortex-a7 -mfpu=neon-vfpv4 -mfloat-abi=hard"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-O3"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

case "$ARCH" in
    arm*) TARGET=$ARCH-slackware-linux-gnueabihf ;;
    *)    TARGET=$ARCH-slackware-linux ;;
esac

# Establishment tmp directory for
TMP=${TMP:-/tmp}
PKG=$TMP/package-$NAME
rm -rf $PKG
mkdir -p $PKG

# Delete a directory previous build / create new and change rights
cd $TMP
rm -rf $NAME-$VERSION
tar xvf $CWD/$NAME-$VERSION.tar.?z* || exit 1
cd $NAME-$VERSION || exit 1
chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

[[ ! -x configure ]] && NOCONFIGURE=1 ./autogen.sh

# Then change the Linux-PAM configuration files so that elogind is used: 
sed -i s/systemd/elogind/ data/pam/*

# Configuration
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --mandir=/usr/man \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --sbindir=/usr/bin \
  --libexecdir=/usr/libexec/$NAME \
  --enable-liblightdm-qt5=no \
  --with-greeter-user=lightdm \
  --with-greeter-session=lightdm-gtk-greeter \
  --disable-static \
  --disable-gtk-doc \
  --disable-tests \
  --docdir=/usr/doc/$NAME-$VERSION \
  --build=$TARGET

# Compilation
make $NUMJOBS || exit 1
make install DESTDIR=$PKG

# Don't ship .la files:
rm -f $PKG/{,usr/}lib${LIBDIRSUFFIX}/*.la

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

cp tests/src/lightdm-session $PKG/usr/bin
sed -i '1 s/sh/bash --login/' $PKG/usr/bin/lightdm-session

# install Xsession wrapper to use with lightdm
install -dm 755 -g root $PKG/etc/lightdm/lightdm.conf.d
install -m 644 $CWD/50-slarm64-defaults.conf $PKG/etc/lightdm/lightdm.conf.d/50-slarm64-defaults.conf.new
install -m 644 $CWD/50-directories.conf $PKG/etc/lightdm/lightdm.conf.d/50-directories.conf.new
mv $PKG/etc/lightdm/keys.conf $PKG/etc/lightdm/keys.conf.new
mv $PKG/etc/lightdm/lightdm.conf $PKG/etc/lightdm/lightdm.conf.new
mv $PKG/etc/lightdm/users.conf $PKG/etc/lightdm/users.conf.new
install -dm 755 $PKG/var/cache/lightdm
install -dm 755 $PKG/var/lib/lightdm{,-data}
install -dm 770 $PKG/var/log/lightdm
chmod +t $PKG/var/{cache/lightdm,lib/lightdm{,-data}}
chown 620:620 -R $PKG/var/cache/lightdm
chown 620:620 -R $PKG/var/lib/lightdm{,-data}
chown 620:620 -R $PKG/var/log/lightdm

# adjust users.conf to accept userid<500
sed -i 's/500/100/g' $PKG/etc/lightdm/users.conf.new

# install PolKit rules
install -dm 755 -g root $PKG/usr/share/polkit-1/rules.d
install -m 644 $CWD/lightdm.rules $PKG/usr/share/polkit-1/rules.d/lightdm.rules.new

# remove junk
#rm -fr $PKG/usr/share/{locale,gtk-doc,help}
#rm -fr $PKG/etc/{init,apparmor.d,pam.d}
rm -fr $PKG/usr/share/{gtk-doc,help}
rm -fr $PKG/etc/{init,apparmor.d}

# install PAM configurations
#install -d $PKG/etc/pam.d
#install -m644 $CWD/lightdm.pam $PKG/etc/pam.d/lightdm.new
#install -m644 $CWD/lightdm-autologin.pam $PKG/etc/pam.d/lightdm-autologin.new
mv $PKG/etc/pam.d/lightdm $PKG/etc/pam.d/lightdm.new
mv $PKG/etc/pam.d/lightdm-autologin $PKG/etc/pam.d/lightdm-autologin.new
mv $PKG/etc/pam.d/lightdm-greeter $PKG/etc/pam.d/lightdm-greeter.new

mv $PKG/etc/dbus-1/system.d/org.freedesktop.DisplayManager.conf $PKG/etc/dbus-1/system.d/org.freedesktop.DisplayManager.conf.new

# Compress manual pages:
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

# Copy docs
mkdir -vpm755 $PKG/usr/doc/$NAME-$VERSION
cp -fav \
  LICENSE README \
  $PKG/usr/doc/$NAME-$VERSION/
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild

# Description and installation script
mkdir -p $PKG/install
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc

# Package creation
cd $PKG
makepkg -l n -c n $TMP/${NAME}-${VERSION}-${ARCH}-${BUILD}${PACKAGER}.txz
