#!/bin/ash

# Replace distfeeds.conf with non-working sources commented out
# Check if the temporary distfeeds.conf file exists
if [ -f /tmp/distfeeds.conf ]; then
    # Backup the existing /etc/opkg/distfeeds.conf if it exists
    if [ -f /etc/opkg/distfeeds.conf ]; then
        cp /etc/opkg/distfeeds.conf /etc/opkg/distfeeds.conf.bak
        echo "Backed up original /etc/opkg/distfeeds.conf to /etc/opkg/distfeeds.conf.bak"
    fi

    # Move the new distfeeds.conf from /tmp to /etc/opkg
    mv /tmp/distfeeds.conf /etc/opkg/distfeeds.conf
    echo "Replaced /etc/opkg/distfeeds.conf with the custom version."
else
    echo "Error: /tmp/distfeeds.conf not found. Cannot replace /etc/opkg/distfeeds.conf."
    exit 1
fi


# Make the init script executable
chmod +x /etc/init.d/mount-fix
chmod +x /etc/init.d/init-overlay-watchdog
chmod +x /usr/sbin/init-overlay-watchdog.sh

# Enable and start the service
service mount-fix enable
service mount-fix start
sleep 1
opkg install /tmp/libinotifytools_3.20.11.0-1_aarch64_cortex-a53.ipk
opkg install /tmp/inotifywait_3.20.11.0-1_aarch64_cortex-a53.ipk
service init-overlay-watchdog enable
service init-overlay-watchdog start
echo -e "\e[32m sdxpinn-mount-fix installed! Here is the new file structure! \e[0m"
echo -e "\e[32m ============================================================ \e[0m"
mount && df -h
echo -e "\e[32m ============================================================ \e[0m"
echo -e "\e[32m sdxpinn-mount-fix installed! New file structure above! \e[0m"
exit 0
