#!/bin/bash
#
# source: http://people.debian.org/~kov/gksu/gksu-2.0.2.tar.gz
#         http://people.debian.org/~kov/gksu/libgksu-2.0.12.tar.gz
#
# maintainer: gmg (g.monpontet@gmail.com)
#

soft='gksu'
softversion='2.0.2'
packageversion='64.2'

arch='i486'
cpu='i686'

buildir=$(pwd)
srcpkg="$buildir/$soft-$softversion.tar.gz"
src="$buildir/$soft-$softversion"
package="$soft-$softversion-$arch-$packageversion"
dest="$buildir/$package"

cd $buildir
rm -rf $src
tar -xvzf $buildir/libgksu-2.0.12.tar.gz
rm -f $dest.tgz
rm -rf $dest
mkdir -p $dest

cd libgksu-2.0.12

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 {} \;
export CFLAGS="-O2 -march=$arch -mtune=$cpu"
export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"

./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --mandir=/usr/man \
  --localstatedir=/var \
  --enable-static=no \
  --enable-gtk-doc=no \
  --disable-schemas-install

make -j2
make install
make install DESTDIR=$dest

mkdir -p $dest/usr/doc/libgksu-2.0.7
cp -a ABOUT-NLS AUTHORS ChangeLog COPYING INSTALL \
$dest/usr/doc/libgksu-2.0.7

cd ..

tar -xvzf $srcpkg

cd $src
cat $buildir/gksu-2.0.2-use-Terminal-for-terminal.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 {} \;
export CFLAGS="-O2 -march=$arch -mtune=$cpu"
export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"

./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --mandir=/usr/man \
  --localstatedir=/var \
  --enable-static=no \
  --enable-nautilus-extension=no \
  --enable-gtk-doc=no \
  --disable-schemas-install \
  --with-gconf-source="xml::/etc/gconf/gconf.xml.defaults"

make -j2
make install DESTDIR=$dest

# gconf stuff

export GCONF_CONFIG_SOURCE="xml::$dest/etc/gconf/gconf.xml.defaults"
if [ -d $dest/etc/gconf/schemas ]; then
	install -v -d -m755 $dest/etc/gconf/gconf.xml.defaults
	SCHEMAS=$dest/etc/gconf/schemas
	for schema in $SCHEMAS/*.schemas; do
	gconftool-2 --makefile-install-rule $schema
	done
	# Reset / Verify correct permissions
	( cd $dest/etc/gconf ; find . -type d -exec chmod 755 {} \; )
	( cd $dest/etc/gconf ; find . -type f -exec chmod 644 {} \; )
fi

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

## Compress and fix manpage links

( cd $dest/usr/man
  find . -type f -exec gzip -9 {} \;
  for i in $( find . -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
)

mkdir $dest/usr/libexec
mv $dest/usr/bin/gksu $dest/usr/libexec

# make gksu use locale

cat << 'EOF' > $dest/usr/bin/localize
#!/bin/sh
. /etc/profile.d/lang.sh
[ "$*" ] && exec $*
echo "Usage : localize <command>"
EOF
chmod 0755 $dest/usr/bin/localize

cat << 'EOF' > $dest/usr/bin/gksu
#!/bin/sh
[ "$*" ] && exec /usr/libexec/gksu localize "$*"
exec /usr/libexec/gksu
EOF
chmod 0755 $dest/usr/bin/gksu

mkdir -p $dest/usr/share/icons/hicolor/48x48/apps
cp -a $dest/usr/share/pixmaps/*.png /$dest/usr/share/icons/hicolor/48x48/apps

rm $dest/usr/share/applications/gksu-properties.desktop
cat << EOF > $dest/usr/share/applications/gksu-properties.desktop
[Desktop Entry]
Name=Privilege granting
Name[cs]=Přidělování práv
Name[eu]=Pribilegioa ematea
Name[fr]=Gestion des privilèges
Name[hu]=Jogosultság megadása
Name[it]=Concessione privilegi
Name[ko]=관리 권한 부여
Name[sv]=Rättighetstilldelning
Name[zh_CN]=权限授予
Comment=Configure behavior of the privilege-granting tool
Comment[cs]=Nastavuje chování nástroje pro přidělování práv
Comment[eu]=konfiguratu pribilegioak emateko tresnaren portaera
Comment[fr]=Configurer la gestion des privilèges super utilisateur
Comment[hu]=A jogosultságmegadó eszköz viselkedésének beállítása
Comment[it]=Configura il comportamento dello strumento di concessione privilegi
Comment[ko]=관리 권한 부여 프로그램의 동작을 설정합니다
Comment[sv]=Konfigurera beteendet för verktyget för rättighetstilldelning
Comment[zh_CN]=配置权限授予工具的行为
Exec=gksu-properties
Icon=gksu-root-terminal
StartupNotify=true
Terminal=false
Type=Application
NoDisplay=true
Categories=Application;System;Settings;X-Zenpanel;
EOF

# Description file

mkdir -p $dest/install
cat << EODESC > $dest/install/slack-desc
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.  Line
# up the first '|' above the ':' following the base package name, and the '|'
# on the right side marks the last column you can put a character in.  You must
# make exactly 11 lines for the formatting to be correct.  It's also
# customary to leave one space after the ':'.

    |-----handy-ruler--------------------------------------------------------|
gksu: gksu - is a Gtk+ frontend to su and sudo
gksu:
gksu: GKSu is a library that provides a Gtk+ frontend to su and sudo. It
gksu: supports login shells and preserving environment when acting as a su
gksu: frontend. It is useful to menu items or other graphical programs that
gksu: need to ask a user's password to run another program as another user
gksu:
gksu:
gksu:
gksu:
gksu:
EODESC

# Documentation

mkdir -p $dest/usr/doc/$soft-$softversion
cp -a ABOUT-NLS AUTHORS ChangeLog COPYING INSTALL \
$dest/usr/doc/$soft-$softversion

mkdir -p $dest/usr/src/$soft-$softversion
cp $buildir/build-$soft.sh $dest/usr/src/$soft-$softversion

# Packaging

cd $dest
makepkg -l y -c n $dest.txz
makepkg -l y -c n $dest.tgz
cd $buildir
md5sum $package.txz > $package.md5