#!/bin/bash

# AMOS build script for terminal-svg

# Copyright 2026 ANNE Media
# 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=terminal-svg
VERSION=${VERSION:-0.6.0}
BUILD=${BUILD:-1}
PKGTYPE=${PKGTYPE:-txz}

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

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

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

if [ "$ARCH" = "i686" ]; then
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  LIBDIRSUFFIX="64"
else
  LIBDIRSUFFIX=""
fi

# ---------------------------------------------------------------------------
# HARDENING FLAGS
#
# The Rust compiler does not consume CFLAGS/CXXFLAGS. Hardening is passed
# through RUSTFLAGS, which rustc reads directly.
#
#   -C relocation-model=pic
#     PIC objects can be linked into both executables and shared libraries.
#     This binary is an executable, but pic is the safe default for a
#     standalone Rust tool and produces the same mitigation set as pie
#     for a single-crate binary.
#
#   -C link-arg=-Wl,-z,relro,-z,now
#     Full RELRO: BIND_NOW, no lazy binding.
#
#   -C link-arg=-Wl,-z,noexecstack
#     Non-executable stack.
#
#   -C link-arg=-Wl,-z,force-ibt -C link-arg=-Wl,-z,shstk
#     CET: force the IBT and SHSTK properties into the output. rustc
#     uses rust-lld as the default linker on x86_64-unknown-linux-gnu,
#     and rust-lld does NOT recognise the GNU ld spelling '-z ibt'. It
#     requires '-z force-ibt'. Passing '-z ibt' to rust-lld produces
#     "unknown -z value: ibt" and silently drops the property, leaving
#     the binary without IBT.
#
#   -C force-frame-pointers=yes
#     Frame pointers for better debugging.
#
# Stack canaries and FORTIFY are not available on stable Rust. See the
# AUDIT.TXT entry for this package for the documented exceptions.
# ---------------------------------------------------------------------------
export RUSTFLAGS="-C relocation-model=pic \
  -C link-arg=-Wl,-z,relro,-z,now \
  -C link-arg=-Wl,-z,noexecstack \
  -C link-arg=-Wl,-z,force-ibt \
  -C link-arg=-Wl,-z,shstk \
  -C force-frame-pointers=yes"

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 {} \;

# Build the release binary with a fixed job count. Cargo fetches
# dependencies from crates.io unless a vendor directory or a
# Cargo.lock with a local registry is present. The upstream project
# is a single binary with no build.rs.
cargo build --release --locked --jobs 6

# Verify the binary carries the hardening flags before packaging.
echo "--- hardening check on target/release/$PRGNAM ---"
BIN="target/release/$PRGNAM"
if [ ! -f "$BIN" ]; then
  echo "FAIL: $BIN not found"
  exit 1
fi
readelf -d "$BIN" | grep -q BIND_NOW \
  || { echo "FAIL: $BIN has no BIND_NOW (no full RELRO)"; exit 1; }
readelf -lW "$BIN" | grep -q GNU_RELRO \
  || { echo "FAIL: $BIN has no GNU_RELRO"; exit 1; }
readelf -lW "$BIN" | grep GNU_STACK | grep -q ' RWE ' \
  && { echo "FAIL: $BIN has an executable stack"; exit 1; } || true
readelf -nW "$BIN" | grep -q IBT \
  || { echo "FAIL: $BIN has no IBT property"; exit 1; }
readelf -nW "$BIN" | grep -q SHSTK \
  || { echo "FAIL: $BIN has no SHSTK property"; exit 1; }
echo "--- hardening check passed ---"

# Install
mkdir -p $PKG/usr/bin
install -m 755 "$BIN" $PKG/usr/bin/

# Documentation
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a README.md LICENSE $PKG/usr/doc/$PRGNAM-$VERSION 2>/dev/null || true
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.txz
