/*
 * softcam.key2gbox key converter
 * version 0.91 24.02.2003
 * www.dbox2world.com

libtool --mode=compile /dbox2/cdk/bin/powerpc-tuxbox-linux-gnu-gcc -O2 -Wall -I/home/head/tuxbox-cvs/apps/include -mcpu=823 -c -o plugin.lo plugin.c
/dbox2/cdk/bin/powerpc-tuxbox-linux-gnu-gcc -shared -Wall plugin.lo -o plugin.so
/dbox2/cdk/bin/powerpc-tuxbox-linux-gnu-strip plugin.so
*/

#include <stdio.h>
#include <string.h>
#include </home/tux/tuxbox_20030224_release/apps/tuxbox/plugins/include/plugin.h>

	char key1[3];
	char key2[3];
	char key3[3];
	char key4[3];
	char key5[3];
	char key6[3];
	char key7[3];
	char key8[3];
	char key9[3];
	char key10[3];
	char key11[3];
	char key12[3];
	char key13[3];
	char key14[3];
	char key15[3];
	char key16[3];
	char key17[3];

//int main(int argc, char **argv)
void plugin_exec(PluginParam *par)
{
  char comment[255];
  char line[255];
  char line_m[60];
  int i;
  FILE *in;

  // originale key files
  FILE *out_i;
  FILE *out_s;
  FILE *out_v;
  FILE *out_n;
  // temp key files
  FILE *out_i_t = fopen("/var/tmp/irdeto_t", "w");
  FILE *out_s_t = fopen("/var/tmp/seca_t", "w");
  FILE *out_v_t = fopen("/var/tmp/via_t", "w");
  FILE *out_n_t = fopen("/var/tmp/nagra_t", "w");

  printf("\nSoftCam.Key Converter by dbox2world-team\nVersion 0.91\ngreetings to all developers\n");

  if((in=fopen("/var/tmp/SoftCam.Key", "r")) == NULL) // ist SoftCam.Key schon vorhanden ?
  { // Nein, get it.
    system("wget -c \"http://softcam.tforums.com/generate.php?prov[1]=1&prov[2]=1&prov[3]=1&prov[4]=1&prov[5]=1&prov[6]=1&prov[7]=1&prov[8]=1&prov[9]=1&prov[17]=1&prov[19]=1&prov[20]=1&prov[21]=1&prov[22]=1&prov[24]=1&prov[25]=1&prov[26]=1&prov[27]=1&prov[28]=1&prov[29]=1&prov[30]=1&prov[32]=1&prov[34]=1&prov[36]=1&prov[37]=1&prov[38]=1&prov[39]=1&prov[40]=1&prov[41]=1&prov[43]=1&prov[44]=1&prov[45]=1&prov[46]=1&prov[47]=1&prov[48]=1&prov[49]=1&prov[50]=1&prov[51]=1&prov[52]=1&prov[53]=1&prov[54]=1&prov[55]=1&prov[56]=1&prov[57]=1&prov[60]=1&prov[61]=1&prov[62]=1&prov[63]=1&type=key-file\" -O /var/tmp/SoftCam.Key");
    in = fopen("/var/tmp/SoftCam.Key", "r");
  }
//--------------orig irdeto auslesen und alles auáer I: extrahieren----------/
  if((out_i=fopen("/var/tmp/irdeto", "r")) == NULL) {
      printf("\nirdeto nicht vorhanden..\n");
      exit(0);
  } else
      while(fgets(&line_m[0], 255, out_i)!=NULL) if(strncmp(line_m,"I",1)!=0) fprintf(out_i_t, "%s", line_m);
//--------------orig seca auslesen und alles auáer S: extrahieren----------/
  if((out_s=fopen("/var/tmp/seca", "r")) == NULL) {
      printf("\nseca nicht vorhanden..\n");
      exit(0);
  } else
      while(fgets(&line_m[0], 255, out_s)!=NULL) if(strncmp(line_m,"S",1)!=0) fprintf(out_s_t, "%s", line_m);
//--------------orig via auslesen und alles auáer V: extrahieren----------/
  if((out_v=fopen("/var/tmp/via", "r")) == NULL) {
      printf("\nvia nicht vorhanden..\n");
      exit(0);
  } else
      while(fgets(&line_m[0], 255, out_v)!=NULL) if(strncmp(line_m,"V",1)!=0) fprintf(out_v_t, "%s", line_m);
//--------------orig nagra auslesen und alles auáer N: extrahieren----------/
  if((out_n=fopen("/var/tmp/nagra", "r")) == NULL) {
      printf("\nnagra nicht vorhanden..\n");
      exit(0);
  } else
      while(fgets(&line_m[0], 255, out_n)!=NULL) if(strncmp(line_m,"N",1)!=0) fprintf(out_n_t, "%s", line_m);
// SoftCam.Key loop -----------------------------------------------------------
  while(fgets(&line[0], 255, in)!=NULL)
    {
       if(strncmp(line,"I",1)==0)
       {
        sscanf(line, "I %2c     %2c %2c%2c%2c%2c%2c%2c%2c%2c;  %[A-z0-9.!,;:-+&=_ ] \n", key1, key2, key3, key4, key5, key6, key7, key8, key9, key10,comment);
        fprintf(out_i_t,"I: { %s { %s %s %s %s %s %s %s %s }} %s\n", key2,key3,key4,key5,key6,key7,key8,key9,key10,comment);
       }
       else if(strncmp(line,"S",1)==0)
       {
        sscanf(line, "S %2c%2c   %2c %2c%2c%2c%2c%2c%2c%2c%2c;  %[A-z0-9.!,;:-+&=_ ] \n", key1, key2, key3, key4, key5, key6, key7, key8, key9, key10, key11,comment);
        fprintf(out_s_t,"S: { %s %s %s { %s %s %s %s %s %s %s %s }} %s\n", key1,key2,key3,key4,key5,key6,key7,key8,key9,key10,key11,comment);
       }
       else if(strncmp(line,"V",1)==0)
       {
        sscanf(line, "V %2c%2c%2c   %2c %2c%2c%2c%2c%2c%2c%2c%2c;  %[A-z0-9.!,;:-+&=_ ] \n", key1, key2, key3, key4, key5, key6, key7, key8, key9, key10, key11, key12,comment);
        fprintf(out_v_t,"V: { %s %s %s %s { %s %s %s %s %s %s %s %s }} %s\n", key1,key2,key3,key4,key5,key6,key7,key8,key9,key10,key11,key12, comment);
       }
       else if(strncmp(line,"N",1)==0)
       {
        sscanf(line, "N %2c%2c   %2c %2c%2c%2c%2c%2c%2c%2c%2c;  %[A-z0-9.!,;:-+&=_ ] \n", key1, key2, key3, key4, key5, key6, key7, key8, key9, key10, key11,comment);
        fprintf(out_n_t,"N: { %s %s %s { %s %s %s %s %s %s %s %s }} %s\n", key1,key2,key3,key4,key5,key6,key7,key8,key9,key10,key11,comment);
       }
    }
// ----------------------------------------------------------------------------

  fclose(in);
  fclose(out_i);
  fclose(out_s);
  fclose(out_v);
  fclose(out_n);

  fclose(out_i_t);
  fclose(out_s_t);
  fclose(out_v_t);
  fclose(out_n_t);

  system("rm -rf /var/tmp/irdeto");
  system("rm -rf /var/tmp/seca");
  system("rm -rf /var/tmp/via");
  system("rm -rf /var/tmp/nagra");

  system("cp /var/tmp/irdeto_t /var/tmp/irdeto");
  system("cp /var/tmp/seca_t /var/tmp/seca");
  system("cp /var/tmp/via_t /var/tmp/via");
  system("cp /var/tmp/nagra_t /var/tmp/nagra");

  system("rm -rf /var/tmp/irdeto_t");
  system("rm -rf /var/tmp/seca_t");
  system("rm -rf /var/tmp/via_t");
  system("rm -rf /var/tmp/nagra_t");

}
