#!/bin/sh
export LANG=C
SRC_DIR=/home/archives/source
CWD=`pwd`
WORK=$CWD/work
WORK_PKG=$CWD/work/pkg
CFLAGS="-O2"
MAKEFLAGS="-j 3"
PKG_NAME=strigi
SRC_VERSION=1019159
ARCHI_VERSION=i586
BUILD_VERSION=P3
SRC_FILE="strigi-${SRC_VERSION}.tar.bz2"

################################################
# Keep variables below.
################################################
export PATH=$PATH:/opt/kde/bin:/usr/bin:/opt/www/htdig/bin:/usr/X11R7/bin
export PKG_CONFIG_PATH=/usr/X11R7/lib/pkgconfig:/usr/lib/pkgconfig:/opt/kde/lib/pkgconfig
export XDG_DATA_DIRS=/opt/kde/share:/usr/share:/usr/local/share

###############################################
# Clean up. remove work directory
###############################################
if [ -d $WORK ]; then
  rm -rf $WORK;
fi;
mkdir $WORK

# check source file
# if not exist, get files.
#for i in $SRC_FILE ; do 
#  if [ ! -f $SRC_DIR/$i ]; then
#    (cd $SRC_DIR; wget http://www.strigi.org/files/v2.4/$i);
#  fi;
#done


echo "Building strigi-${SRC_VERSION}.tar.bz2..."
cd $WORK
tar xvf $SRC_DIR/$SRC_FILE
pushd $WORK/${PKG_NAME}-${SRC_VERSION}
for i in $CWD/files/0??-*.diff ; do
	patch -p0 < $i
	if [ $? -ne 0 ]; then
		echo "$i: not applied properly"
		exit 255
	fi
done
popd

###############################################
# Build
###############################################
cd strigi-${SRC_VERSION}
cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Release
make

make install DESTDIR=$WORK_PKG

mkdir -p $WORK_PKG/usr/share/doc/strigi-${SRC_VERSION}
cp -ap AUTHORS COPYING ChangeLog NEWS README TODO TODO.Phreedom \
  TODOMONDAY \
  $WORK_PKG/usr/share/doc/strigi-${SRC_VERSION}
gzip -9 $WORK_PKG/usr/share/doc/strigi-${SRC_VERSION}/*
chown -R root.root $WORK_PKG/usr/share/doc/strigi-${SRC_VERSION}
strip $WORK_PKG/usr/bin/*

mkdir -p $WORK_PKG/usr/share/doc/strigi-${SRC_VERSION}/build
cp -ap $CWD/PlamoBuild \
  $WORK_PKG/usr/share/doc/strigi-${SRC_VERSION}/build

mkdir -p $WORK_PKG/usr/share/doc/${PKG_NAME}-${SRC_VERSION}/build/files
cp -ap $CWD/files/* \
  $WORK_PKG/usr/share/doc/${PKG_NAME}-${SRC_VERSION}/build/files

#make package
cd $WORK_PKG
echo "y
" | makepkg strigi-${SRC_VERSION}.tgz
mv strigi-${SRC_VERSION}.tgz $CWD/strigi-${SRC_VERSION}-${ARCHI_VERSION}-${BUILD_VERSION}.tgz
cd $CWD
