#!/bin/bash
# CUSTOM_BUILD_HARDENED
export CFLAGS="${CFLAGS:--O2} -fhardened -fPIC"
export CXXFLAGS="${CXXFLAGS:--O2} -fhardened -fPIC"
export LDFLAGS="${LDFLAGS:-} -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,-z,ibt -Wl,-z,shstk"

# AMOS build script for vanguards
# Tor controller addon that defends against guard discovery attacks.
#
# Following upstream's advice, this package does *not* use setup.py/pip.
# The pristine source tree is installed to /usr/share/vanguards and a
# small wrapper in /usr/local/bin runs ./src/vanguards.py as the tor user.

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

PRGNAM=vanguards
VERSION=${VERSION:-0.3.1}
BUILD=${BUILD:-1}
PKGTYPE=${PKGTYPE:-txz}

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.$PKGTYPE"
  exit 0
fi

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

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM
tar xvf $CWD/$PRGNAM.tar.gz

# Normalize the extracted top-level directory name.
if [ ! -d "$PRGNAM" ]; then
  SRCDIR=$(tar tzf $CWD/$PRGNAM.tar.gz | head -1 | cut -f1 -d"/")
  mv "$SRCDIR" "$PRGNAM"
fi
cd $PRGNAM

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

# --- Python 3.12 compatibility patch ---------------------------------------
# SafeConfigParser was removed in Python 3.12 (renamed to ConfigParser).
if [ -f src/vanguards/config.py ]; then
  sed -i 's/\bSafeConfigParser\b/ConfigParser/g' src/vanguards/config.py
fi

# --- Install the pristine source tree (no setup.py, no pip) ----------------
mkdir -p $PKG/usr/share/$PRGNAM
cp -a src $PKG/usr/share/$PRGNAM/
# Also ship any bundled docs we might want at runtime.
[ -f vanguards.conf.sample ] && \
  cp -a vanguards.conf.sample $PKG/usr/share/$PRGNAM/

# Strip nothing; there is nothing compiled. Ensure perms are sane.
find $PKG/usr/share/$PRGNAM -type d -exec chmod 755 {} \;
find $PKG/usr/share/$PRGNAM -type f -exec chmod 644 {} \;
chmod 755 $PKG/usr/share/$PRGNAM/src/vanguards.py

# --- Wrapper ---------------------------------------------------------------
mkdir -p $PKG/usr/local/bin
cat > $PKG/usr/local/bin/$PRGNAM <<'EOF'
#!/bin/sh
# Wrapper for vanguards.
#
# Upstream ships vanguards as ./src/vanguards.py, intended to be run
# directly (no setup.py / pip install). We keep that model: the source
# tree lives in /usr/share/vanguards and we simply exec the entry point.
#
# vanguards must run as the tor user so it can access Tor's control
# socket and write its state file. The init script /etc/rc.d/rc.vanguards
# handles that; for manual invocations use:
#     su -s /bin/sh -c '/usr/local/bin/vanguards ...' tor
#
# PYTHONDONTWRITEBYTECODE avoids __pycache__ writes into the
# root-owned source tree.
PYTHONDONTWRITEBYTECODE=1
export PYTHONDONTWRITEBYTECODE
exec python3 /usr/share/vanguards/src/vanguards.py "$@"
EOF
chmod 755 $PKG/usr/local/bin/$PRGNAM

# --- Init script -----------------------------------------------------------
mkdir -p $PKG/etc/rc.d
install -m 0755 $CWD/rc.vanguards $PKG/etc/rc.d/rc.vanguards

# --- Default configuration -------------------------------------------------
mkdir -p $PKG/etc/vanguards
install -m 0644 $CWD/vanguards.conf $PKG/etc/vanguards/vanguards.conf.new

# --- Runtime / log directories --------------------------------------------
mkdir -p $PKG/var/run/vanguards $PKG/var/log/vanguards \
         $PKG/var/lib/vanguards
chmod 0750 $PKG/var/run/vanguards $PKG/var/log/vanguards \
           $PKG/var/lib/vanguards

# --- Docs ------------------------------------------------------------------
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
for f in README.md README CHANGELOG.md CHANGELOG LICENSE COPYING \
         vanguards.conf.sample; do
  [ -f "$f" ] && cp -a "$f" $PKG/usr/doc/$PRGNAM-$VERSION/
done
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

# --- Install metadata ------------------------------------------------------
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.$PKGTYPE
