#!/bin/bash
#
# author           : George Vlahavas (vlahavas~AT~gmail~DOT~com)
# project web page : http://xchm.sourceforge.net/

# Package name
NAME="xchm"
# Package version
VERSION="1.17"
# Source code URL
SOURCE="http://downloads.sourceforge.net/xchm/xchm-$VERSION.tar.gz"
# Files that should be placed in /usr/doc/package-version/
DOCS="ABOUT-NLS AUTHORS COPYING ChangeLog INSTALL NEWS README"

CWD=`pwd`
PKG="$CWD/$NAME-install"
ARCH="i486"
CPU="i686"
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: xchm (a viewer for .chm help files)
$NAME:
$NAME: xCHM is a cross-platform GUI front-end to chmlib. It can show the
$NAME: content tree if one is available, print pages, change font face
$NAME: and size, remember bookmarks, display a searchable index, and
$NAME: search for words in the current page and the whole book.
$NAME:
$NAME:
$NAME:
$NAME:
$NAME:
END

# Create the desktop entry
mkdir -p $PKG/usr/share/applications
cat > $PKG/usr/share/applications/$NAME.desktop <<EOF
[Desktop Entry]
Encoding=UTF-8
Name=xCHM
Exec=xchm
Terminal=false
Type=Application
Categories=Office;Viewer;
Icon=xchm
MimeType=application/x-chm
EOF

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

if [ $ARCH = "x86_64" ];then
	export CFLAGS="-O2 -fPIC"
	export CXXFLAGS=$CFLAGS
	LIBDIRSUFFIX="64"
else
	export CFLAGS="-O2 -march=$ARCH -mtune=i686"
	export CXXFLAGS=$CFLAGS
	LIBDIRSUFFIX=""
fi

# Configure and make
cd $NAME-$VERSION
./configure --prefix=/usr --libdir=/usr/lib${LIBDIRSUFFIX} --mandir=/usr/man
make

# Install on temporary directory
make install DESTDIR=$PKG

# Strip binaries
cd $PKG
find . | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

# Copy Docs
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cd $CWD/$NAME-$VERSION
cp -a $DOCS $PKG/usr/doc/$NAME-$VERSION

# Symlink icons
ICONSIZES="128 48 32 16"
for i in $ICONSIZES; do
	mkdir -p $PKG/usr/share/icons/hicolor/$i\x$i/apps;
	cd $PKG/usr/share/icons/hicolor/$i\x$i/apps;
	ln -sf ../../../../pixmaps/$NAME-$i.xpm $PKG/usr/share/icons/hicolor/$i\x$i/apps/$NAME.xpm;
	mkdir -p $PKG/usr/share/icons/hicolor/$i\x$i/mimetypes;
	cd $PKG/usr/share/icons/hicolor/$i\x$i/mimetypes;
	ln -sf ../../../../pixmaps/$NAME\doc-$i.xpm $PKG/usr/share/icons/hicolor/$i\x$i/mimetypes/gnome-mime-application-x-chm.xpm;
done;

# Create the doinst.sh script to update the mime databases
cat << "EOF" > $PKG/install/doinst.sh
# update the mime desktop database
if [ -x usr/bin/update-desktop-database ]; then
        usr/bin/update-desktop-database -q
fi
# update the mime database
if [ -x usr/bin/update-mime-database ]; then
        usr/bin/update-mime-database usr/share/mime > /dev/null 2>&1
fi
EOF

# 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