#!/bin/bash
#  Copyright (C) 2010, 2012  Matias A. Fonzo, <selk@dragora.org>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -e

CWD=$(pwd)

TMP=${TMP:-/tmp/sources}
OUT=${OUT:-/tmp/packages}

V=2.7.8
ARCH=${ARCH:-x86_64}
B=4

# Flags for the compiler:
DCFLAGS=${DCFLAGS:=-O2 -mtune=generic}

# Parallel jobs for the compiler:
JOBS=${JOBS:=-j4}

PKG=${TMP}/package-libxml2

rm -rf $PKG
mkdir -p $PKG $OUT

echo "Uncompressing the tarball..."
rm -rf ${TMP}/libxml2-${V}
lzip -cd ${CWD}/libxml2-${V}.tar.lz | tar -xvf - -C $TMP

cd ${TMP}/libxml2-${V}

# Set sane ownerships and permissions:
chown -R 0:0 .
find . \
 \( -perm 2777 -o \
    -perm 777  -o \
    -perm 775  -o \
    -perm 711  -o \
    -perm 555  -o \
    -perm 511     \
 \) -exec chmod 755 {} + \
  -o \
 \( -perm 666 -o \
    -perm 664 -o \
    -perm 600 -o \
    -perm 444 -o \
    -perm 440 -o \
    -perm 400    \
 \) -exec chmod 644 {} +

# Important upstream patch:
zcat ${CWD}/libxml2-2.7.8-versioning.patch.gz | patch -p1 --verbose

# Fix CVE-2010-4494:
zcat ${CWD}/patches/df83c17e5a2646bd923f75e5e507bc80d73c9722.patch.gz | \
 patch -p1 --verbose

# Fix CVE-2011-0216:
zcat ${CWD}/patches/69f04562f75212bfcabecd190ea8b06ace28ece2.patch.gz | \
 patch -p1 --verbose

# Fix CVE-2011-1944:
zcat ${CWD}/patches/d7958b21e7f8c447a26bb2436f08402b2c308be4.patch.gz | \
 patch -p1 --verbose

# Fix CVE-2011-3102:
zcat ${CWD}/patches/d8e1faeaa99c7a7c07af01c1c72de352eb590a3e.patch.gz | \
 patch -p1 --verbose

# Fix CVE-2011-3905:
zcat ${CWD}/patches/77404b8b69bc122d12231807abf1a837d121b551.patch.gz | \
 patch -p1 --verbose

# Fix CVE-2011-3919:
zcat ${CWD}/patches/5bd3c061823a8499b27422aee04ea20aae24f03e.patch.gz | \
 patch -p1 --verbose

# Fix CVE-2012-2807:
zcat ${CWD}/patches/CVE-2012-2807/459eeb9dc752d5185f57ff6b135027f11981a626.patch.gz | \
 patch -p1 --verbose
zcat ${CWD}/patches/CVE-2012-2807/4f9fdc709c4861c390cd84e2ed1fd878b3442e28.patch.gz | \
 patch -p1 --verbose
zcat ${CWD}/patches/CVE-2012-2807/baaf03f80f817bb34c421421e6cb4d68c353ac9a.patch.gz | \
 patch -p1 --verbose

# Fix CVE-2012-8941:
zcat ${CWD}/patches/8973d58b7498fa5100a876815476b81fd1a2412a.patch.gz | \
 patch -p1 --verbose

autoreconf

PY_VER=$(python -c 'import sys ; print sys.version[:3]')

CFLAGS="$DCFLAGS" \
./configure \
 --prefix=/usr \
 --mandir=/usr/man \
 --docdir=/usr/doc/libxml2-${V} \
 --localstatedir=/var \
 --libdir=/usr/lib64 \
 --disable-static \
 --enable-shared \
 --build=${ARCH}-dragora-linux-gnu

make $JOBS pythondir=/usr/lib/python${PY_VER}/site-packages
make install DESTDIR=$PKG pythondir=/usr/lib/python${PY_VER}/site-packages

# Strip binaries & libraries:
( cd $PKG
  find . -type f | xargs file | awk '/ELF/ && /executable/ || /shared object/' | \
   cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# Compress man-pages:
( cd ${PKG}/usr/man ; gzip -9N man?/* )

# Add and move the documentation under the correct location:
mkdir -p ${PKG}/usr/doc/libxml2-${V}
mv ${PKG}/usr/share/doc ${PKG}/usr/doc/libxml2-${V}
cp -a \
 AUTHORS Copyright NEWS README* TODO* \
 ${PKG}/usr/doc/libxml2-${V}
( cd ${PKG}/usr/doc/libxml2-${V}
  find . -type f | xargs chmod 644
)

# Copy the description files:
mkdir -p ${PKG}/description
cp ${CWD}/description/* ${PKG}/description

# Include the post-install script:
mkdir -p ${PKG}/install
zcat ${CWD}/post-install.gz > ${PKG}/install/post-install

cd $PKG
makepkg -l ${OUT}/libxml2-${V}-${ARCH}-${B}.tlz

