#!/bin/bash

# Slackware build script for OpenOffice Language Packs
#
# Adapted from Slackware build scripts for LibreOffice Language Packs by
# Niels Horn, Rio de Janeiro, RJ, Brazil (Copyright 2010-2014) and
# Willy Sudiarto Raharjo (Copyright 2014-2017)

# Copyright 2017 Ekin Akoglu, Erdemli, Mersin, Turkey
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the “Software”),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=openoffice-langpack
VERSION=${VERSION:-"4.1.6"}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

OOLANG=${OOLANG:-en-GB}
PKG_LANG=${OOLANG//-/_}

# I'll leave this line here, as I always have to change it with every release
SRCVERSION=$VERSION
SRCSHORT=$(echo $VERSION | cut -f1 -d.)

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

# Change source package name
if [ "$ARCH" = "x86_64" ]; then
  SRCARCH="x86-64"
  PKGARCH="$ARCH"
  LIBDIRSUFFIX=64
elif [ "$ARCH" = "arm" ]; then
  printf "\n$ARCH is unsupported for Apache OpenOffice...\n\n"
  exit 1
else
  SRCARCH="x86"
  PKGARCH="i586"
  LIBDIRSUFFIX=""
fi

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-${VERSION}_${PKG_LANG}-$PKGARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

set -e

# Concatenate all parts to get the full name of the tarball
TARBALL="Apache_OpenOffice_${SRCVERSION}_Linux_${SRCARCH}_langpack-rpm_${OOLANG}.tar.gz"
if [ ! -r $TARBALL ]; then
  echo "$TARBALL not found"
  exit 1
fi

# Get "base" directory of tarball
SOURCEDIR=$(tar tzf $CWD/$TARBALL 2>/dev/null | head -n 1 | tr -d \/)

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
rm -rf $TMP/$SOURCEDIR

tar xvf $CWD/$TARBALL -C $TMP

cd $TMP/$SOURCEDIR/RPMS/

for FILE in *.rpm ; do rpm2cpio < $FILE | cpio -imdv ; done
mv opt $PKG
cd $PKG

# Move docs to their expected locations
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cd $PKG/opt/openoffice$SRCSHORT
  mv readmes $PKG/usr/doc/$PRGNAM-$VERSION
cd -
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Fix ownership and permissions and make the package
chown -R root:root .
find . -type d -exec chmod 755 {} \;
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-${VERSION}_${PKG_LANG}-$PKGARCH-$BUILD$TAG.$PKGTYPE
