#!/bin/bash

# Slackware build script for seamonkey 

# Copyright 2026 Frank Honolka <info@snukware.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=seamonkey			
VERSION=${VERSION:-2.53.24}	
BUILD=${BUILD:-1}
TAG=${TAG:-_snuk}		
PKGTYPE=${PKGTYPE:-txz}
PYTHONSRC=Python-3.11.10
PYTHONINST=pythoninstall

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
       *) ARCH=$( uname -m ) ;;
  esac
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-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/var/cache/snukware/tmp}	
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/var/cache/snukware/xap}	

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
elif [ "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e 


# Google API keys (see http://www.chromium.org/developers/how-tos/api-keys)
# Note: These are for Arch Linux use ONLY. For your own distribution, please
# get your own set of keys. Feel free to contact foutrelis@archlinux.org for
# more information.
_google_api_key=AIzaSyDwr302FpOSkGRpLlUpPThNTDPbXcIn_FM

# Mozilla API keys (see https://location.services.mozilla.com/api)
# Note: These are for Arch Linux use ONLY. For your own distribution, please
# get your own set of keys. Feel free to contact heftig@archlinux.org for
# more information.
_mozilla_api_key=e05d56db0a694edc8b5aaebda3f2db6a

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


# Seamonkey does not build with 1.95
RUSTROOT=/tmp/rust-env

if [ -d "$RUSTROOT" ]; then
	rm -rf "$RUSTROOT"
fi

export RUSTUP_HOME=$RUSTROOT/rustup
export CARGO_HOME=$RUSTROOT/cargo
export PATH=$CARGO_HOME/bin:$PATH

if [ ! -x "$CARGO_HOME/bin/rustup" ]; then
  mkdir -p "$RUSTROOT"

  curl https://sh.rustup.rs -sSf | sh -s -- -y \
    --profile minimal \
    --default-toolchain 1.77.2
fi

# Test not to write in $HOME/bashrc and $HOME/bash_profile
#if [ ! -x "$CARGO_HOME/bin/rustup" ]; then
#  mkdir -p "$RUSTROOT"
#
#  curl https://sh.rustup.rs -sSf | sh -s -- -y \
#  --profile minimal \
#  --default-toolchain 1.77.2 \
#  --no-modify-path
#fi

rustup default 1.77.2

rm -rf $PRGNAM-$VERSION $PYTHONSRC $PYTHONINST 

tar xvf $CWD/$PRGNAM-$VERSION.source.tar.?z
cd $PRGNAM-$VERSION
cp "$CWD/mozconfig" .mozconfig

echo -n "$_google_api_key" > google-api-key
echo -n "$_mozilla_api_key" > mozilla-api-key

{
  echo "ac_add_options --with-google-location-service-api-keyfile=${PWD@Q}/google-api-key"
  echo "ac_add_options --with-google-safebrowsing-api-keyfile=${PWD@Q}/google-api-key"
  echo "ac_add_options --with-mozilla-api-keyfile=${PWD@Q}/mozilla-api-key"
} >> .mozconfig || exit 1

cd $TMP
tar xvf $CWD/$PYTHONSRC.tar.xz || exit 1
cd $PYTHONSRC || exit 1
./configure 
make 
make DESTDIR="$TMP/$PYTHONINST" install || exit 1
cd ..
PATH="$PWD/pythoninstall/usr/local/bin:$PATH"

cd $PRGNAM-$VERSION

# Don't use mold - fails.
LDFLAGS=$(printf '%s' "$LDFLAGS" | sed 's/-fuse-ld=[^[:space:]]*//')
export LDFLAGS
export MACH_USE_SYSTEM_PYTHON=1
export MOZBUILD_STATE_PATH="$TMP/mozbuild"
# Fix build on non utf-8 charsets
LC_ALL=C.UTF-8 \
./mach build

# shellcheck disable=SC1007
LC_ALL=C.UTF-8 DESTDIR="$PKG" INSTALL_SDK= ./mach install

install -Dm644 comm/suite/branding/seamonkey/default128.png \
  "$PKG/usr/share/pixmaps/seamonkey.png"

install -Dm644 toolkit/mozapps/installer/linux/rpm/mozilla.desktop \
    "$PKG/usr/share/applications/seamonkey.desktop"
  sed -i 's/@MOZ_APP_DISPLAYNAME@/SeaMonkey internet suite/' \
    "$PKG/usr/share/applications/seamonkey.desktop"
  sed -i 's/@MOZ_APP_NAME@/seamonkey/' "$PKG/usr/share/applications/seamonkey.desktop"

find $PKG -name perllocal.pod \
  -o -name ".packlist" \
  -o -name "*.bs" \
  | xargs rm -f

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
DOCS="AUTHORS CLOBBER README.txt"
cp -a $DOCS $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

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