#!/bin/sh

# Script by MrVideo from https://forum.qnap.com/
# V1.1 3/18/18

#
# Kernel must be 3.2, or later
#
KERNEL=`uname -r`
KERNEL1=`echo $KERNEL | cut -d. -f1`
KERNEL2=`echo $KERNEL | cut -d. -f2`

if [[ $KERNEL1 -lt 3 ]] ; then
    echo "Current kernel is $KERNEL and cannot be upgraded to Entware."
    exit
elif [[ $KERNEL2 -lt 2 ]] ; then
    echo "Current kernel is $KERNEL and cannot be upgraded to Entware."
    exit
else
    echo "Current kernel is $KERNEL and the upgrade will continue."
fi

#
# Architecture must be armv5 or x86-64
#
if [[ -n `grep x86-64 /opt/etc/opkg.conf` ]] ; then
    echo "Architecture is x86-64, upgrade will continue."
    echo "Updating config file."
    wget http://bin.entware.net/x64-k3.2/installer/opkg.conf -O /opt/etc/opkg.conf
    echo "Updating and upgrading."
    sed s/x64-3.x/x86-64/ /opt/etc/opkg.conf > temp.conf
    mv temp.conf /opt/etc/opkg.conf
elif [[ -n `grep armv5 /opt/etc/opkg.conf` ]] ; then
    echo "Architecture is armv5, upgrade will continue."
    echo "Updating config file."
    wget http://bin.entware.net/armv5sf-k3.2/installer/opkg.conf -O /opt/etc/opkg.conf
    sed s/armv5-3x/armv5soft/ /opt/etc/opkg.conf > temp.conf
    mv temp.conf /opt/etc/opkg.conf
else
    echo "Architecture is not armv5 or x86-64, upgrade cannot continue."
    exit
fi
echo "Updating and upgrading."
opkg update
opkg upgrade
#
# Creating missing symlinks
#
wget http://bin.entware.net/other/symlinks.sh -O- | sh
