#!/bin/sh
#
# libreoffice-helppack.SlackBuild
# 2024-05-10
#
# LibreOffice Help Pack

PRGNAM=libreoffice-helppack
LOLANG=${LOLANG:-de}
VERSION=7.6.7

# automatically determine the architecture
if [ -z "$ARCH" ]; then
  case "$(uname -m)" in
    x86_64) ARCH=x86_64 ;;
       *) exit 1 ;;
  esac
fi

BUILD=${BUILD:-1}
TAG=${TAG:-scr}
PKGTYPE=${PKGTYPE:-txz}

HOMEPAGE="https://www.libreoffice.org/"
SOURCE="LibreOffice_${VERSION}_Linux_x86-64_rpm_helppack_${LOLANG}.tar.gz"
DOWNLOAD="https://downloadarchive.documentfoundation.org/libreoffice/old/${VERSION}.2/rpm/x86_64/LibreOffice_${VERSION}.2_Linux_x86-64_rpm_helppack_${LOLANG}.tar.gz"
MD5SUM="3b22af12ee3d0814736377ac0433cccd"

REQUIRES="libreoffice"

MAINTAINER="Gernot Walzl"
EMAIL="gernot.walzl@gmx.at"

DESC="LibreOffice Help Pack

${HOMEPAGE}"

DOCS="$(pwd)/$0"

# EOF info


# set initial variables
CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# exit on error
set -e

# download the source if needed
if [ ! -f $CWD/$SOURCE ]; then
  wget -O $CWD/$SOURCE $DOWNLOAD || exit 1
fi

# check for correct source file
if [ "$(md5sum $CWD/$SOURCE | cut -f 1 -d ' ')" != "$MD5SUM" ]; then
  exit 1
fi

# extract the application source
cd $TMP || exit 1
tar xvf $CWD/$SOURCE || exit 1

# change to the application source directory
APPDIR="LibreOffice_${VERSION}.?_Linux_x86-64_rpm_helppack_${LOLANG}"
cd $APPDIR || exit 1

# extract rpms
rm -rf $PKG
mkdir -p $PKG/install

cd $PKG
for RPM in $(ls $TMP/$APPDIR/RPMS/*.rpm); do
  rpm2cpio $RPM | cpio -imdv
done

# add documentation
cd $TMP/$APPDIR
if [ "$DOCS" != "" ]; then
  mkdir -p $PKG/usr/doc/$PRGNAM-$LOLANG-$VERSION
  cp -a $DOCS $PKG/usr/doc/$PRGNAM-$LOLANG-$VERSION
  find $PKG/usr/doc/$PRGNAM-$LOLANG-$VERSION -type f -exec chmod 644 {} \;
fi

# add some description to the package
if [ "$DESC" != "" ]; then
  echo "$DESC" | sed 's/^/'$PRGNAM-$LOLANG': /g' > $PKG/install/slack-desc
fi

# append dependency information to the package
if [ "$REQUIRES" != "" ]; then
  for REQ in $REQUIRES; do
    echo "$REQ" >> $PKG/install/slack-required
  done
fi

# fix permissions
cd $PKG
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# build the package
makepkg -l y -c n \
  $OUTPUT/$PRGNAM-$LOLANG-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE \
  || exit 1

# make everything clean
cd $CWD
rm -rf $PKG
rm -rf $TMP/$APPDIR
