#!/bin/bash

# ASCII to the script start 
echo " ▗▄▄▖▗▖ ▗▖ ▗▄▄▖▗▄▄▄▖                          "
echo "▐▌   ▐▌ ▐▌▐▌   ▐▌                             "
echo " ▝▀▚▖▐▌ ▐▌ ▝▀▚▖▐▛▀▀▘                          "
echo "▗▄▄▞▘▝▚▄▞▘▗▄▄▞▘▐▙▄▄▖                          "
echo "                                              "
echo "                                              "
echo "▗▄▄▄▖▗▖  ▗▖ ▗▄▄▖▗▄▄▄▖ ▗▄▖ ▗▖   ▗▖   ▗▄▄▄▖▗▄▄▖ "
echo "  █  ▐▛▚▖▐▌▐▌     █  ▐▌ ▐▌▐▌   ▐▌   ▐▌   ▐▌ ▐▌"
echo "  █  ▐▌ ▝▜▌ ▝▀▚▖  █  ▐▛▀▜▌▐▌   ▐▌   ▐▛▀▀▘▐▛▀▚▖"
echo "▗▄█▄▖▐▌  ▐▌▗▄▄▞▘  █  ▐▌ ▐▌▐▙▄▄▖▐▙▄▄▖▐▙▄▄▖▐▌ ▐▌"
echo "                                              "

# Check file integrity before attempting installation
echo "--------------------------------------------------------------------------------------"
echo "                            PACKAGE SECURITY VERIFICATION                             "
echo "--------------------------------------------------------------------------------------"

# Verify package file exists
if [ ! -f "$1" ]; then
    echo "Error: Package file not found - $1"
    read -p "Press Enter to close..."
    exit 1
fi

# Optional security check: hash or signature verification
echo "Security Check: Enter SHA256 hash or signature file path (press Enter to skip):"
read -r SECURITY_INPUT

if [ -n "$SECURITY_INPUT" ]; then
    if [ -f "$SECURITY_INPUT" ]; then

        # Verify RPM signature
        echo "Verifying RPM signature..."
        if rpm --checksig "$1" 2>/dev/null; then
            echo "✓ Signature verified successfully"
        else
            echo "✗ Signature verification failed - Installation cancelled"
            read -p "Press Enter to close..."
            exit 1
        fi
    else
    
        # Verify SHA256 hash
        echo "Verifying SHA256 hash..."
        FILE_HASH=$(sha256sum "$1" 2>/dev/null | cut -d' ' -f1)
        if [ "$FILE_HASH" = "$SECURITY_INPUT" ]; then
            echo "✓ Hash verified successfully"
        else
            echo "✗ Hash verification failed - Installation cancelled"
            echo "Expected: $SECURITY_INPUT"
            echo "Actual:   $FILE_HASH"
            read -p "Press Enter to close..."
            exit 1
        fi
    fi
fi

# Detect which command to use for privilege escalation
if command -v run0 >/dev/null 2>&1; then
    PRIV_CMD="run0"
elif command -v doas >/dev/null 2>&1; then
    PRIV_CMD="doas"
else
    PRIV_CMD="sudo"
fi

# Package installation
echo "--------------------------------------------------------------------------------------"
echo "                                   INSTALLING...                                      "
echo "--------------------------------------------------------------------------------------"

if [ -z "$1" ]; then
    echo "Error: No RPM file provided."
    read -p "Press Enter to close..."
    exit 1
fi

if $PRIV_CMD zypper install --no-recommends "$1"; then
    echo "Installation successful"
else
    echo "Error: Installation failed. Check dependencies."
fi

echo "--------------------------------------------------------------------------------------"
echo "                                INSTALLATION COMPLETE                                "
echo "--------------------------------------------------------------------------------------"
read -p "Press Enter to close..."
