#!/bin/sh

if [ -s /usr/lib/enigma2/python/Plugins/Extensions/AFNKeyUpdater/scripts/Your-Softcam-Key-Location ]
then
        echo "I found your SoftCam.Key file ;) "
else
        echo
        echo "I can't find your SoftCam.Key please use option 4 of the menu first "
        echo
        exit 0
fi

cat /tmp/index.html | sed -e 's/<[^>]*>//g' > temp
grep PowerVu temp | tr '.' '\n'  | grep -i AFN | head -n 1 | tr 'Key' '\n' | grep ^0 | grep -iv "&" | sed '/^$/d;s/[[:blank:]]//g' | cut -f 2 -d ':' | sed -e 's/^\(.\{14\}\).*/\1/' > 00
grep PowerVu temp | tr '.' '\n'  | grep -i AFN | head -n 1 | tr 'Key' '\n' | grep ^1 | grep -iv "&" | sed '/^$/d;s/[[:blank:]]//g' | cut -f 2 -d ':' | sed -e 's/^\(.\{14\}\).*/\1/' > 01
echo "################# AFN KEYs added by POWERVU_KEY_UPDATER #################" >> SoftCam.Key.AFN

while read line
do
cat 00 | sed "s/^/P $line 00 /g" >> SoftCam.Key.AFN
cat 01 | sed "s/^/P $line 01 /g" >> SoftCam.Key.AFN
done < /usr/lib/enigma2/python/Plugins/Extensions/AFNKeyUpdater/sids/P_AFN
echo "################# AFN KEYs end #################" >> SoftCam.Key.AFN
rm /tmp/index.html 00 01 temp

                        while read line
                        do
                        sed -i '/AFN KEYs added/,/AFN KEYs end/d' $line
                        cat SoftCam.Key.AFN >> $line
                        done < /usr/lib/enigma2/python/Plugins/Extensions/AFNKeyUpdater/scripts/Your-Softcam-Key-Location
rm SoftCam.Key.AFN
                        echo
                        echo PLEASE RESTART OSCAM AFN KEYS UPDATED
                        echo
