#include <stdio.h>
#include "dirent.h"
#include <sys/types.h>
//#include <stdio.h>
//#include <string.h>
//#include <ctype.h>
//#include <sys/stat.h>
//#include <fcntl.h>
//#include <errno.h>
//#include <limits.h>

char nfile[260];
char nfilebkp[260];
char bufdir[260];
char buf[400];
char codpg[20];
char bufr[30];
int i=0;
FILE *fp;
int ret=0;
int frepeat=0;
FILE * fd;
int fdel=0;

static void list_dir(const char *path) {
    struct dirent *entry;
    DIR *dir = opendir(path);
    if (dir == NULL) { printf("invalid dir/path\n",bufr);
        return;
    }
   skipbkp:
   
    while ((entry = readdir(dir)) != NULL) {
		
	   strcpy(nfile,entry->d_name);
		
		i=strlen(nfile);
		
		if (nfile[i-1] == 't')
		        {
					
				if (nfile[i-2] == 'r')
				              {	
					           if (nfile[i-3] == 's')
							     {
							     if (nfile[i-4] == '.')
								        {
									
									   sprintf(buf,"uchardet.exe  \"%s\\%s\" > utf.tmp",bufdir,nfile); 
									    system(buf);
										
										fd = fopen("utf.tmp","rb");
										
                              			ret=fread(&bufr,sizeof(char),30,fd);
										
                                        fclose(fd);	
										
										//printf("srt input encoding  %s\n",bufr);
										
									  if (strstr(bufr,"UTF-8")!=NULL || strstr(bufr,"UTF8") != NULL ) {printf("file %s is utf8 - no conversion\n",nfile); goto skipbkp;} 
									  if (strstr(bufr,"UTF-16")!=NULL || strstr(bufr,"UTF16") != NULL ) {printf("file %s is utf16 - no conversion\n",nfile); goto skipbkp;} 
										//sprintf(buf,"iconv.exe -f %s -t utf-8 \"c:\\FORCETRANSCODE\\a b.srt\" > \"C:\\FORCETRANSCODE\\a c.srt\"",codpg); //OK!!!!!
										sprintf(buf,"iconv.exe -f %s -t utf-8 \"%s\\%s\" > \"%s\\%s.new\"",codpg,bufdir,nfile,bufdir,nfile); //OK!!!!!
									//	 ret=system("iconv.exe -f cp1252 -t utf-8 ac.srt > out.srt");
		  							    printf("converting %s \n",buf);
                                         ret= system(buf);
										 if (ret != 0)
										  {
										 
										  printf("conversion error - maybe wrong codepage or character encoding %s\n",nfile);
										  sprintf(buf,"del \"%s\\%s.new\"",bufdir,nfile);
										  system(buf);
										  
                                         goto	skipbkp;									 
                                          }
                                          else  { 
										  printf("conversion ok  %s\n",nfile);										  
										        
												}
										 if (fdel == 0)
										 {	 
										 sprintf(buf,"rename \"%s\\%s\"  \"%s.bkp\"",bufdir,nfile,nfile);
										
										 ret= system(buf);
										 sprintf(buf,"rename \"%s\\%s.new\"  \"%s\"",bufdir,nfile,nfile);
										 ret= system(buf);
										 }
										 else 
										 {
                                         sprintf(buf,"del \"%s\\%s\"",bufdir,nfile);
										  system(buf); 
                                         sprintf(buf,"rename \"%s\\%s.new\"  \"%s\"",bufdir,nfile,nfile);
                                         system(buf);										 

                                         }											 
									  										 
										//system("powershell 'echo \"hello from powershell\"'");
									    										
									}
								 }
					          } 
			    }		
    }

    closedir(dir);
}

int main(int argc, char** argv) {
	
	if (argv[1] == NULL || argc==0)
	{
	 printf("usage: conv.exe -f cp1252 -d c:\\forcetranscode\n");
	 printf("or: conv.exe -f cp1252 -y -d c:\\forcetranscode\n");
	 printf("-y: bkp files not created (caution...)\n");
     printf("cp 1252 is codepage of the input .srt, for a list of codepages run iconv.exe -l\n");
     exit(0);	 
	}
	int iarg=1;
	strcpy(codpg,"cp1252");
	new45:
	if (iarg > 4) goto end66;
	strcpy(bufdir,argv[iarg]);
	if (strstr(bufdir,"-f")!=NULL || strstr(bufdir,"-F") != NULL )
	     {
          iarg++;		 
		 strcpy(codpg,argv[iarg]);
	     iarg++; goto new45;
		 }
	if (strstr(bufdir,"-y")!=NULL || strstr(bufdir,"-Y") != NULL )
	     {
          
		 fdel=1;
		 iarg++;
		 goto new45;
		 }
	if (strstr(bufdir,"-d")!=NULL || strstr(bufdir,"-D") != NULL )
	     {
         iarg++;
		 strcpy(bufdir,argv[iarg]);
		 iarg++;
		 goto new45;
		 }	 
	end66:  	 
     		 
	printf("input .srt codepage=%s\n",codpg);
	//printf("bufdir=%s\n",argv[2]);
	printf("subtitles directory=%s\n",bufdir);
	
    list_dir(bufdir);
    return EXIT_SUCCESS;
}  