#!/bin/sh
export LANG=C
SRC_DIR=/usr/src/distfiles
CWD=`pwd`
export WORK=$CWD/work
export WORK_PKG=$CWD/work/pkg
export CFLAGS="-O2"
export MAKEFLAGS="-j 2"
SRC_VERSION=4.0.6+0.20041122cvs
ARCHI_VERSION=noarch
BUILD_VERSION=P1
SRC_FILE=egg_${SRC_VERSION}.orig.tar.gz
SRC_PATCH="\
egg_4.0.6+0.20041122cvs-11.diff.gz"
PATH=$PATH:/opt/kde/bin:/usr/bin:/opt/www/htdig/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 http://ring.asahi-net.or.jp/pub/linux/debian/debian/pool/main/e/egg/$i);
  fi;
done

for i in $SRC_PATCH ; do 
  if [ ! -f $SRC_DIR/$i ]; then
    (cd $SRC_DIR; wget http://ring.asahi-net.or.jp/pub/linux/debian/debian/pool/main/e/egg/$i);
  fi;
done

echo "Building egg_${SRC_VERSION}.tar.gz..."
cd $WORK
tar xvf $SRC_DIR/$SRC_FILE
for i in $SRC_PATCH ; do 
patch -p0 < $SRC_DIR/$i;
done

cd egg-${SRC_VERSION}
./configure --prefix=/usr
make
mkdir -p $WORK_PKG/usr
mkdir -p $WORK_PKG/usr/share/emacs/site-lisp
make install prefix=$WORK_PKG/usr

mkdir -p $WORK_PKG/usr/share/doc/egg-${SRC_VERSION}
cp -ap AUTHORS COPYING ChangeLog* \
  PROBLEMS README.ja.txt TODO \
  $WORK_PKG/usr/share/doc/egg-${SRC_VERSION}
gzip -9 $WORK_PKG/usr/share/doc/egg-${SRC_VERSION}/*
chown -R root.root $WORK_PKG/usr/share/doc/egg-${SRC_VERSION}
strip $WORK_PKG/usr/bin/*

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

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