#!/usr/bin/env bash # This is NAPS2 update script for Porteus # with support "driverless" scanning # via eSCL (Apple AirScan) and WSD # Version 2026-12-04 # Copyright 2023-2030, Blaze, Dankov, Russia # 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. # Exit on undefined variables to catch typos set -u PRGNAM=${PRGNAM:-naps2} BUILD=${BUILD:-1} ARCH=$(uname -m) BOLD="\e[1m" CYAN="\e[96m" GREEN="\e[92m" RED="\e[31m" RESET="\e[0m" CWD=$(pwd) # Added $$ (PID) to avoid conflicts if multiple instances run WRKDIR=$(mktemp -d /tmp/portch-XXXXXX) PKG="$WRKDIR/package-$PRGNAM" PKGINFO="$PKG/var/lib/pkgtools/packages" OUTPUT=${OUTPUT:-/tmp} SLKCFLAGS="-O2 -fPIC" CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" SANAME=${SANAME:-sane-airscan} if [[ "$ARCH" != "x86_64" ]]; then echo -e "\n${RED}${BOLD}NAPS2 official releases are x86_64 only. This host is: ${ARCH}${RESET}\n" exit 1 fi # --- Robust Version Fetching via curl & GitHub API --- NAPS2_URL="https://github.com/cyanfish/naps2/releases/latest" VERSION=$(curl -sIL --max-time 30 --retry 3 "$NAPS2_URL" \ | grep -i "^location:" | grep -oE "tag/v[0-9.]+" | cut -d'/' -f2 | tr -d v) if [[ -z "$VERSION" ]]; then echo -e "\n${RED}Failed to fetch NAPS2 version. Check your internet connection.${RESET}\n" exit 1 fi SOURCE="https://github.com/cyanfish/naps2/releases/download/v${VERSION}/${PRGNAM}-${VERSION}-linux-x64.deb" AIRSCAN_URL="https://api.github.com/repos/alexpevzner/sane-airscan/tags" SAVER=$(curl -sS --max-time 30 --retry 3 "$AIRSCAN_URL" | grep -m 1 '"name":' | sed -E 's/.*"([^"]+)".*/\1/' | tr -d v) if [[ -z "$SAVER" ]]; then echo -e "\n${RED}Failed to fetch sane-airscan version.${RESET}\n" exit 1 fi # Use specific tag instead of master for reproducible builds SAMASTER="https://github.com/alexpevzner/sane-airscan/archive/refs/tags/${SAVER}.zip" # --- Cleanup Trap --- cleanup() { cd "$CWD" 2>/dev/null || true if [[ -d "$WRKDIR" ]]; then rm -rf "$WRKDIR" fi } trap cleanup EXIT # root check if [[ $(id -u) -ne 0 ]]; then echo -e "\n${RED}You need to be root to run this script.${RESET}\n" exit 1 fi if ! command -v ar &>/dev/null; then echo -e "\n${RED}Please activate 05-devel.xzm module and rerun this script.${RESET}\n" exit 1 fi # check if NAPS2 installed in the system MYVER="" if [[ -f "/usr/bin/naps2" ]]; then PKG_FILE=$(ls -1 /var/lib/pkgtools/packages/naps2-* 2>/dev/null | tail -n1) if [[ -n "$PKG_FILE" ]]; then MYVER=$(basename "$PKG_FILE" | cut -d'-' -f2) fi fi if [[ "$MYVER" == "$VERSION" ]]; then echo -e "You already have the latest ${GREEN}$MYVER${RESET} version of $PRGNAM." exit 0 else echo -e "Current version: ${CYAN}${MYVER:-Not installed}${RESET}. Latest available: ${GREEN}$VERSION${RESET}." read -p "$(echo -e "1) Would you like to build $PRGNAM ${GREEN}$VERSION${RESET} xzm module? [y/n] ")" -n 1 -r -s echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then exit 0 fi fi rm -rf "$PKG" mkdir -p "$WRKDIR" "$PKG" cd "$PKG" || exit 1 # add support "driverless" scanning via eSCL (Apple AirScan) and WSD read -p "$(echo -e "2) Would you like to add support for ${BOLD}\"driverless\"${RESET} scanning via eSCL (Apple AirScan) and WSD? [y/n] ")" -n 1 -r -s echo if [[ $REPLY =~ ^[Yy]$ ]]; then if ! command -v gcc &>/dev/null; then echo -e "\n${RED}Please activate 05-devel.xzm module and rerun this script.${RESET}\n" exit 1 fi if ! command -v unzip &>/dev/null; then echo -e "\n${RED}'unzip' is required to build sane-airscan but is not installed.${RESET}\n" exit 1 fi if ! XZM=no getpkg -q sane libieee1284; then echo -e "\n${RED}Failed to download sane or libieee1284.${RESET}\n" exit 1 fi for i in "$OUTPUT"/sane-*.txz "$OUTPUT"/libieee1284-*.txz; do [ -f "$i" ] || continue ROOT="$PKG" installpkg "$i" >/dev/null done if ! getmod -q universal-ctags; then echo -e "\n${RED}Failed to download universal-ctags modules.${RESET}\n" exit 1 fi activate "$OUTPUT"/universal-ctags-*.xzm -q > /dev/null # build xzm module of sane-airscan echo -e "\nCompiling ${BOLD}sane-airscan${RESET}" cd "$PKG" || exit 1 wget --show-progress --timeout=60 --tries=3 -O sane-airscan.zip "$SAMASTER" || { echo -e "\n${RED}${BOLD}Failed to download sane-airscan sources.${RESET}\n" exit 1 } unzip -q sane-airscan.zip -d "$PKG" && rm -f sane-airscan.zip || { echo -e "\n${RED}${BOLD}Failed to extract sane-airscan sources.${RESET}\n" exit 1 } # Find extracted directory dynamically (e.g. sane-airscan-0.9.29) AIRSCAN_DIR=$(find "$PKG" -maxdepth 1 -type d -name "sane-airscan-*" | head -n1) if [[ -z "$AIRSCAN_DIR" ]]; then echo -e "\n${RED}Failed to find extracted sane-airscan directory.${RESET}\n" exit 1 fi cd "$AIRSCAN_DIR" || exit 1 # fix bug with double // in the make file sed -i 's|$(DESTDIR)/|$(DESTDIR)|g' Makefile chown -R root:root . find -L . \ \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 750 -o -perm 555 \ -o -perm 511 \) -exec chmod 755 {} \; -o \ \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 640 -o -perm 444 \ -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \; # Use all CPU cores for faster compilation make -j"$(nproc)" || { echo -e "\n${RED}${BOLD}sane-airscan compilation failed.${RESET}\n" exit 1 } make install DESTDIR="$PKG" || { echo -e "\n${RED}${BOLD}sane-airscan install failed.${RESET}\n" exit 1 } cd "$PKG" || exit 1 rm -rf "$AIRSCAN_DIR" # Strip binaries safely find "$PKG" -type f -executable -exec file {} + | grep ELF | cut -d: -f1 | xargs -r strip --strip-unneeded 2> /dev/null ### fake Slackware type package info mkdir -p "$PKGINFO" echo "PACKAGE NAME: $SANAME-$SAVER-$ARCH" > "$PKGINFO/$SANAME-$SAVER-$ARCH" cat >> "$PKGINFO/$SANAME-$SAVER-$ARCH" << EOM PACKAGE DESCRIPTION: sane-airscan: sane-airscan (Scanner Access Now Easy) sane-airscan: sane-airscan: Scanner Access Now Easy - universal driver for sane-airscan: eSCL (Apple AirScan) and WSD sane-airscan: sane-airscan: sane-airscan: sane-airscan: Homepage: https://github.com/alexpevzner/sane-airscan sane-airscan: sane-airscan: sane-airscan: FILE LIST: EOM # add missing .so symlink (check both lib64 and lib) if [[ -d "$PKG/usr/lib64/sane" ]]; then cd "$PKG/usr/lib64/sane" || exit 1 ln -sf libsane-airscan.so.1 libsane-airscan.so cd "$PKG" || exit 1 elif [[ -d "$PKG/usr/lib/sane" ]]; then cd "$PKG/usr/lib/sane" || exit 1 ln -sf libsane-airscan.so.1 libsane-airscan.so cd "$PKG" || exit 1 fi # activate "airscan" backend for compatible with AirSane DLLCONF="$PKG/etc/sane.d/dll.conf" if [[ -f "$DLLCONF" ]]; then sed -i 's|^escl|#escl|' "$DLLCONF" grep -q '^airscan' "$DLLCONF" || echo airscan >> "$DLLCONF" else echo -e "${RED}Warning: $DLLCONF not found; airscan backend not registered.${RESET}" fi deactivate "$OUTPUT"/universal-ctags-*.xzm -q > /dev/null rm -f "$OUTPUT"/universal-ctags-*.xzm cd "$PKG" || exit 1 else # no add AirScan support if ! XZM=no getpkg -q sane libieee1284; then echo -e "\n${RED}Failed to download sane or libieee1284.${RESET}\n" exit 1 fi for i in "$OUTPUT"/sane-*.txz "$OUTPUT"/libieee1284-*.txz; do [ -f "$i" ] || continue ROOT="$PKG" installpkg "$i" >/dev/null done rm -f "$OUTPUT"/sane-*.txz "$OUTPUT"/libieee1284-*.txz fi # download NAPS2 echo -e "\nDownloading ${BOLD}${PRGNAM}-${VERSION}${RESET}" wget --show-progress --timeout=60 --tries=3 "$SOURCE" || { echo -e "\n${RED}${BOLD}Download seems to have failed, Exiting...${RESET}" exit 1 } if [[ ! -f "${PRGNAM}-${VERSION}-linux-x64.deb" ]]; then echo -e "\n${RED}${BOLD}Download seems to have failed, Exiting...${RESET}" exit 1 fi # Universal unpack for .deb (handles tar.xz, tar.gz, and modern tar.zst) ar x "${PRGNAM}-${VERSION}-linux-x64.deb" DATA_ARCHIVE=$(ls data.tar.* 2>/dev/null | head -n1) if [[ -z "$DATA_ARCHIVE" ]]; then echo -e "\n${RED}Failed to find data archive in deb package.${RESET}" exit 1 fi case "$DATA_ARCHIVE" in *.tar.xz) tar -xJf "$DATA_ARCHIVE" ;; *.tar.gz) tar -xzf "$DATA_ARCHIVE" ;; *.tar.zst) if command -v zstd &>/dev/null; then tar --zstd -xf "$DATA_ARCHIVE" else echo -e "\n${RED}Zstandard archive detected but 'zstd' is not installed.${RESET}" exit 1 fi ;; *) tar -xf "$DATA_ARCHIVE" ;; esac || { echo -e "\n${RED}${BOLD}Failed to extract NAPS2 data archive.${RESET}" exit 1 } # cleanup deb parts rm -f "$DATA_ARCHIVE" control.tar.* debian-binary "${PRGNAM}-${VERSION}-linux-x64.deb" # cleanup unnecessary files rm -rf "$PKG/usr/"{doc,man,include,info,src} rm -rf "$PKG/usr/lib64/pkgconfig" rm -f "$PKG/usr/lib/naps2/"*.txt rm -rf "$PKG/usr/share/"{doc,gir-1.0,gtk-doc,help,licenses,locale,man,pkgconfig} rm -rf "$PKG/var/lib/pkgtools/douninst.sh" rm -f "$PKG/var/log/scripts/"* find "$PKG" -type f -name "*.a" -delete find "$PKG" -type f -name "*.la" -delete # remove MIME types and add Russian translation DESKTOP="$PKG/usr/share/applications/naps2.desktop" if [[ -f "$DESKTOP" ]]; then sed -i '/^MimeType=/d' "$DESKTOP" sed -i '/^Name=/a Name[ru]=Сканер документов NAPS2' "$DESKTOP" else echo -e "${RED}Warning: naps2.desktop not found in $PKG/usr/share/applications/${RESET}" fi ### fake Slackware type package info mkdir -p "$PKGINFO" echo "PACKAGE NAME: $PRGNAM-$VERSION-$ARCH" > "$PKGINFO/$PRGNAM-$VERSION-$ARCH" cat >> "$PKGINFO/$PRGNAM-$VERSION-$ARCH" << EOM PACKAGE DESCRIPTION: NAPS2: NAPS2 - Not Another PDF Scanner. NAPS2: NAPS2: Scan documents to PDF and more, as simply as possible. NAPS2: NAPS2: https://www.naps2.com NAPS2: FILE LIST: EOM # Robust file list generation (excludes /var and strips leading ./) find . -mindepth 1 | grep -v '^\./var' | sed 's|^\./||' >> "$PKGINFO/$PRGNAM-$VERSION-$ARCH" cd "$CWD" || exit 1 dir2xzm "$PKG" "$OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD.xzm" # check on NAPS2 xzm file exists if [[ -f "$OUTPUT/${PRGNAM}-${VERSION}-${ARCH}-${BUILD}.xzm" ]]; then echo -e "\n${BOLD}Your $PRGNAM module is at: ${GREEN}${BOLD}$OUTPUT/$PRGNAM-$VERSION-${ARCH}-${BUILD}.xzm${RESET}\n${BOLD}Please copy it to your modules folder to survive a reboot.${RESET}" else echo -e "\n${RED}${BOLD}Failed. Your $PRGNAM module is not built.${RESET}\n" fi