#!/bin/sh

#set -x

# Inicializacia premennych
KEYFILE1=/var/scce/tpscrypt
KEYFILE2=/var/keys/camd3.keys
KEYFILE3=/var/keys/tps
KEYFILE4=/var/keys/SoftCam.Key
PCAMD=/var/bin/pcamd_3825
WWWSOFTCAM=http://byksat.kabel1.cz/dreambox/SoftCam.Key

# stiahnutie noveho kluca
new_key=`wget -q -O - $WWWSOFTCAM | grep "V 007C00 01"`

if [ ! -e $KEYFILE1 ]
then
echo "Newcamd: Nemozem najst subor $KEYFILE1"
else

# Uprava AES kluca na pozadovany tvar pre newcamd
new=`echo $new_key | sed -e 's/^V \(......\) \(..\) \(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)/\1:\2:\3 \4 \5 \6 \7 \8 \9 /' -e 's/^\(.\{31\}\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)/\1\2 \3 \4 \5 \6 \7 \8 \9 /' | sed 'y/abcdef/ABCDEF/'`


# Zistenie aktualneho AES kluca
current=`grep -i "^007c00:01:" $KEYFILE1 | tail -1 | sed 'y/abcdef/ABCDEF/' `

# Porovnanie noveho a aktualneho kluca
if [ "$new" = "$current" ]
then
      echo "Newcamd: Aktualny kluc pre TPS je v poriadku"
else
      echo "Newcamd: Kluc pre TPS bol aktualizovany"
   

      # Zapis noveho kluca do $KEYFILE1
      sed -e "s/^007[cC]00:01:.*/$new/" $KEYFILE1 > /tmp/tpscryp.new
      
     cp /tmp/tpscryp.new $KEYFILE1
fi
fi

if [ ! -e $KEYFILE2 ]
then
echo "Camd3:   Nemozem najst subor $KEYFILE2"
else

# Uprava AES kluca na pozadovany tvar
new=`echo $new_key | sed -e "s/^V 007C00 01 /0500:007C00:0000000000:80:/g"`

# Zistenie aktualneho kluca
current=`grep "^0500:007C00:0000000000:80:" $KEYFILE2 | tail -1`

# Porovnanie noveho a aktualneho kluca
if [ "$new" = "$current" ]
then
      echo "Camd3:   Aktualny kluc pre TPS je v poriadku"
      
else
      echo "Camd3:   Kluc pre TPS bol aktualizovany"
      
      # Zapis noveho kluca do $KEYFILE2
      sed -e "s/^0500:007C00:0000000000:80:.*/$new/" $KEYFILE2 > /tmp/camd3.keys.new
      
      cp /tmp/camd3.keys.new $KEYFILE2
      # on applique les clefs:
      if [ -e $PCAMD ]
      then
        $PCAMD -loadkeys  $KEYFILE2
      fi
     
fi
fi

if [ ! -e $KEYFILE3 ]
then
echo "Evocamd: Nemozem najst subor $KEYFILE3"
else

current=`grep T: $KEYFILE3 |awk '{FS="{";print $NF}' | awk '{FS="}";print $1}' |sed 's/ //g'` 
new=`echo $new_key | awk '{print $NF}'` 

if [ "T:$new" = "$current" ] 
then 
      echo "Evocamd: Aktualny kluc pre TPS je v poriadku"
else
      echo "Evocamd: Kluc pre TPS bol aktualizovany"

      rest=$new 
      aes= 

      i=0 
      while test $i -le 15 
      do 
        i=`expr $i + 1` 
        char=`echo $rest`
        oct=`echo $char | awk '{print $1}'` 
        aes=`echo $aes " $oct"` 
        rest=`echo $char | awk '{print $2}'` 
      done 

      sed -e "s/^T:.*/T:`echo $aes "`/" $KEYFILE3 > /tmp/new_tpscrypt 
      mv /tmp/new_tpscrypt $KEYFILE3
fi
fi

if [ ! -e $KEYFILE4 ]
then
echo "Mgcamd:  Nemozem najst subor $KEYFILE4"
else

current=`cat $KEYFILE4 | grep "V 007C00 01" | awk '{print $NF}'` 
new=`echo $new_key | awk '{print $NF}'` 

if [ "$new" = "$current" ] 
then 
      echo "Mgcamd:  Aktualny kluc pre TPS je v poriadku"
else
      echo "Mgcamd:  Kluc pre TPS bol aktualizovany"

      rest=$new 
      aes= 
      
      i=0 
      while test $i -le 15 
      do 
        i=`expr $i + 1` 
        char=`echo $rest`
        oct=`echo $char | awk '{print $1}'` 
        aes=`echo $aes " $oct"` 
        rest=`echo $char | awk '{print $2}'` 
      done 

      sed -e "s/^V 007C00 01.*/V 007C00 01 `echo $aes"`/" $KEYFILE4 > /tmp/new_tpscrypt 
      mv /tmp/new_tpscrypt $KEYFILE4 
fi
fi
