#!/bin/sh
#script - gutosie 

if [ -f /proc/stb/info/vumodel ];  then  
    VUMODEL=$( cat /proc/stb/info/vumodel )     
fi 

if [ -f /proc/stb/info/boxtype ];  then  
    BOXTYPE=$( cat /proc/stb/info/boxtype )    
fi

if [ -f /proc/stb/info/chipset ];  then  
    CHIPSET=$( cat /proc/stb/info/chipset )    
fi

if [ -f /tmp/zImage.ipk ];  then  
    rm -f /tmp/zImage.ipk    
fi

if [ -f /tmp/zImage ];  then  
    rm -f /tmp/zImage    
fi

if [ ! -f /bin/busybox.nosuid ];  then
        ln -sf "busybox" "/bin/busybox.nosuid"   
fi

KERNEL=`uname -r` 
IMAGE=ImageBoot
IMAGENEXTBOOT=/ImageBoot/.neonextboot
NEOBOOTMOUNT=$( cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location) 
BOXNAME=$( cat /etc/hostname) 
NandWrite=/usr/lib/enigma2/python/Plugins/Extensions/NeoBoot//bin/nandwrite
# $NEOBOOTMOUNT$IMAGE 
# $NEOBOOTMOUNT
UPLOAD=ImagesUpload
MOUNTneoDisk=$( cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/bin/install)
MOUNTblkid=$( cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/bin/reading_blkid)
MOUNTMEDIA=$( ls /media)
DYSKMODELVENDOR=$( cat /sys/block/sda/device/model | sed "s/ *$//"; cat /sys/block/sd*/device/vendor | sed "s/ *$//" ) 

if [ -f $NEOBOOTMOUNT$IMAGENEXTBOOT ]; then
  TARGET=`cat $NEOBOOTMOUNT$IMAGENEXTBOOT`
else
  TARGET=Flash              
fi
                   
