#!/bin/sh
export LANG=C
SRC_DIR=/usr/src/distfiles/kde
CWD=`pwd`
export WORK=$CWD/work
export WORK_PKG=$CWD/work/pkg
export CFLAGS="-O2"
export MAKEFLAGS="-j 2"
PKG_NAME=htdig
SRC_VERSION=3.2.0b6
ARCHI_VERSION=i386
BUILD_VERSION=P1
SRC_FILE="${PKG_NAME}-${SRC_VERSION}.tar.bz2"
SRC_SITE="http://www.htdig.org/files"
PATCH_SITE=""
CFG_OPT="--host=i686-pc-linux-gnu --build=i686-pc-linux-gnu --prefix=/opt/www --infodir=/usr/share/info --mandir=/usr/share/man
make"
DOC_FILE="COPYING ChangeLog* README* STATUS"

PATH=$PATH:/opt/kde/bin:/usr/bin:/opt/www/bin:/usr/X11R6/bin

# 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 ${SRC_SITE}/$i);
  fi;
done

#for i in $SRC_PATCH ; do 
#  if [ ! -f $SRC_DIR/$i ]; then
#    (cd $SRC_DIR; wget ${PATCH_SITE}/$i);
#  fi;
#done

echo "Building ${PKG_NAME}-${SRC_VERSION}.tar.bz2..."
cd $WORK
tar xjvf $SRC_DIR/$SRC_FILE
#for i in $CWD/files/patch* ; do 
#patch -p0 < $i;
#done

cd ${PKG_NAME}-${SRC_VERSION}
./configure ${CFG_OPT}
make
make install-strip DESTDIR=$WORK_PKG

# copy doc files
mkdir -p $WORK_PKG/usr/share/doc/${PKG_NAME}-${SRC_VERSION}
cp -ap ${DOC_FILE} \
  $WORK_PKG/usr/share/doc/${PKG_NAME}-${SRC_VERSION}
gzip -9 $WORK_PKG/usr/share/doc/${PKG_NAME}-${SRC_VERSION}/*
chown -R root.root $WORK_PKG/usr/share/doc/${PKG_NAME}-${SRC_VERSION}
strip $WORK_PKG/usr/bin/*

# copy env files.
mkdir -p $WORK_PKG/etc/profile.d
cp -p $CWD/files/${PKG_NAME}.sh $WORK_PKG/etc/profile.d
cp -p $CWD/files/${PKG_NAME}.csh $WORK_PKG/etc/profile.d

# copy build env.
mkdir -p $WORK_PKG/usr/share/doc/${PKG_NAME}-${SRC_VERSION}/build
cp -ap $CWD/PlamoBuild \
  $WORK_PKG/usr/share/doc/${PKG_NAME}-${SRC_VERSION}/build
cp -apr $CWD/files \
  $WORK_PKG/usr/share/doc/${PKG_NAME}-${SRC_VERSION}/build

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