#!/bin/bash

# Slackware build script for netexec

# Copyright 2026 danix <danix@danix.xyz>
# 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=netexec
VERSION=${VERSION:-1.5.1}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

# The upstream GitHub archive unpacks to a capitalized directory name.
SRCNAM=NetExec

# Name of the vendored wheels tarball (hosted by the maintainer).
WHEELS=${PRGNAM}-wheels-${VERSION}

# Install prefix for the self-contained virtualenv.
VENVDIR=/opt/$PRGNAM

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

# Several deps ship platform-specific compiled wheels (cryptography, pypykatz
# crypto). The vendored wheels tarball is x86_64-only; bail out clearly on
# other arches rather than failing deep in pip.
if [ "$ARCH" != "x86_64" ]; then
  echo "$PRGNAM is only supported on x86_64."
  exit 1
fi

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

PKGNAM=$PRGNAM # PRINT_PACKAGE_NAME
if [ "$PRINT_PACKAGE_NAME" ]; then
  echo "$PKGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.gz
cd $SRCNAM-$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 {} \+

# Unpack the vendored wheels. The tarball extracts to a top-level wheels/ dir.
rm -rf $TMP/wheels
tar xvf $CWD/$WHEELS.tar.gz -C $TMP

# Build the self-contained virtualenv at the install prefix and install
# netexec plus its full dependency tree from the local wheels only. No
# network access at build time.
python3 -m venv $PKG$VENVDIR
# Install every vendored wheel directly with --no-deps. netexec's metadata pins
# some deps (e.g. certipy-ad) as git+https URLs; letting pip resolve deps makes
# it ignore the local wheel and clone/build from git, which fails offline. The
# wheels tarball already contains the full closed dependency set.
$PKG$VENVDIR/bin/pip install --no-index --no-deps --find-links=$TMP/wheels \
  $TMP/wheels/*.whl

# Drop bytecode: it bakes in the build path and is non-reproducible; the
# interpreter regenerates it on first run.
find $PKG$VENVDIR -depth -type d -name __pycache__ -exec rm -rf {} +

# venvs are not relocatable: the absolute build path ($PKG$VENVDIR) is baked
# into bin/* script shebangs and pyvenv.cfg. Rewrite it to the installed
# prefix so the venv resolves at $VENVDIR after the package is installed.
# Only rewrite regular files (bin/ also holds symlinks like python3).
sed -i "s|$PKG$VENVDIR|$VENVDIR|g" $PKG$VENVDIR/pyvenv.cfg
find $PKG$VENVDIR/bin -type f -exec sed -i "s|$PKG$VENVDIR|$VENVDIR|g" {} +
# pip leaves the venv's own python as a symlink to the build host's python3;
# that resolves fine on the target since it points at the system interpreter.

# Drop pip's cached wheels and bytecode build noise from the package.
rm -rf $PKG$VENVDIR/share/python-wheels 2>/dev/null || true

# Strip compiled extension modules in the venv (cryptography et al.).
find $PKG$VENVDIR -name '*.so' -print0 \
  | xargs -0 strip --strip-unneeded 2> /dev/null || true

# Console-script symlinks into the default PATH.
mkdir -p $PKG/usr/bin
for bin in nxc netexec nxcdb; do
  ln -sf $VENVDIR/bin/$bin $PKG/usr/bin/$bin
done

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a LICENSE README.md \
  $PKG/usr/doc/$PRGNAM-$VERSION 2>/dev/null || true
cp -a $CWD/requirements.txt $PKG/usr/doc/$PRGNAM-$VERSION/requirements.txt
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
chmod 644 $PKG/usr/doc/$PRGNAM-$VERSION/*

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
