#!/bin/sh
#
# mozilla-thunderbird-dictionary.SlackBuild
# 2024-05-05
#
# Mozilla Thunderbird Dictionary

PRGNAM=mozilla-thunderbird-dictionary
LOLANG=de-AT
VERSION=2.1

# automatically determine the architecture
if [ -z "$ARCH" ]; then
  case "$(uname -m)" in
    i?86) ARCH=i686 ;;
       *) ARCH=$(uname -m) ;;
  esac
fi

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

HOMEPAGE="https://addons.thunderbird.net/"
SOURCE="german_dictionary_extended_for_austria-${VERSION}.xpi"
DOWNLOAD="${HOMEPAGE}user-media/addons/_attachments/3079/${SOURCE}"
MD5SUM="4a004d452170bc1a7f9da19c8d6d76f8"

REQUIRES="mozilla-thunderbird"

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

DESC="Mozilla Thunderbird Dictionary

${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

# set options
case "$ARCH" in
  i?86) LIBDIRSUFFIX="" ;;
  x86_64) LIBDIRSUFFIX="64" ;;
esac

# install everything into the package directory
rm -rf $PKG
mkdir -p $PKG/install

install -D -m 0644 $CWD/$SOURCE \
  $PKG/usr/lib$LIBDIRSUFFIX/thunderbird/distribution/extensions/$LOLANG@dictionaries.addons.mozilla.org.xpi

# add documentation
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
cd $PKG
PKGVERSION=$(echo $VERSION | sed 's/-/./g')
makepkg -l y -c n \
  $OUTPUT/$PRGNAM-$LOLANG-$PKGVERSION-$ARCH-$BUILD$TAG.$PKGTYPE \
  || exit 1

# make everything clean
cd $CWD
rm -rf $PKG
