#!/bin/sh

# Vavoo Stream Live - Preinstallation Script
# Author: Lululla
# License: CC BY-NC-SA 4.0

print_header() {
    echo "############################################################"
    echo "#                                                          #"
    echo "#  Vavoo Stream Live Plugin - Installation                 #"
    echo "#  Author: Lululla (https://github.com/Belfagor2005)       #"
    echo "#  License: CC BY-NC-SA 4.0                                #"
    echo "#                                                          #"
    echo "############################################################"
}

print_footer() {
    echo "############################################################"
    echo "#                                                          #"
    echo "#  Installation Completed Successfully!                    #"
    echo "#                                                          #"
    echo "#  Important: Restart Enigma2 to activate the plugin       #"
    echo "#                                                          #"
    echo "#  Access:                                                 #"
    echo "#  - Main Menu -> Plugins -> Vavoo Stream Live             #"
    echo "#  - Or via dedicated button in extensions                 #"
    echo "#                                                          #"
    echo "#  Thank you for choosing Vavoo Stream Live!               #"
    echo "#                                                          #"
    echo "############################################################"
}

# Check that opkg exists
command -v opkg >/dev/null 2>&1 || {
    echo "ERROR: opkg package manager not found."
    echo "This script requires an Enigma2-based system with opkg."
    exit 1
}

print_header

# Remove old version if found
echo "Checking for existing Vavoo installations..."
if [ -d "/usr/lib/enigma2/python/Plugins/Extensions/vavoo" ]; then
    echo "Removing previous version of Vavoo plugin..."
    rm -rf /usr/lib/enigma2/python/Plugins/Extensions/vavoo > /dev/null 2>&1
    echo "✓ Previous version removed successfully"
else
    echo "✓ No previous installation found"
fi

# This package requires Python 3 (see CONTROL/control's Depends:
# python3-requests, python3-six - opkg already refuses to install this
# package on a system without a python3-requests candidate, so there is
# no supported Python 2 fallback here).
echo "Installing required dependencies..."
PY="python3"
SIXPKG="python3-six"
REQPKG="python3-requests"
if [ -e "/usr/bin/python3" ]; then
    echo "✓ Using Python 3"
else
    echo "⚠ /usr/bin/python3 not found, but continuing (required by package dependencies)"
fi

# Update package list and install dependencies
echo "Updating package repository..."
opkg update > /dev/null 2>&1

echo "Installing ${SIXPKG}..."
if opkg install --force-reinstall "${SIXPKG}" > /dev/null 2>&1; then
    echo "✓ ${SIXPKG} installed successfully"
else
    echo "⚠ Could not install ${SIXPKG}, but continuing installation"
fi

echo "Installing ${REQPKG}..."
if opkg install --force-reinstall "${REQPKG}" > /dev/null 2>&1; then
    echo "✓ ${REQPKG} installed successfully"
else
    echo "⚠ Could not install ${REQPKG}, but continuing installation"
fi

# Additional dependency checks
echo "Verifying system compatibility..."
if [ -e "/usr/bin/enigma2" ]; then
    echo "✓ Enigma2 detected"
else
    echo "⚠ Enigma2 not found in standard location"
fi

print_footer

exit 0