#!/bin/bash

# This is Chromium with Russian GOST cryptographic algorithms support
# build script of xzm module for Porteus
# Version 2026-07-21

# 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.

set -euo pipefail
trap 'echo -e "\n${RED}${BOLD}Script interrupted. Cleaning up...${RESET}"; cleanup' INT TERM

# Root check
if [ "$(whoami)" != "root" ]; then
    echo -e "\nOnly root can run this.\n"
    exit 1
fi

PRGNAM=${PRGNAM:-chromium-gost}
VERSION=$(lynx -head -dump "https://github.com/deemru/chromium-gost/releases/latest" | grep Location | grep -o "[0-9].*$")
SOURCE="https://github.com/deemru/Chromium-Gost/releases/download/${VERSION}/${PRGNAM}-${VERSION}-linux-amd64.rpm"
BOLD=${BOLD:-"\e[1m"}
CYAN=${CYAN:-"\e[96m"}
GREEN=${GREEN:-"\e[92m"}
RED=${RED:-"\e[31m"}
RESET=${RESET:-"\e[0m"}
CWD=$(pwd)
TMPDIR=/tmp/portch
PKG=$TMPDIR/package-$PRGNAM
PKGINFO=$PKG/var/lib/pkgtools/packages
OUTPUT=${OUTPUT:-/tmp}
ARCH=$( uname -m )

cleanup(){
    echo -e "\n${BOLD}Cleaning up temporary files...${RESET}"
    [ -d "$TMPDIR" ] && rm -rf "$TMPDIR"
    [ -d "$PKG" ] && rm -rf "$PKG"
    exit
}

# Check if Chromium-Gost installed in the system
if [[ -f "/usr/bin/chromium-gost-stable" ]]; then
    MYVER=${MYVER:-$(/usr/bin/chromium-gost-stable --product-version 2>/dev/null || true)}
    echo -e " ${BOLD}Checking if Chromium-Gost is installed ...${RESET}"
    echo -e "[${GREEN}${BOLD}OK${RESET}] Chromium-Gost is installed"
    echo -e "[${GREEN}${BOLD}OK${RESET}] Chromium-Gost version: ${GREEN}${BOLD}${MYVER}${RESET}\n"
fi

if [[ "${MYVER:-}" == "$VERSION" ]]; then
    echo -e "You already have the latest ${GREEN}${BOLD}${MYVER}${RESET} version of $PRGNAM"
    sleep 5 && exit 0
else
    read -p "$(echo -e "Would you like to build $PRGNAM ${GREEN}${BOLD}${VERSION}${RESET} xzm module? [y/n] ")" -n 1 -r -s
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 0
    fi
    echo "We continue the execution of the $0 script" &>/dev/null
fi

rm -rf "$PKG"
mkdir -p "$TMPDIR" "$PKG" "$OUTPUT"
cd "$PKG"

# Download Chromium-Gost
echo -e "\nDownloading ${CYAN}${BOLD}${PRGNAM}-${VERSION}${RESET}"
if ! wget -q --show-progress "$SOURCE"; then
    echo -e "${RED}${BOLD}Failed to download $SOURCE${RESET}"
    cleanup
fi

# Extract RPM
for i in *.rpm; do
    if [ -f "$i" ]; then
        rpm2cpio "$i" | cpio -idmv &>/dev/null
    fi
