#!/bin/sh
#
# Copyright (c) 2007, 2008 Marco A. Frias B. <marcofrias@linuxpackages.net>
# All rights reserved.
#
#   Permission to use, copy, modify, and distribute this script for
#   any purpose with or without fee is hereby granted, provided that
#   the above copyright notice and this permission notice appear in all
#   copies.
#
#   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
#   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#   IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
#   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
#   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#   SUCH DAMAGE.
#
# Based on Template SlackBuild written by Eric Hameleers <alien@slackware.com>
# -----------------------------------------------------------------------------
#
# Slackware SlackBuild Script
# ===========================
#
# By:    Marco A. Frias B. <marcofrias@linuxpackages.net>
# For:   openoffice.org-langpack
# Descr: Native LangPack for OpenOffice.org Suite
# URL:   http://projects.openoffice.org/native-lang.html
# Needs: openoffice.org
#
# Changelog:
#
# -- Slackware 12.1
#
# 2.4.1-1: 05/Jul/2008 by Marco A. Frias B. <marcofrias@linuxpackages.net>
#          Release new version and rewrite SlackBuild.
#
# -- Slackware 12.0
#
# 2.4.0-1: 03/Apr/2008 by Marco A. Frias B. <marcofrias@linuxpackages.net>
#          Release new version 2.4.0.
#
# 2.3.0-2: 23/Jan/2008 by Marco A. Frias B. <marcofrias@linuxpackages.net>
#          Rewrite SlackBuild.
#
# 2.3.0-1: 17/Nov/2007 by Marco A. Frias B. <marcofrias@linuxpackages.net>
#          * Initial build.
#
# NOTE:
#      Run 'sh openoffice.org-langpack.SlackBuild --cleanup' to build a Slackware package.
# -----------------------------------------------------------------------------

## Name and version of the package
PRGNAME=openoffice.org-langpack
VERSION=2.4.1
PRGVERSION=2.4
LOCALE=es

## Source name and source version of the program
SRCNAME=${SRCNAME:-OOo}
SRCVERSION=${SRCVERSION:-2.4.1}

## The arch we will build for
ARCH=${ARCH:-noarch}

## Build tag
RELEASE=1
BUILDTAG=mfb

## The documentation files:
DOCFILES="licenses/LICENSE_es licenses/LICENSE_es.html readmes/README_es \
          readmes/README_es.html"

## Where do we look for sources?
CWD=$(pwd)
SRCDIR=$(dirname $0)
[ "${SRCDIR:0:1}" == "." ] && SRCDIR=$CWD/${SRCDIR}

## Place to build (TMP) package (PKG) and output (OUTPUT) the program:
TMP=${TMP:-/tmp/build}
PKG=$TMP/package-${PRGNAME}
OUTPUT=${OUTPUT:-/usr/src/slackpackages}

## Full path of source:
SOURCE="$SRCDIR/${SRCNAME}_${SRCVERSION}_LinuxIntel_langpack_${LOCALE}.tar.gz"
SOURCEDIR=$(tar -tzf ${SOURCE} | head -1 | tr -d \/)

##
## --- with a little luck, you won't have to edit below this point ---
##

## Exit the script on errors:
set -e
trap 'echo "$0 FAILED at line ${LINENO}" | tee $TMP/error-${PRGNAME}.log' ERR

## Catch unitialized variables:
set -u
P1=${1:-1}

## Create working directories:
mkdir -p $OUTPUT             # place for the package to be saved
mkdir -p $TMP/tmp-${PRGNAME} # location to build the source
mkdir -p $PKG                # place for the package to be built

rm -rf $PKG/*                # always erase old package's contents
rm -rf $TMP/tmp-${PRGNAME}/* # remove the remnants of previous build

## Source file availability:
if ! [ -f ${SOURCE} ]; then
  echo -e "\E[1;31m"
  echo -ne "File '$(basename ${SOURCE})' not available... aborting the build."
  echo -e "\E[0m\n"
  exit 1
fi

## --- PACKAGE BUILDING ---
cd $TMP/tmp-${PRGNAME}

echo -e "\E[1;32m"
echo -ne "Extracting the source archive(s) for $PRGNAME..."
echo -e "\E[0m\n"
sleep 1

if $(file ${SOURCE} | grep -q ": bzip2"); then
  tar -xjvf ${SOURCE}
elif $(file ${SOURCE} | grep -q ": gzip"); then
  tar -xzvf ${SOURCE}
fi

(cd $SOURCEDIR/RPMS
for rpmfile in *.rpm; do rpm2cpio < $rpmfile | cpio -imdv; done
mv opt $PKG
)

## Add documentation:
mkdir -p $PKG/usr/doc/${PRGNAME}-${VERSION}${LOCALE}
for docfile in ${DOCFILES}; do
  cp -a $PKG/opt/openoffice.org$PRGVERSION/$docfile $PKG/usr/doc/${PRGNAME}-${VERSION}${LOCALE}
done;

find $PKG -type d -exec chmod 755 {} \;
chmod -R u+rw,go+r-w,a-s $PKG
chown -R root.root $PKG

echo -e "\E[1;32m"
echo -ne "Building package..."
echo -e "\E[0m\n"
sleep 1

## Add slackbuild files:
mkdir -p $PKG/usr/src/slackbuilds/${PRGNAME}-${VERSION}${LOCALE}

(cd $PKG/usr/src/slackbuilds/${PRGNAME}-${VERSION}${LOCALE}
install -m 644 $SRCDIR/slack-desc .
install -m 644 $SRCDIR/slack-required .
install -m 644 $SRCDIR/${PRGNAME}.info .
install -m 644 $SRCDIR/${PRGNAME}.SlackBuild .
)

# Add a package description:
mkdir -p $PKG/install
install -m 644 $SRCDIR/slack-desc $PKG/install
install -m 644 $SRCDIR/slack-required $PKG/install

## Build the package:
cd $PKG
makepkg -l y -c n $OUTPUT/${PRGNAME}-${VERSION}${LOCALE}-${ARCH}-${RELEASE}${BUILDTAG}.tgz \
  2>&1 | tee $TMP/makepkg-${PRGNAME}.log

echo -e "\E[1;32m"
echo -ne "Your $PRGNAME package is complete. It is located in $OUTPUT"
echo -e "\E[0m\n"

if [ "$P1" = "--cleanup" ]; then
  rm -rf $TMP/tmp-${PRGNAME}
  rm -rf $PKG
fi
