#!/bin/bash

# Zenwalk Gnu/Linux build script written for "pmount"
# Soft Home:	http://alioth.debian.org/projects/pmount/
# Source URL:	https://alioth.debian.org/frs/download.php/3310/pmount-0.9.23.tar.bz2
# Soft's Deps: 	dbus,hal,util-linux-ng
# Package Maintainer: Markus Muttilainen stillborn.zenwalk@gmail.com

set -e -u -x
#------------------( Log )
(

#------------------( Unique Package Variables )
SOFT='pmount'
SOFTVERSION='0.9.23'
BUILDVERSION='66.1'
ARCH='i486'
CPU='i686'
PACKAGE="$SOFT-$SOFTVERSION-$ARCH-$BUILDVERSION"
BUILDIR=$(pwd)
DEST="$BUILDIR/$PACKAGE"


#------------------( Source )
SOURCES="pmount-0.9.23.tar.bz2"


for file in $SOURCES ; do
	echo -n "$file :"
		if [ -e $file ]; then
        		echo " OK"
		else
			echo " MISSING !"
			exit 1
		fi
done


#------------------( Clean Work Directory )
cd $BUILDIR
rm -rf \
	$DEST $DEST.dep $DEST.md5 $DEST.txz \
	pmount-0.9.23 1>/dev/null 2>&1


#------------------( Create The Necessary Package Directories )
mkdir -p $DEST/install
mkdir -p $DEST/usr/doc/$SOFT-$SOFTVERSION
mkdir -p $DEST/usr/src/$SOFT-$SOFTVERSION


#------------------( Extract Source And Ensure Permissions )
cd $BUILDIR
tar -xf $SOURCES
cd $SOFT-$SOFTVERSION
  chown -R root:root .
    find . -perm 666 -exec chmod 644 {} \;
    find . -perm 664 -exec chmod 644 {} \;
    find . -perm 600 -exec chmod 644 {} \;
    find . -perm 444 -exec chmod 644 {} \;
    find . -perm 400 -exec chmod 644 {} \;
    find . -perm 440 -exec chmod 644 {} \;
    find . -perm 777 -exec chmod 755 {} \;
    find . -perm 775 -exec chmod 755 {} \;
    find . -perm 511 -exec chmod 755 {} \;
    find . -perm 711 -exec chmod 755 {} \;
    find . -perm 555 -exec chmod 755 {} \;


#------------------( Export Flags, Configure, Build & Install To Package )
export CFLAGS="-O2 -march=$ARCH -mtune=$CPU"
export CXXFLAGS="$CFLAGS"


./configure \
  --prefix=/usr \
  --libdir=/usr/lib \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$SOFT-$SOFTVERSION \
  --enable-hal \
  --disable-static \
  --build=$ARCH-zenwalk-linux
make -j2 || exit 1
make install DESTDIR=$DEST

# Give pmount/pumount suid bit.
chown root.plugdev $DEST/usr/bin/pmount $DEST/usr/bin/pumount
chmod 4750 $DEST/usr/bin/pmount $DEST/usr/bin/pumount

# Let's not clobber the config file
mv $DEST/etc/pmount.allow $DEST/etc/pmount.allow.new



#------------------( Copy Documentation To The Package )
cp -av  AUTHORS COPYING ChangeLog INSTALL README.devel TODO \
          $DEST/usr/doc/$SOFT-$SOFTVERSION


#------------------( Create Slack-desc )
cat <<EOF > $DEST/install/slack-desc
      |-----handy-ruler------------------------------------------------------|
pmount: pmount (Wrapper which allows users to mount removable devices)
pmount: 
pmount: pmount is a wrapper around the standard mount program which
pmount: permits normal users to mount removable devices without a
pmount: matching /etc/fstab entry. This package also contains a
pmount: wrapper "pmount-hal" which reads some information like device
pmount: labels and mount options from hal and passes them to pmount.
pmount: Install the package "hal" if you want to use this feature.
pmount: 
pmount: 
pmount: 
EOF


#------------------( Remove Unneeded Symbols And Sections From Binaries )
( cd $DEST
     find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null
     find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null
)


#------------------( Copy The Build Script Into The Package )
cp $BUILDIR/build-$SOFT.sh $DEST/usr/src/$SOFT-$SOFTVERSION/build-$SOFT.sh


#------------------( Compress The Man Pages )
gzip -9 $DEST/usr/man/*/*


#------------------( helperline )
(cd $DEST;find . -printf "%y %m %u:%g %p %l\n") >$BUILDIR/pkgtree.log


#------------------( Compress The Package )
cd $DEST
makepkg -l y -c n $BUILDIR/$SOFT-$SOFTVERSION-$ARCH-$BUILDVERSION.txz


#------------------( Create The Dependency Listing )
cd $BUILDIR
        depfinder $PACKAGE.txz > $BUILDIR/$SOFT-$SOFTVERSION-$ARCH-$BUILDVERSION.dep


#------------------( Generate The Package's Checksum )
cd $BUILDIR
md5sum $PACKAGE.txz > $BUILDIR/$SOFT-$SOFTVERSION-$ARCH-$BUILDVERSION.md5


#------------------( Clean Our Messy Build Area )
cd $BUILDIR
rm -rf $SOFT-$SOFTVERSION $DEST

#------------------( log )
) 2>&1 | tee pmount-build.log.txt
