//---------------------------------------------------------------------------

#pragma hdrstop
#include <shlobj.h>
#include <iostream>
#include <fstream>
//---------------------------------------------------------------------------

#pragma argsused

char Lang[4];
char Prefix[8];
void CreateOne(bool AllCreation);
//---------------------------------------------------------------------------
void Errorout(const char* Msg)
{
  CONSOLE_SCREEN_BUFFER_INFO cScreen;
  HANDLE cHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  GetConsoleScreenBufferInfo(cHandle, &cScreen); // get exsiting values
  SetConsoleTextAttribute(cHandle, FOREGROUND_RED| FOREGROUND_INTENSITY);
  std::cout << Msg << std::endl;
  SetConsoleTextAttribute(cHandle, cScreen.wAttributes);
  exit(EXIT_FAILURE);
}
//---------------------------------------------------------------------------
void CreateAll(void)
{
  char languages[][4] = {"BR","CAT","DE","DK","ES","FR","HU","IT","KO","NL","PL","US"};
  int NumLang = 12;
  std::ofstream temp;
  DeleteFile("bcbsfx_all.rc");
  DeleteFile(".\\sfxres\\bcbsfx_all.res");
  temp.open("bcbsfx_all.rc"); // te re-create the file
  if(!temp)
  {
    Errorout("Could not open bcbsfx_all.rc for output");
  }
  temp.close();
  for(int i = 0; i < NumLang; ++i)
  {
    strcpy(Lang, languages[i]);
    std::cout << "creating " << Lang << " file" << std::endl;
    CreateOne(true);
  }
  DWORD ExitCode;
  SHELLEXECUTEINFO SEInfo = {sizeof(SHELLEXECUTEINFO)};
  SEInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
  SEInfo.hwnd = NULL;
  SEInfo.lpFile = "brcc32.exe";
  SEInfo.lpParameters = "bcbsfx_all.rc";
  SEInfo.lpDirectory = NULL;
  SEInfo.nShow = SW_HIDE; // avoid flashing of dos box
  if(ShellExecuteEx(&SEInfo))
  {
    do
    {
      Sleep(1);
      GetExitCodeProcess(SEInfo.hProcess, &ExitCode);
    }while(ExitCode == STILL_ACTIVE);
  }
  std::cout << " _all.res file created" << std::endl;
  MoveFileEx("bcbsfx_all.RES", ".\\sfxres\\bcbsfx_all.res", MOVEFILE_REPLACE_EXISTING);
  std::cout << ".\\sfxres\\bcbsfx_all.res file created" << std::endl;
  for(int i = 0; i < 9; ++i)
  {
    strcpy(Lang, languages[i]);
    char siz[20];
    strcpy(siz,Lang);
    strcat(siz, ".siz");
    DeleteFile(siz);
  }
  DeleteFile("bcbsfx_all.rc");
}
//---------------------------------------------------------------------------
void CreateOne(bool AllCreation)
{
  // create __.r1 with lang_id
  std::ofstream temp("xxx.r1");
  if(!temp)
  {
    Errorout("Could not open xxx.r1 for output");
  }
  char text[255];
  temp << Prefix << Lang << "_BIN_LANG RCDATA\nBegin\n##LANG_ID##\nEnd\n";
  temp.close();
  strcpy(text,".\\lang\\SFX");
  strcat(text, Lang);
  strcat(text, ".TXT xxx.r1 xxx.rc");
  // convert to .rc with resmaker
  // first create compressed files
  DWORD ExitCode;
  SHELLEXECUTEINFO SEInfo = {sizeof(SHELLEXECUTEINFO)};
  SEInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
  SEInfo.hwnd = NULL;
  SEInfo.lpFile = "Resmaker.exe";
  SEInfo.lpParameters = text;
  SEInfo.lpDirectory = NULL;
  SEInfo.nShow = SW_HIDE; // avoid flashing of dos box
  if(ShellExecuteEx(&SEInfo))
  {
    do
    {
      Sleep(1);
      GetExitCodeProcess(SEInfo.hProcess, &ExitCode);
    }while(ExitCode == STILL_ACTIVE);
  }
  DeleteFile("xxx.r1");
  std::cout << "rc file with lang id created" << std::endl;
  // put bin as RCData in rc
  temp.open("xxx.rc", std::ios::ate);
  if(!temp)
  {
    Errorout("Could not open xxx.rc for output");
  }
  temp << Prefix << Lang;
  temp << "_BIN RCDATA \".\\sfx\\bcbsfx";
  temp << Lang << ".bin\"\n";
  // put size in rc
  temp << Prefix << Lang << "_BIN_SIZE RCDATA \"" << Lang << ".siz\"" << std::endl;
    // get version from .bin
  strcpy(text, ".\\sfx\\");
  strcat(text, Prefix);
  strcat(text, Lang);
  strcat(text, ".bin ");
  DWORD VersionHandle;
  int VersionSize = GetFileVersionInfoSize(text, & VersionHandle);
  if(VersionSize)
  {
    char* Buffer = new char[VersionSize];
    GetFileVersionInfo(text, VersionHandle, VersionSize, Buffer);
    char *buff;
    UINT buflen;
    char txtstr[100];
    char sbuff[30];
    struct LANGANDCODEPAGE
    {
      WORD wLanguage;
      WORD wCodePage;
    } *lpTranslate;
    UINT vsize;
    VerQueryValue(Buffer,"\\VarFileInfo\\Translation",(LPVOID *)&lpTranslate,&vsize);
    wsprintf(sbuff,"\\StringFileInfo\\%04x%04x\\",lpTranslate->wLanguage,lpTranslate->wCodePage);
    if(VerQueryValue(Buffer,strcat(strcpy(txtstr,sbuff),"FileVersion"),
                   reinterpret_cast<void **>(&buff),&buflen))
       // put version in rc
      temp << Prefix << Lang << "_BIN_VERSION RCDATA\nBegin\n\"" << buff << "\"\nEnd\n";
      std::cout << "size and bin added" << std::endl;
    delete[] Buffer;
  }
  else Errorout("No file version info available");
  temp.close();
  strcpy(text, Lang);
  strcat(text, ".siz");
  DeleteFile(text); // delete siz file
  // create size file
  strcpy(text, ".\\sfx\\BCBSFX");
  strcat(text, Lang);
  strcat(text, ".bin ");
  strcat(text, Lang);
  strcat(text, ".siz");
  SEInfo.lpFile = "FSize.exe";
  SEInfo.lpParameters = text;
  SEInfo.lpDirectory = NULL;
  SEInfo.nShow = SW_HIDE; // avoid flashing of dos box
  if(ShellExecuteEx(&SEInfo))
  {
    do
    {
      Sleep(1);
      GetExitCodeProcess(SEInfo.hProcess, &ExitCode);
    }while(ExitCode == STILL_ACTIVE);
  }
  std::cout << "size added" << std::endl;
  // create new xxx.res
  DeleteFile("xxx.RES");
  SEInfo.lpFile = "brcc32.exe";
  SEInfo.lpParameters = "xxx.rc";
  SEInfo.lpDirectory = NULL;
  SEInfo.nShow = SW_HIDE; // avoid flashing of dos box
  if(ShellExecuteEx(&SEInfo))
  {
    do
    {
      Sleep(1);
      GetExitCodeProcess(SEInfo.hProcess, &ExitCode);
    }while(ExitCode == STILL_ACTIVE);
  }
  std::cout << "res created" << std::endl;
  // move file to final destination
  strcpy(text, ".\\sfxres\\BCBSFX");
  strcat(text, Lang);
  strcat(text, ".res");
  std::cout << "res moved to " << text << std::endl;
  MoveFileEx("xxx.RES", text, MOVEFILE_REPLACE_EXISTING);
  // copy rx to rc_all if needed
  if(AllCreation)
  {
      temp.open("bcbsfx_all.rc",std::ios::ate);
      if(!temp)
      {
        Errorout("Could not open bcbsfx_all.rc for output");
      }
    std::ifstream rtemp("xxx.rc");
    if(!rtemp)
    {
      Errorout("Could not open xxx.rc for input");
    }
    char ch;
    while(rtemp.get(ch)) temp.put(ch);
    temp.close();
    rtemp.close();
  }
  DeleteFile("xxx.rc");
  strcpy(text, Lang);
  if(!AllCreation)
  {
    strcat(text, ".siz");
    DeleteFile(text); // delete siz file
  }
}
//---------------------------------------------------------------------------
int main(int argc, char* argv[])
{  // expects 1st parameter Lang or All, 2nd -S for spanning
  if(argc < 2)
  {
    CONSOLE_SCREEN_BUFFER_INFO cScreen;
    HANDLE cHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(cHandle, &cScreen); // get exsiting values
    SetConsoleTextAttribute(cHandle, FOREGROUND_GREEN);
    std::cout << "==> Usage MakeBinRes <Lang> [-S]\n";
    std::cout << "==> Lang = Language indicator e.g. US\n";
    std::cout << std::endl;
    SetConsoleTextAttribute(cHandle, cScreen.wAttributes);
    return -1;
  }
  strcpy(Lang, argv[1]); //copy argurment in Lang
  strcpy(Prefix,"BCBSFX");
  for(unsigned i = 0; i < strlen(Lang); ++i)
      Lang[i] = toupper(Lang[i]);
  if(strcmp(Lang, "ALL") == 0) CreateAll();
  else CreateOne(false);
  return 0;
}
//---------------------------------------------------------------------------

