#!/bin/sh
export LANG=C
CWD=`pwd`
PKG_NAME=ptetex3
SRC_VERSION=20090610
ARCHI_VERSION=i586
BUILD_VERSION=P3
SRC_HOME="/home/archives/source"
SRC_FILE="${PKG_NAME}-${SRC_VERSION}.tar.gz"
WORK_PKG=$CWD/build
WORK=$CWD/${PKG_NAME}-${SRC_VERSION}
patchfiles="000-make.dif 001-install.diff"
xpatchfile="099-remove.diff"
################################################
# Keep variables below.
################################################
unset TEXDIR
export PATH=$PATH:/usr/X11R7/bin
export PKG_CONFIG_PATH=/usr/X11R7/lib/pkgconfig:/usr/lib/pkgconfig
# important
export CFLAGS="-D_XOPEN_SOURCE=500"

###############################################
# Clean up. remove work directory
###############################################
if [ -d /opt/teTeX.old ]; then
	echo "/opt/teTeX.old exists!! please delete first"
	exit 255
fi

cp $SRC_HOME/$SRC_FILE .
cp $SRC_HOME/tetex-src-3.0.tar.gz .
cp $SRC_HOME/tetex-texmf-3.0po.tar.gz .
if [ $? -ne 0 ]; then
 exit 255
fi

zcat $SRC_FILE | tar xvf -
for i in $patchfiles
do
	patch -p0 < $i
	if [ $? -ne 0 ]; then
	 exit 255
	fi
done

patch -p0 < $xpatchfile
if [ $? -ne 0 ]; then
 exit 255
fi

cp $WORK/my_option.sample my_option

pushd $WORK
INSTONLY=0
if [ $INSTONLY -ne 1 ]; then
	make all0
	if [ $? -ne 0 ]; then
	 exit 255
	fi
	make otf
	if [ $? -ne 0 ]; then
	 exit 255
	fi
	make babel
	if [ $? -ne 0 ]; then
	 exit 255
	fi
	make font
	if [ $? -ne 0 ]; then
	 exit 255
	fi
	make test
	if [ $? -ne 0 ]; then
	 exit 255
	fi
fi

mv /opt/teTeX /opt/teTeX.old
make install
popd

rm -rf $WORK_PKG
mkdir -p $WORK_PKG/opt
mv /opt/teTeX $WORK_PKG/opt
mkdir -p $WORK_PKG/etc/profile.d
cp -p $CWD/ptetex.sh $CWD/ptetex.csh $WORK_PKG/etc/profile.d
cp -p $CWD/plamo_update_tex.sh $WORK_PKG/opt/teTeX/bin

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 -p $CWD/ptetex.sh $CWD/ptetex.csh \
  $WORK_PKG/usr/share/doc/${PKG_NAME}-${SRC_VERSION}/build
pushd $CWD
cp -ap $patchfiles \
  $WORK_PKG/usr/share/doc/${PKG_NAME}-${SRC_VERSION}/build
popd
cp -ap $CWD/$xpatchfile \
  $WORK_PKG/usr/share/doc/${PKG_NAME}-${SRC_VERSION}/build
cp -ap $CWD/plamo_update_tex.sh \
  $WORK_PKG/usr/share/doc/${PKG_NAME}-${SRC_VERSION}/build

# Additional doinst.
mkdir -p $WORK_PKG/install
cat << EOF >> $WORK_PKG/install/initpkg
/bin/bash /opt/teTeX/bin/plamo_update_tex.sh yes
EOF

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

