#!/bin/bash
#
# author           : George Vlahavas (vlahavas~AT~gmail~DOT~com)
# project web page : http://www.unihedron.com/projects/gonvert/index.php

# Package name
NAME="gonvert"
# Package version
VERSION="0.2.22"

CWD=`pwd`
PKG="$CWD/$NAME-install"
ARCH="noarch"
BUILD="1gv"

rm -rf $PKG
mkdir -p $PKG

# Create package install directory
mkdir $PKG/install

# Create the slack-desc file
cat > $PKG/install/slack-desc << END
$NAME: gonvert is a unit conversion utility
$NAME:
$NAME: gonvert is a conversion utility that allows conversion between many
$NAME: units like CGS, Ancient, Imperial with many categories like length,
$NAME: mass, numbers, etc. All units converted values shown at once as you
$NAME: type. Easy to add/change your own units.
$NAME:
$NAME:
$NAME:
$NAME:
$NAME:
END

# Unpack source
tar xvzf $NAME-$VERSION.tar.gz

# Change original destination of /usr/local to /usr
cd $NAME-$VERSION
sed -i -e 's,/usr/local,/usr,' Makefile

# Install on temporary directory
make install DESTDIR=$PKG

# Copy Docs
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cd $CWD/$NAME-$VERSION
cp -a \
	doc/{README,COPYING,THANKS,INSTALL,FAQ,TODO,CHANGELOG} \
	$PKG/usr/doc/$NAME-$VERSION
rm -rf $PKG/usr/share/doc

mkdir -p $PKG/usr/share/icons/hicolor/48x48/apps/
( cd $PKG/usr/share/icons/hicolor/48x48/apps; ln -s ../../../../pixmaps/gonvert.png gonvert.png )
rm $PKG/usr/share/pixmaps/gonvert-icon_alternative.png

# Create the .desktop file
mkdir -p $PKG/usr/share/applications
cp $CWD/$NAME-$VERSION/gonvert.desktop $PKG/usr/share/applications
rm -rf $PKG/usr/share/gnome
sed -i -e 's/.png//' $PKG/usr/share/applications/gonvert.desktop

# Make sure ownerships and permissions are sane
cd $PKG
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 {} \;

# Make the package
/sbin/makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.txz

# Calculate md5sum
cd $CWD/
md5sum $NAME-$VERSION-$ARCH-$BUILD.txz > $NAME-$VERSION-$ARCH-$BUILD.md5

# Remove source-code and temporary install directories
# since they are not needed anymore
rm -rf $CWD/$NAME-$VERSION
rm -rf $PKG