#!/bin/bash

# Slackware build script for timetrap

# Copyright 2013-2022 Jostein Berntsen <jvbernts@online.no>
# 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.

# 20220303 bkw: Modified by SlackBuilds.org, BUILD=2:
# - fix REQUIRES: sequel, not sequel4.
# - get rid of hardcoded dependency versions in the gemspec.
# - remove redundant t and dev_t scripts from /usr/bin.

# 20220520 bkw: Modified by Jostein Berntsen, BUILD=3:
# - fix: Update removal line so the executable t is retained
# - Added variable to check for Ruby version and sed line to ensure compatibility 
#   for newer Ruby versions than 3.0.0 

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

PRGNAM=timetrap
VERSION=${VERSION:-1.15.2}
BUILD=${BUILD:-3}
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}

# 20220303 bkw: no SLKCFLAGS needed: there's no compiled C/C++ code.
# No LIBDIRSUFFIX needed, 'gem install' knows the right path to use.
# The only reason this isn't noarch is that the install path includes
# lib or lib64 (like pure python modules do).

set -e

rm -rf $PKG
mkdir -p $TMP/$PRGNAM-$VERSION $PKG $OUTPUT
cd $TMP/$PRGNAM-$VERSION

DESTDIR=$( ruby -r rbconfig -e '
include RbConfig
printf("%s/%s/gems/%s\n",
	CONFIG["libdir"],
	CONFIG["RUBY_INSTALL_NAME"],
	CONFIG["ruby_version"]
      )
')

gem specification $CWD/$PRGNAM-$VERSION.gem | \
	ruby -r yaml -r rbconfig -e '
c = RbConfig::CONFIG
path = sprintf("%s/%s/gems/%s",
        c["libdir"],
        c["RUBY_INSTALL_NAME"],
        c["ruby_version"])
sys_gemspecs = Dir.glob(path + "/specifications/**/*.gemspec").map {|g| gs = Gem::Specification.load(g); gs.name }
obj = Gem::Specification.from_yaml($stdin)
obj.dependencies.each {|dep|
        if not(dep.type == :runtime)
                next
        end
        if not(sys_gemspecs.include?(dep.name))
                $stderr.write("WARNING: #{dep.name} gem not found\n")
                sleep 0.5
        end

}'

gem install \
	--local \
	--no-update-sources \
	--ignore-dependencies \
	--backtrace \
	--install-dir $PKG/$DESTDIR \
	--bindir $PKG/usr/bin \
	$CWD/$PRGNAM-$VERSION.gem

# 20220303 bkw: don't need these extra almost-identical scripts in /usr/bin:
rm -f $PKG/usr/bin/{dev_t}

# 20220303 bkw: the version numbers of the deps are hardcoded in the
# .gemspec. Any time someone updates one of them, it would break
# timetrap. So get rid of the version numbers. This may lead to
# breakage at some point, but right now it fixes timetrap complaining
# that it can't find its sequel dep at runtime (it wants 5.30.0,
# we actually have 5.42.0, and it seems to work fine). This diff is
# unusual as it patches $PKG after the build, because picking apart
# the .gem file, patching it, and reassembling it (and fixing the
# checksum) is more work than I care to do.

# Note that the patch has hardcoded lib64 paths, so we sed it.
cd $PKG
cp $CWD/remove_hardcoded_versions.diff tmp.diff
[ "$ARCH" = "x86_64" ] || sed -i 's,usr/lib64,usr/lib,g' tmp.diff
RUBYVERSION=$(ls /usr/lib*/ruby/gems)
sed -i "s/gems\/3\.[0-9]\.[0-9]\//gems\/$RUBYVERSION\//g" *.diff
patch -p1 < tmp.diff
rm -f tmp.diff

mkdir -p $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
