#!/bin/bash

# Slackware build script for jitsi-meet-desktop

# Copyright 2022-2023 Isaac Yu <isaacyu@protonmail.com>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS 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 AUTHOR 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.

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

PRGNAM=jitsi-meet-desktop
VERSION=${VERSION:-2025.10.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  ARCH=$(uname -m)
fi

case "$ARCH" in
  # do nothing and pass through
  x86_64) ;;
  *)
    echo "$ARCH is not supported for $PRGNAM-$VERSION"
    exit 1
    ;;
esac

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

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

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP

# Extract AppImage at the new jitsi-meet-desktop folder
rm -rf jitsi-meet-desktop
mkdir jitsi-meet-desktop
cp $CWD/jitsi-meet-$ARCH.AppImage jitsi-meet-desktop
cd jitsi-meet-desktop
chmod u+x jitsi-meet-$ARCH.AppImage
./jitsi-meet-$ARCH.AppImage --appimage-extract
rm jitsi-meet-$ARCH.AppImage    # Once AppImage is extracted, remove it from /tmp/SBo/jitsi-meet-desktop

find squashfs-root/locales/ -type d -exec chmod 755 {} +
find squashfs-root/resources/ -type d -exec chmod 755 {} +

install -d $PKG/opt/$PRGNAM
cp -r squashfs-root/* $PKG/opt/$PRGNAM

# remove broken or unused files and directories
rm -r $PKG/opt/$PRGNAM/usr/
rm $PKG/opt/$PRGNAM/AppRun
rm $PKG/opt/$PRGNAM/jitsi-meet.desktop
rm $PKG/opt/$PRGNAM/jitsi-meet.png

find squashfs-root/usr/share/icons/ -type d -exec chmod 755 {} +

install -d $PKG/usr/share/icons
cp -r squashfs-root/usr/share/icons/hicolor $PKG/usr/share/icons/hicolor

install -d $PKG/usr/bin
ln -s ../../opt/$PRGNAM/jitsi-meet $PKG/usr/bin/$PRGNAM

# Install custom desktop file
install -Dm644 "$CWD/$PRGNAM.desktop" $PKG/usr/share/applications/$PRGNAM.desktop

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a squashfs-root/LICENSE.electron.txt $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

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

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
