#!/bin/sh
export LANG=C
SRC_DIR=/home/archives/source
CWD=`pwd`
export WORK=$CWD/work
export WORK_PKG=$CWD/work/pkg
export CFLAGS="-O2"
export MAKEFLAGS="-j 6"
PKG_NAME=libzip
SRC_VERSION=0.8
ARCHI_VERSION=i386
BUILD_VERSION=P1
SRC_FILE="${PKG_NAME}-${SRC_VERSION}.tar.gz"
#SRC_PATCH=""
PATH=$PATH:/opt/kde/bin:/usr/bin:/opt/www/htdig/bin:/usr/X11R7/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 http://www.nih.at/libzip/$i);
  fi;
done

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

echo "Building ${PKG_NAME}-${SRC_VERSION}.tar.gz..."
cd $WORK
tar xvf $SRC_DIR/$SRC_FILE

#for i in $CWD/files/patch* ; do 
#  patch -p0 < $i;
#done
cd ${PKG_NAME}-${SRC_VERSION}
./configure --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man
make
make install DESTDIR=$WORK_PKG

cd ../${PKG_NAME}-${SRC_VERSION}
mkdir -p $WORK_PKG/usr/share/doc/${PKG_NAME}-${SRC_VERSION}
cp -ap AUTHORS INSTALL \
  NEWS README THANKS TODO \
  $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/*
strip $WORK_PKG/usr/lib/*

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

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