#!/bin/bash

# Slackware build script for spyder

# Copyright 2010-2016 Larry Hajali <larryhaja[at]gmail[dot]com>
# Copyright 2024-2025 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=spyder
VERSION=${VERSION:-5.5.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

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

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
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
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 {} \;

# Sphinx craps out when files are in Windows format.  We need to
# change doc/conf.py to Unix format.  Simple hack! :/
for FILE in $(find . -type f \( ! -iname "*\.*o" ! -iname "*\.png" \) \
  | xargs grep -Pl '\r$'); do
  sed -e 's/\r//g' $FILE > ${FILE}.new
  touch -r $FILE ${FILE}.new
  mv ${FILE}.new $FILE
done

# Allow SlackBuilds python libraries versions
sed "s|PYLINT_REQVER = '>=2.5.0,<3.1'|PYLINT_REQVER = '>=2.5.0'|" -i spyder/dependencies.py
sed "s|QTCONSOLE_REQVER = '>=5.5.0,<5.6.0'|QTCONSOLE_REQVER = '>=5.5.0'|" -i spyder/dependencies.py
sed "s|pylint>=2.5.0,<3.1|pylint>=2.5.0|" -i setup.py
sed "s|qtconsole>=5.5.0,<5.6.0|qtconsole>=5.5.0|" -i setup.py

# Replace pkg_resources dependency with python3-importlib-metadata
# Patch backported from a pull request included in Spyder 5.5.6
# https://github.com/spyder-ide/spyder/pull/22244 
patch -p1 -b < $CWD/replace-pkg_resources.patch

python3 setup.py install --root=$PKG

# If Python version is 3.9 (i.e. Slackware 15 is installed):
# Then look for additional Python modules at /opt/python3.9/site-packages
# This is for fixing an incorrect claim of missing setuptools
PYVER=$(python3 -c 'import sys; print("%d.%d" % sys.version_info[:2])')
[[ $PYVER == 3.9 ]] && sed -i "/import sys/a\
sys.path.insert(0, '/opt/python$PYVER/site-packages')" $PKG/usr/bin/spyder

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

for i in 16 24 32 36 48 64 96 128; do
  convert spyder/images/$PRGNAM.svg -resize ${i}x${i}! $PRGNAM-$i.png
  install -D -m 0644 $PRGNAM-$i.png $PKG/usr/share/icons/hicolor/${i}x${i}/apps/$PRGNAM.png
done
install -D -m 0644 spyder/images/$PRGNAM.svg \
  $PKG/usr/share/icons/hicolor/scalable/apps/$PRGNAM.svg

# Remove unwanted files/directories.
rm -rf $PKG/usr/share/pixmaps $PKG/usr/bin/spyder_win_post_install.py

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS.txt LICENSE.txt NOTICE.txt *.md $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
