#!/bin/bash
#  Copyright (C) 2010-2011  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)

unpack() {
  echo "Uncompressing the tarball..."
  lzip -cd "$1" | tar -xvf - -C $TMP
}

set_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 {} +
}

strip_elfs() {
  ( cd "$1"
    find . -type f | xargs file | awk '/ELF/ && /executable/ || /shared object/' | \
     cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true;
  )
}

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

V=2.28.6
GOBJECT_V=0.10.8
ARCH=${ARCH:-i486}
B=2

# Flags for the compiler:
DCFLAGS=${DCFLAGS:=-O2 -march=i486 -mtune=i686}

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

PKG1=${TMP}/package-gobject-introspection
PKG2=${TMP}/package-pygobject

rm -rf $PKG{1,2}
mkdir -p $PKG{1,2} $OUT

cd $TMP

rm -rf gobject-introspection-${GOBJECT_V}
unpack ${CWD}/gobject-introspection-${GOBJECT_V}.tar.lz 
cd gobject-introspection-${GOBJECT_V}
set_permissions
CFLAGS="$DCFLAGS" \
./configure \
 --prefix=/usr \
 --localstatedir=/var \
 --disable-static \
 --enable-shared \
 --build=${ARCH}-dragora-linux-gnu
make $JOBS
make install DESTDIR=$PKG1
strip_elfs $PKG1
mkdir -p ${PKG1}/usr/doc/gobject-introspection-${GOBJECT_V}
cp -a \
 AUTHORS CONTRIBUTORS COPYING* ChangeLog NEWS README TODO \
 ${PKG1}/usr/doc/gobject-introspection-${GOBJECT_V}
mkdir -p ${PKG1}/description
cp ${CWD}/descriptions/gobject-introspection/?? ${PKG1}/description
( cd $PKG1
  makepkg -l ${OUT}/gobject-introspection-${GOBJECT_V}-${ARCH}-${B}.tlz
)
( cd $OUT
  /sbin/pkg upgrade gobject-introspection-${GOBJECT_V}-${ARCH}-${B}.tlz || \
   /sbin/pkg add gobject-introspection-${GOBJECT_V}-${ARCH}-${B}.tlz
)

cd -

rm -rf pygobject-${V}
unpack ${CWD}/pygobject-${V}.tar.lz
cd pygobject-${V}
set_permissions
CFLAGS="$DCFLAGS" \
./configure \
 --prefix=/usr \
 --localstatedir=/var \
 --disable-static \
 --enable-shared \
 --build=${ARCH}-dragora-linux-gnu
make $JOBS
make install DESTDIR=$PKG2
strip_elfs $PKG2
mkdir -p ${PKG2}/usr/doc/pygobject-${V}
cp -a \
 AUTHORS COPYING ChangeLog NEWS PKG-INFO README \
 ${PKG2}/usr/doc/pygobject-${V}
mkdir -p ${PKG2}/description
cp ${CWD}/descriptions/pygobject/?? ${PKG2}/description
( cd $PKG2
  makepkg -l ${OUT}/pygobject-${V}-${ARCH}-${B}.tlz
)
echo ""
echo "# Note: Install the package (pygobject) manually."
echo ""