if [ -e /.multinfo ]; then
    #if [ $TARGET = "Flash" ]; then                    
    if [ $BOXNAME = "bm750" ] || [ $BOXNAME = "vuduo" ] || [ $BOXNAME = "vusolo" ] || [ $BOXNAME = "vuuno" ] || [ $BOXNAME = "vuultimo" ]; then                                                
                            if [ -f /proc/stb/info/vumodel ] || [ ! -e /proc/stb/info/boxtype ]; then
                                if [ -e $NEOBOOTMOUNT$UPLOAD/.kernel/$BOXNAME.vmlinux.gz ] ; then
                                    echo "Kasowanie kernel z /dev/mtd1..."
                                    sleep 2                                
                                    flash_eraseall /dev/mtd1  
                                    echo "Instalacja kernel do /dev/mtd1..." 
                                    sleep 2
		                                $NandWrite -p /dev/mtd1 $NEOBOOTMOUNT$UPLOAD/.kernel/$BOXNAME.vmlinux.gz
                                fi
                            fi
                            update-alternatives --remove vmlinux vmlinux-`uname -r` || true                                          
                            echo "NEOBOOT is booting image from " $TARGET
                            echo "Used Kernel: " $TARGET > $NEOBOOTMOUNT$UPLOAD/.kernel/used_flash_kernel 
    elif  [ $BOXNAME = "vusolo2" ] || [ $BOXNAME = "vuduo2" ] || [ $BOXNAME = "vusolose" ] || [ $BOXNAME = "vuzero" ]; then     #[ $BOXNAME = "mbultra" ]
                            if [ -f /proc/stb/info/vumodel ] || [ ! -e /proc/stb/info/boxtype ]; then
                                if [ -e $NEOBOOTMOUNT$UPLOAD/.kernel/$BOXNAME.vmlinux.gz ] ; then
                                    echo "Kasowanie kernel z /dev/mtd2..."
                                    sleep 2                                
                                    flash_eraseall /dev/mtd2  
                                    echo "Instalacja kernel do /dev/mtd2..." 
                                    sleep 2
		                                $NandWrite -p /dev/mtd2 $NEOBOOTMOUNT$UPLOAD/.kernel/$BOXNAME.vmlinux.gz
                                fi
                            fi
                            update-alternatives --remove vmlinux vmlinux-`uname -r` || true                                          
                            echo "NEOBOOT is booting image from " $TARGET
                            echo "Used Kernel: " $TARGET > $NEOBOOTMOUNT$UPLOAD/.kernel/used_flash_kernel
    elif [ $BOXNAME = "vuultimo4k" ] || [ $BOXNAME = "vusolo4k" ] || [ $BOXNAME = "vuuno4kse" ] || [ $BOXNAME = "vuuno4k" ]; then
                INFOBOOT=$( cat /.multinfo )
                if [ $TARGET = $INFOBOOT ] ; then
                                    echo "NEOBOOT is booting image " $TARGET
                elif [ -e /.multinfo ]; then
                                [ $PL ] && echo "Instalacja pliku kernel bin /dev/mmcblk0p1......" || echo "Instaling kernel bin file /dev/mmcblk0p1... "                                                                                                              
                                cd /media/InternalFlash; ln -sfn /sbin/init.sysvinit /media/InternalFlash/sbin/init
                                if [ -e $NEOBOOTMOUNT$UPLOAD/.kernel/flash-kernel-$BOXNAME.bin ] ; then                                                                                                                                                                                                                       
                                    if [ -d /proc/stb ] ; then
                      	    	            dd if=$NEOBOOTMOUNT$UPLOAD/.kernel/flash-kernel-$BOXNAME.bin of=/dev/mmcblk0p1
                                    fi
                                    echo "Boot - Flash. "                                                                                                                                                                                        
                                    echo "Start image Flash in progress _RESTART_..." 
                                fi
                                cat /dev/mmcblk0p1 | grep "kernel"
                                update-alternatives --remove vmlinux vmlinux-`uname -r` || true
                                echo "NEOBOOT is booting image from " $TARGET
                                echo "Used Kernel: " $TARGET > $NEOBOOTMOUNT$UPLOAD/.kernel/used_flash_kernel                                 
                fi
    elif [ $BOXNAME = "vuduo4k" ] ; then
                [ $PL ] && echo "Instalacja pliku kernel bin /dev/mmcblk0p6......" || echo "Instaling kernel bin file /dev/mmcblk0p6... "                
                if [ -e /.multinfo ]; then                                                                                                                             
                                cd /media/InternalFlash; ln -sfn /sbin/init.sysvinit /media/InternalFlash/sbin/init
                                if [ -e $NEOBOOTMOUNT$UPLOAD/.kernel/flash-kernel-$BOXNAME.bin ] ; then                                                                                                                                                                                                                       
                                    if [ -d /proc/stb ] ; then
                      	    	            dd if=$NEOBOOTMOUNT$UPLOAD/.kernel/flash-kernel-$BOXNAME.bin of=/dev/mmcblk0p6
                                    fi
                                    echo "Boot - Flash. "                                                                                                                                                                                        
                                    echo "Start image Flash z dysku hdd lub usb za 5 sekund RESTART...; \n\n..................._REBOOT_..................." 
                                fi
                                cat /dev/mmcblk0p1 | grep "kernel"
                                update-alternatives --remove vmlinux vmlinux-`uname -r` || true
                                echo "NEOBOOT is booting image from " $TARGET
                                echo "Used Kernel: " $TARGET > $NEOBOOTMOUNT$UPLOAD/.kernel/used_flash_kernel                                
                fi
    elif [ $BOXNAME = "vuduo4kse" ] ; then
                [ $PL ] && echo "Instalacja pliku kernel bin /dev/mmcblk0p6......" || echo "Instaling kernel bin file /dev/mmcblk0p6... "                
                if [ -e /.multinfo ]; then                                                                                                                             
                                cd /media/InternalFlash; ln -sfn /sbin/init.sysvinit /media/InternalFlash/sbin/init
                                if [ -e $NEOBOOTMOUNT$UPLOAD/.kernel/flash-kernel-$BOXNAME.bin ] ; then                                                                                                                                                                                                                       
                                    if [ -d /proc/stb ] ; then
                      	    	            dd if=$NEOBOOTMOUNT$UPLOAD/.kernel/flash-kernel-$BOXNAME.bin of=/dev/mmcblk0p6
                                    fi
                                    echo "Boot - Flash. "                                                                                                                                                                                        
                                    echo "Start image Flash z dysku hdd lub usb za 5 sekund RESTART...; \n\n..................._REBOOT_..................." 
                                fi
                                cat /dev/mmcblk0p1 | grep "kernel"
                                update-alternatives --remove vmlinux vmlinux-`uname -r` || true
                                echo "NEOBOOT is booting image from " $TARGET
                                echo "Used Kernel: " $TARGET > $NEOBOOTMOUNT$UPLOAD/.kernel/used_flash_kernel                                
                fi
    elif [ $VUMODEL = "zero4k" ]   ; then
                INFOBOOT=$( cat /.multinfo )
                if [ $TARGET = $INFOBOOT ] ; then
                                    echo "NEOBOOT is booting image " $TARGET
                elif [ -e /.multinfo ]; then
                                [ $PL ] && echo "Instalacja pliku kernel bin /dev/mmcblk0p4......" || echo "Instaling kernel bin file /dev/mmcblk0p4... "                                                                                                              
                                cd /media/InternalFlash; ln -sfn /sbin/init.sysvinit /media/InternalFlash/sbin/init
                                if [ -e $NEOBOOTMOUNT$UPLOAD/.kernel/flash-kernel-$BOXNAME.bin ] ; then                                                                                                                                                                                                                       
                                    if [ -d /proc/stb ] ; then
                      	    	            dd if=$NEOBOOTMOUNT$UPLOAD/.kernel/flash-kernel-$BOXNAME.bin of=/dev/mmcblk0p4
                                    fi
                                    echo "VUZERO4K - Boot - Flash. "                                                                                                                                                                                        
                                    echo "Start image Flash z dysku hdd lub usb za 5 sekund _RESTART_..." 
                                fi
                                cat /dev/mmcblk0p1 | grep "kernel"
                                update-alternatives --remove vmlinux vmlinux-`uname -r` || true
                                echo "NEOBOOT is booting image from " $TARGET
                                echo "Used Kernel: " $TARGET > $NEOBOOTMOUNT$UPLOAD/.kernel/used_flash_kernel
                fi
    fi                        
                        echo "Used Kernel: " $TARGET  > $NEOBOOTMOUNT$UPLOAD/.kernel/used_flash_kernel 
                        echo "CHIPSET:"$CHIPSET $HARDWARETYPE" BOXNAME:"$BOXNAME" MODEL:"$VUMODEL" "
                        sleep 1
                        echo "Neoboot location: "
                        echo ""$MOUNTneoDisk" "
                        sleep 1
                        echo "Info media: "
                        echo ""$MOUNTblkid" "
                        sleep 1
                        echo "Media list: "
                        echo ""$MOUNTMEDIA" "
                        sleep 1                        
                        echo "DYSK MODEL VENDOR: "
                        echo ""$DYSKMODELVENDOR" "
                        sleep 1
                        sync && echo 3 > /proc/sys/vm/drop_caches
                        echo "...............Rebooting - Shutdown Now..............."
                        sleep 5
                        PATH=/sbin:/bin:/usr/sbin:/usr/bin
                        reboot -d -f    
                               
else
                    ln -sfn /sbin/init.sysvinit /sbin/init
                    echo "CHIPSET: " $CHIPSET " BOXNAME: "$BOXNAME" MODEL: "$VUMODEL" "
                    echo "Flash "  > $NEOBOOTMOUNT/ImageBoot/.neonextboot
                    echo "Error - Nie wpierany model STB !!! "
                    exit 0
fi
exit 0