done
rm -f ./*.rpm && rm -rf "$PKG/usr/share/man" && rm -rf "$PKG/etc"

# Move icons
mkdir -p "$PKG/usr/share/icons/hicolor/"{24x24,32x32,48x48,64x64,128x128,256x256}"/apps"
rm -f "$PKG/opt/chromium-gost/product_logo_16.png"
rm -f "$PKG/opt/chromium-gost/product_logo_32.png" "$PKG/opt/chromium-gost/product_logo_32.xpm"
mv -f "$PKG/opt/chromium-gost/product_logo_24.png" "$PKG/usr/share/icons/hicolor/24x24/apps/chromium-gost.png"
# Note. About cp -a 48x48 to 32x32
cp -a "$PKG/opt/chromium-gost/product_logo_48.png" "$PKG/usr/share/icons/hicolor/32x32/apps/chromium-gost.png"
mv -f "$PKG/opt/chromium-gost/product_logo_48.png" "$PKG/usr/share/icons/hicolor/48x48/apps/chromium-gost.png"
mv -f "$PKG/opt/chromium-gost/product_logo_64.png" "$PKG/usr/share/icons/hicolor/64x64/apps/chromium-gost.png"
mv -f "$PKG/opt/chromium-gost/product_logo_128.png" "$PKG/usr/share/icons/hicolor/128x128/apps/chromium-gost.png"
mv -f "$PKG/opt/chromium-gost/product_logo_256.png" "$PKG/usr/share/icons/hicolor/256x256/apps/chromium-gost.png"

# Old tweaks
#ln -sf /usr/lib64/libEGL.so $PKG/opt/chromium-gost/libEGL.so
#ln -sf /usr/lib64/libGLESv2.so $PKG/opt/chromium-gost/libGLESv2.so
#cp -a $PKG/opt/chromium-gost/{libEGL.so,libGLESv2.so} $PKG/opt/chromium-gost/swiftshader

## functions to choose locale from a menu
array_menu(){
    echo
    echo "$1"
    echo "$2"
    select CHOICE in "${RESULT[@]}"; do
        if [ -z "$CHOICE" ]; then
            CHOICE="en_US"
            echo "English locale chosen." && echo
        else
            echo -e "${BOLD}${CYAN}${CHOICE}${RESET} locale chosen"
        fi
        break
    done
}

get_locale(){
    # Set locales in array
    local locales_list="af am ar bg bn ca cs da de el en-GB en-US es-419 es et fa fi fil fr gu he hi hr hu id it ja kn ko lt lv ml mr ms nb nl pl pt-BR pt-PT ro ru sk sl sr sv sw ta te th tr uk ur vi zh-CN zh-TW"
    RESULT=()
    for a in $locales_list; do
        RESULT+=( "$a" )
    done

    array_menu "Choose a locale from the list." "All other locales will be removed."
    unset RESULT
}

get_locale

echo "Removing locales ..."
if [ -d "$PKG/opt/chromium-gost/locales" ]; then
    find "$PKG/opt/chromium-gost/locales" -type f ! -name 'en-US.pak' | grep -Ev "${CHOICE}.pak" | xargs -r rm -f
fi

# Remove MimeType. For example pdf files are open in the Chromium-Gost
for desktop_file in "$PKG/usr/share/applications/"*.desktop; do
    [ -f "$desktop_file" ] && sed -i '/MimeType/d' "$desktop_file"
done

### fake Slackware type package info: super dumb version
mkdir -p "$PKGINFO"
echo "PACKAGE NAME: $PRGNAM-$VERSION-$ARCH" > "$PKGINFO/$PRGNAM-$VERSION-$ARCH"

cat >> "$PKGINFO/$PRGNAM-$VERSION-$ARCH" << EOM
PACKAGE DESCRIPTION:
chromium-gost: chromium-gost (Open Source version of Chrome Web Browser)
chromium-gost:
chromium-gost: Chromium-Gost is the open-source project behind Chromium,
chromium-gost: with GOST cryptographic algorithms support.
chromium-gost:
chromium-gost: This package contains the un-googled version.
chromium-gost:
chromium-gost:
chromium-gost:
chromium-gost: chromium-gost home: https://github.com/deemru/Chromium-Gost
chromium-gost:
FILE LIST:
EOM

find . -not -path './var/*' -not -name 'var' | sort >> "$PKGINFO/$PRGNAM-$VERSION-$ARCH" 2>/dev/null

# Create xzm module
if ! dir2xzm "$PKG" "$OUTPUT/$PRGNAM-$VERSION-$ARCH-${CHOICE}.xzm"; then
    echo -e "\n${RED}${BOLD}Failed to create xzm module.${RESET}\n"
    cleanup
fi

# Check on Chromium-Gost xzm file exists in /tmp
if [ -f "$OUTPUT/$PRGNAM-$VERSION-$ARCH-${CHOICE}.xzm" ]; then
    echo -e "\n${BOLD}Your $PRGNAM module is at: ${GREEN}${BOLD}$OUTPUT/$PRGNAM-$VERSION-$ARCH-${CHOICE}.xzm${RESET}\n${BOLD}Please copy it to your modules folder to survive a reboot.${RESET}\n"
else
    echo -e "\n${RED}${BOLD}Failed. Your $PRGNAM module is not built.${RESET}\n"
fi
cleanup
