#!/bin/bash
# Packager : Jean-Philippe Guillemin <jp.guillemin@free.fr>
# sources location : http://sourceforge.net/projects/lxde/files/lxdm/lxdm%200.1.0/lxdm-0.1.0.tar.gz/download


NAME='lxdm'
VERSION='0.5.3'
BUILD='6jp' 

arch=${arch:-x86_64}

WD=$(pwd)
srcpkg="$WD/$NAME-$VERSION.tar.xz"
src="$WD/$NAME-$VERSION"

package="$NAME-$VERSION-$arch-$BUILD"

PKG="/tmp/$package"

cd $WD
rm -rf $src
tar -xvf $srcpkg
rm -f $PKG.t*z
rm -rf $PKG
mkdir -p $PKG

(
cd $src
# cat $WD/glib2-2.32.0.patch | patch -p1 --verbose
#cat $WD/greeter-session.patch | patch -p1 --verbose
#cat $WD/gtk-3.4.3.patch | patch -p1 --verbose

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 {} \;

case "$arch" in
  i486)      SLKCFLAGS="-O2 -march=i486 -mtune=i686"
             SLKLDFLAGS=""
             LIBDIRSUFFIX=""
             ;;
  x86_64)    SLKCFLAGS="-O2 -fPIC"
             SLKLDFLAGS="-L/usr/lib64"
             LIBDIRSUFFIX="64"
             ;;
esac

export CFLAGS="$SLKCFLAGS"
export CXXFLAGS="$SLKCFLAGS"
export LDFLAGS="$SLKLDFLAGS"

./configure --prefix=/usr \
--libdir=/usr/lib${LIBDIRSUFFIX} \
--sysconfdir=/etc \
--libexecdir=/usr/libexec \
--localstatedir=/var \
--mandir=/usr/man \
--enable-gtk3 \
--with-pam=no
make -j6

make install DESTDIR=$PKG

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a AUTHORS COPYING INSTALL README TODO $PKG/usr/doc/$NAME-$VERSION/
)

# Striptease
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  for mandir in $(find . -name man -type d) ; do
  if [ -d $mandir ]; then
    ( cd $mandir
      find . -type f -name "*.?" -exec gzip -9 {} \;
    )
  fi
  done
)

# Config
mkdir -p $PKG/usr/share/applications
cat $WD/lxdm-config.desktop > $PKG/usr/share/applications/lxdm-config.desktop
cat $WD/lxdm.conf > $PKG/etc/lxdm/lxdm.conf
mv $PKG/etc/lxdm/lxdm.conf $PKG/etc/lxdm/lxdm.conf.new
chmod 644 $PKG/etc/lxdm/lxdm.conf.new
cat $WD/Init > $PKG/etc/lxdm/PreLogin
cp $WD/login.png $PKG/usr/share/lxdm/themes/Industrial/login.png
cp $WD/greeter*.ui $PKG/usr/share/lxdm/themes/Industrial/
rm $PKG/usr/sbin/lxdm
cat $WD/lxdm > $PKG/usr/sbin/lxdm
chmod 0755 $PKG/usr/sbin/lxdm
( cd $PKG/usr/sbin ; ln -s lxdm gdm )
cp $WD/Xsession $PKG/etc/lxdm/Xsession
chmod 755 $PKG/etc/lxdm/Xsession

# Nice icon
mkdir -p $PKG/usr/share/icons/hicolor/scalable/apps/
cp $WD/lxde.svg $PKG/usr/share/icons/hicolor/scalable/apps/

# Policykit
mkdir -p $PKG/usr/share/polkit-1/actions
cat << EOS > $PKG/usr/share/polkit-1/actions/org.freedesktop.policykit.pkexec.$NAME.policy
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
 "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
<policyconfig>
  
  <action id="org.freedesktop.policykit.pkexec.run-$NAME">
  <description>Run $NAME</description>
  <message>Authentication is required to run LXDM Config as Root</message>
  <defaults>
  <allow_any>no</allow_any>
  <allow_inactive>no</allow_inactive>
  <allow_active>yes</allow_active>
  </defaults>
  <annotate key="org.freedesktop.policykit.exec.path">/usr/bin/lxdm-config</annotate>
  <annotate key="org.freedesktop.policykit.exec.allow_gui">TRUE</annotate>
  </action>
  
</policyconfig>
EOS

# Launcher
mkdir -p $PKG/usr/sbin

cat << "EOF" > $PKG/usr/sbin/lxdm-config
#!/bin/bash
if [ $(which pkexec) ]; then
	pkexec /usr/bin/env USER=$USER /usr/bin/lxdm-config "$@"
else
	/usr/bin/lxdm-config "$@"
fi
EOF
chmod -R 755 $PKG/usr/sbin/lxdm-config

# Install dir
mkdir -p $PKG/install
chmod -R 755 $PKG/install
cat << "EOF" > $PKG/install/slack-desc
lxdm: Lxdm - a Login Manager for X11
lxdm:
lxdm: lxdm is a fast and sexy X11 login manager for use with   
lxdm: any desktop environment. 
lxdm:
lxdm:
lxdm:
lxdm:
lxdm: 
lxdm:
lxdm:
EOF

cat << "EOF" > $PKG/install/doinst.sh
config() {
  NEW="$1"
  OLD="`dirname $NEW`/`basename $NEW .new`"
  # If there's no config file by that name, mv it over:
  if [ ! -r $OLD ]; then
    mv $NEW $OLD
  elif [ "`cat $OLD | md5sum`" = "`cat $NEW | md5sum`" ]; then # toss the redundant copy
    rm $NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}
config etc/lxdm/lxdm.conf.new
EOF

mkdir -p $PKG/usr/src/$NAME-$VERSION
( cd $WD
cp -a $NAME.SlackBuild $PKG/usr/src/$NAME-$VERSION/
)

cd $PKG
makepkg -l y -c y $PKG.txz

rm -rf $PKG
rm -rf $src
