#!/bin/bash

# This is Gosuslugi plugin update script for Porteus
# Version 2026-05-11
#
# 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.

# root check
if [ `whoami` != "root" ]; then
    echo -e "\nYou need to be root to run this script.\n"
    exit 1
fi

PRGNAM=gosplugin
BUILD=1
ARCH=$( uname -m )
RPM_URL="https://gu-st.ru/content/Gosplugin/Gosplugin_Alt-RedOS_Installer.rpm.zip"
OUTPUT=${OUTPUT:-/tmp}

# Create a temporary directory and download the archive
TMPDIR=$(mktemp -d /tmp/gosplugin_XXXXXX)
wget -q --show-progress "$RPM_URL" -O "$TMPDIR/archive.zip" || exit 1
unzip -o "$TMPDIR/archive.zip" -d "$TMPDIR" > /dev/null 2>&1

# Find the .rpm.sh wrapper and extract the RPM from it
RPM_SH=$(find "$TMPDIR" -type f -name "*.rpm.sh" | head -1)
[ -z "$RPM_SH" ] && echo "ERROR: .rpm.sh not found" && exit 1

PAYLOAD_OFFSET=$(grep --text --line-number '^PAYLOAD:$' "$RPM_SH" | cut -d: -f1)
[ -z "$PAYLOAD_OFFSET" ] && echo "ERROR: PAYLOAD marker not found" && exit 1

tail -n +$((PAYLOAD_OFFSET + 1)) "$RPM_SH" | tar -xC "$TMPDIR"
RPM_FILE=$(find "$TMPDIR" -name "*.rpm" | head -1)
[ -z "$RPM_FILE" ] && echo "ERROR: RPM not found" && exit 1

VERSION=$(basename "$RPM_FILE" | sed -n 's/.*-\([0-9.]\+\)-[^-]*\.[^.]*\.rpm$/\1/p')
[ -z "$VERSION" ] && echo "ERROR: cannot extract version" && exit 1

echo "Building $PRGNAM version $VERSION for $ARCH..."

# Prepare the module root
PKG=/tmp/package-$PRGNAM
rm -rf "$PKG"
mkdir -p "$PKG"
( cd "$PKG"; rpm2cpio "$RPM_FILE" | cpio -idm > /dev/null 2>&1 )

# Remove unnecessary files
rm -rf "$PKG/opt/iitrust/gosuslugi_plugin/lib"{,64}"/cmake"
rm -rf "$PKG/opt/iitrust/gosuslugi_plugin/lib"{,64}"/pkgconfig"
rm -f  "$PKG/opt/iitrust/gosuslugi_plugin/lib"{,64}"/libxcb-image.so.0"*
rm -f  "$PKG/opt/iitrust/gosuslugi_plugin/lib"{,64}"/"*.a
rm -f  "$PKG/opt/iitrust/gosuslugi_plugin/lib"{,64}"/"*.la

# Create fake Slackware package info
PKGINFO="$PKG/var/lib/pkgtools/packages"
mkdir -p "$PKGINFO"
echo "PACKAGE NAME: $PRGNAM-$VERSION-$ARCH" > "$PKGINFO/$PRGNAM-$VERSION-$ARCH"

cat >> "$PKGINFO/$PRGNAM-$VERSION-$ARCH" << EOM
PACKAGE DESCRIPTION:
gosplugin: gosplugin (Gosuslugi crypto plugin)
gosplugin:
gosplugin: Plugin for electronic signature verification on Gosuslugi.ru
gosplugin:
gosplugin: https://www.gosuslugi.ru/landing/gosplugin
gosplugin:
FILE LIST:
EOM

find "$PKG" -not -path "*/var/*" | sed "s|^$PKG/||" >> "$PKGINFO/$PRGNAM-$VERSION-$ARCH"

# Build the .xzm module
MODULE="$OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD.xzm"
dir2xzm "$PKG" "$MODULE"

# Clean up temporary files
rm -rf "$TMPDIR" "$PKG"

# Final status
if [ -f "$MODULE" ]; then
    echo -e "\nYour $PRGNAM module is at: $MODULE"
    echo "Please copy it to your modules folder to survive a reboot."
else
    echo -e "\nFailed. Your $PRGNAM module is not built."
    exit 1
fi
