#!/bin/bash

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

while read SOFTCAMKEY
do
grep 'UA: 00540716\|UA: 0057B90F\|UA: 003E7E84\|UA: 00233C13' $SOFTCAMKEY | awk '{print $3" "$4}' > key
if [ -s key ]
then
grep ^00 key | tail -1 > temp_key
grep ^01 key | tail -1 >> temp_key
echo "Found New Keys ;)"
echo
cat temp_key | awk '{print $2}'
> SoftCam.Key
while read line
do
while read key
do
echo P $line $key >> SoftCam.Key
done < temp_key
done < /usr/lib/enigma2/python/Plugins/Extensions/AFNKeyUpdater/sids/P_AFN
rm temp_key

                        sed -i '/AFN KEYs/,/AFN KEYs end/d' $SOFTCAMKEY
                        echo "######################## AFN Keys 9.0 #######################" >> $SOFTCAMKEY
                        cat SoftCam.Key >> $SOFTCAMKEY
                        echo "######################## AFN Keys 9.0 #######################" >> $SOFTCAMKEY
                        echo
                        echo PLEASE RESTART OSCAM AFN KEYS UPDATED
                        echo
                        echo "ALL AFN CHANNELS FIXED FOR YOU ;)"
                        echo
                        sed -i '/UA: 00540716\|UA: 0057B90F\|UA: 003E7E84/d' $SOFTCAMKEY
			sed -i '/^$/d' $SOFTCAMKEY
                        echo
rm SoftCam.Key key
exit 0
else
echo "" > error
echo "No new Key found yet" >> error
echo "" >> error
echo "Please make sure you installed the AU Keys first" >> error
echo "(INSTALL AU Keys Option 2 from the menu)" >> error
echo "Please tune to Atlantic 9.0 east and don't change the channel" >> error
echo "The AU can take up to 120 mins if your OScam is configured correctly" >> error
echo "Once you got a picture choose AFN AU again to fix the rest of the channels" >> error
echo "" >> error
fi
done < /usr/lib/enigma2/python/Plugins/Extensions/AFNKeyUpdater/scripts/Your-Softcam-Key-Location
cat error
rm error
