#!/bin/bash

NAME='systemtools'
VERSION='8.2'
BUILD='5'

arch=${arch:-x86_64}
CWD=$(pwd)

PKG="/tmp/$NAME-$VERSION-$arch-$BUILD"

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"

cd $CWD
rm -rf $PKG 
rm -f $PKG.tgz
tar -zxvf $NAME-$VERSION.tar.gz 




for tool in clockconfig keyboardconfig proxyconfig videoconfig hostconfig \
localeconfig serviceconfig kernelconfig userconfig ; do

(
	cd $CWD/$NAME-$VERSION/$tool
	make clean
	make install DESTDIR=$PKG
)

done

# Shell colors
cat $CWD/shell-colors > $PKG/etc/shell-colors

# We still use gksu, for now...
# sed -i 's|^Exec=pkexec\(.*\)$|Exec=gksu \1|g' $PKG/usr/share/applications/*.desktop

# POLKIT COOKING #########"""
# Policykit header
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>

EOS

# Policykit corpus
for tool in hostconfig serviceconfig userconfig ; do

(

cat << EOS >> $PKG/usr/share/polkit-1/actions/org.freedesktop.policykit.pkexec.$NAME.policy
  
  <action id="org.freedesktop.policykit.pkexec.run-$tool">
  <description>Run $tool</description>
  <message>Root authentication is required to run $tool</message>
  <icon_name>zenwalk</icon_name>
  <defaults>
  <allow_any>no</allow_any>
  <allow_inactive>no</allow_inactive>
  <allow_active>auth_admin_keep</allow_active>
  </defaults>
  <annotate key="org.freedesktop.policykit.exec.path">/usr/sbin/$tool</annotate>
  <annotate key="org.freedesktop.policykit.exec.allow_gui">TRUE</annotate>
  </action>

EOS

)

done

for tool in chpasswd ; do

(

cat << EOS >> $PKG/usr/share/polkit-1/actions/org.freedesktop.policykit.pkexec.$NAME.policy
  
  <action id="org.freedesktop.policykit.pkexec.run-$tool">
  <description>Run $tool</description>
  <message>User authentication required to run $tool</message>
  <icon_name>zenwalk</icon_name>
  <defaults>
  <allow_any>no</allow_any>
  <allow_inactive>no</allow_inactive>
  <allow_active>auth_self_keep</allow_active>
  </defaults>
  <annotate key="org.freedesktop.policykit.exec.path">/usr/sbin/$tool</annotate>
  <annotate key="org.freedesktop.policykit.exec.allow_gui">TRUE</annotate>
  </action>

EOS

)

done

for tool in keyboardconfig clockconfig ; do

(

cat << EOS >> $PKG/usr/share/polkit-1/actions/org.freedesktop.policykit.pkexec.$NAME.policy
  
  <action id="org.freedesktop.policykit.pkexec.run-$tool">
  <description>Run $tool</description>
  <message>Run $tool as root</message>
  <icon_name>zenwalk</icon_name>
  <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/sbin/$tool</annotate>
  <annotate key="org.freedesktop.policykit.exec.allow_gui">TRUE</annotate>
  </action>

EOS

)

done

# Policykit footer
mkdir -p $PKG/usr/share/polkit-1/actions
cat << EOS >> $PKG/usr/share/polkit-1/actions/org.freedesktop.policykit.pkexec.$NAME.policy

</policyconfig>

EOS

read-edit #############
cd $CWD
rm -rf read-edid-2.0.0
tar -zxvf read-edid-2.0.0.tar.gz
(
cd read-edid-2.0.0

./configure --prefix=/usr --mandir=/usr/man --libdir=/usr/lib${LIBDIRSUFFIX}
make
make install DESTDIR=$PKG
mkdir -p $PKG/usr/doc/read-edid-1.4.1
cp -a \
  AUTHORS COPYING INSTALL README NEWS \
  $PKG/usr/doc/read-edid-2.0.0
)
rm -rf $CWD/read-edid-2.0.0

# Strip tease ##############
( cd $PKG
  find . | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded
)
( cd $PKG/usr/man
  find . -name "*.?" | xargs gzip -9
)

# Some third party tools need to show in panel
cp $CWD/*.desktop $PKG/usr/share/applications/


# Install dir
mkdir $PKG/install
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
    cp -f $NEW $OLD
    rm -f $NEW
  elif [ "`cat $OLD | md5sum`" = "`cat $NEW | md5sum`" ]; then # toss the redundant copy
    rm -f $NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}

config etc/rc.d/rc.numlock.new 
EOF

cat << "EOF" > $PKG/install/slack-desc
systemtools: systemtools - various system utilities
systemtools:
systemtools: 
systemtools: This package includes tools related to system
systemtools: administration and their desktop menus entries.
systemtools:
systemtools: These tools use both dialog and Xdialog, providing
systemtools: ncurses and gtk frontends.
systemtools: 
systemtools:
systemtools:
EOF


# Packaging ##############
chown -R root:root $PKG

mkdir -p $PKG/usr/src/$NAME-$VERSION
cp $CWD/$NAME.SlackBuild $PKG/usr/src/$NAME-$VERSION/

cd $PKG

makepkg -l y -c n $PKG.txz

# cleanup
rm -rf $PKG
rm -rf $CWD/$NAME-$VERSION

