unit fun_lin;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, SynEdit;

 procedure console_cr_linux(FileName, FileExeName, param: String);
 //procedure console_cr_linux8(FileName, FileExeName, param: String);
 procedure console_cr_run_linux(FileExeName: String);

 procedure console_cr_fx_linux();

 procedure jar_linux();

 procedure console_run_fx_linux();


 function Str_Char_Del(str: String; ch: Char): String;
 function Name_Package(str1: String): String;
 function Name_Package_Dir(str: String): String;
 procedure Name_Package_Create_Dir(str: String);
 function Find_Package(SynEdit1: TSynEdit): Integer;
 function rep(java_home: String):String;
 function First_Package_Word(str: String): String;

 
implementation
uses
  Unit1, Unit2;

function First_Package_Word(str: String): String;
var word, str0: String;
    p: Integer;
begin
str0:=str;
if LastDelimiter('.',str0)>0 then
 begin
    p:= LastDelimiter('.',str0);
    word := copy(str0, 0, p-1);
    str0:= word;
 end;
      First_Package_Word:= str0;
end;



function rep(java_home: String):String;
var java_home_new: String;
    len, i: Integer;
begin
  java_home_new:='';
  len:= length(java_home);
  for i := 1 to len do
      begin
        if java_home[i] = '\' then java_home_new:= java_home_new + java_home[i] + '\'
          else java_home_new:= java_home_new + java_home[i];
      end;
  rep:=java_home_new;
end;


function Find_Package(SynEdit1: TSynEdit): Integer;
var i, se_len, p_n: Integer;
begin
 p_n:=0;
 se_len:= SynEdit1.Lines.Count;
 for i := 0 to se_len-1 do
     begin
          if pos('package', SynEdit1.Lines.Strings[i])>0 then
          begin
           p_n:=i;
           break;
          end;
     end;
  Find_Package:=p_n;
end;


 procedure Name_Package_Create_Dir(str: String);
var str1: string;
    z, p, len: integer;
    list: TStringList;
begin
 str1:=str;
 list := TStringList.Create;
 while LastDelimiter('.',str1)>0 do
  begin
     p:= LastDelimiter('.',str1);
     len:= Length(str1);
     list.add(copy(str1, p+1, len));
     str1:=copy(str1, 0, p-1);
  end;
  list.add(str1);
  for z:=list.count-1 downto 0 do
  begin
    If Not DirectoryExists(list.strings[z]) then CreateDir (list.strings[z]);
        chdir(list.strings[z]);
  end;
  list.Destroy;
end;


  function Name_Package_Dir(str: String): String;
  var str0: String;
      p: Integer;
  begin
  str0:=str;
  while LastDelimiter('.',str0)>0 do
   begin
      p:= LastDelimiter('.',str0);
      str0[p]:='\';
   end;
	Name_Package_Dir:= str0;
  end;




function Name_Package(str1: String): String;
var str_return, s2: String;
    p_n: Integer;
begin
  str_return:= str1;
  p_n:=pos('package', str_return);
  if p_n>0 then
   begin
    if pos('//', str_return)> 0 then Name_Package:= ''
    else
     begin
      s2 := copy(str_return, p_n+7, Length(str_return));
      s2:= Str_Char_Del(s2, ' ');
      s2:= Str_Char_Del(s2, ';');
      Name_Package:=s2;
     end
   end
  else Name_Package:= '';
end;


// Удаление из стоки str всех символов заданых в параметре ch
function Str_Char_Del(str: String; ch: Char): String;
var str_return: String;
begin
    str_return:= str;
    while pos(ch, str_return)<>0 do
      begin
       Delete(str_return,pos(ch, str_return),1);
      end;
    Str_Char_Del:= str_return;
end;

procedure jar_linux();
  var sss, sss1, jarstr, dop, FileJarName :String;
    i, n:  integer;
begin
      Form1.ProcessUTF8_3.Parameters.Clear;
 ChDir(ExtractFileDir(fullFileName));
     FileName := ExtractFileName(fullFileName);
     if FileExists('file_dir.ini') then Form2.ListBox1.Items.LoadFromFile('file_dir.ini');
     Form1.SynEdit1.Lines.SaveToFile(fullFileName);
     list := TStringList.Create;
     FileJarName:=Copy(FileName,1,Length(FileName)-5);
     pak:= Name_Package(Form1.SynEdit1.Lines.Strings[Find_Package(Form1.SynEdit1)]);
      if pak <> '' then
         begin
              sss := 'main-class: ' + pak + '.' + FileJarName;
              list.Clear;
              list.Add(sss);
              list.Add('');
              list.SaveToFile('MYMANIFEST.MF');
              list.Destroy;
              jarstr := DirJDK+ st_bin +'jar' + ' -cvfm '+ FileJarName + '.jar' +' MYMANIFEST.MF ' + First_Package_Word(pak);
              Form1.ProcessUTF8_3.Executable:=DirJDK+ st_bin +'jar';
              Form1.ProcessUTF8_3.Parameters.Add('-cvfm');
              Form1.ProcessUTF8_3.Parameters.Add(FileJarName + '.jar');
              Form1.ProcessUTF8_3.Parameters.Add('MYMANIFEST.MF');
              Form1.ProcessUTF8_3.Parameters.Add(First_Package_Word(pak));
         end
      else
          begin

               sss := 'main-class: ' + FileJarName;
               list.Clear;
               list.Add(sss);
               list.Add('');
               list.SaveToFile('MYMANIFEST.MF');
               list.Destroy;
               jarstr := DirJDK+ st_bin +'jar' + ' -cvfm '+ FileJarName + '.jar' +' MYMANIFEST.MF ' + '*.class ';
              Form1.ProcessUTF8_3.Executable:=DirJDK+ st_bin +'jar';
              Form1.ProcessUTF8_3.Parameters.Add('-cvfm');
              Form1.ProcessUTF8_3.Parameters.Add(FileJarName + '.jar');
              Form1.ProcessUTF8_3.Parameters.Add('MYMANIFEST.MF');
              sss1:= FileJarName+ '.class';
              if FileExists(sss1) then Form1.ProcessUTF8_3.Parameters.Add(sss1);
              sss1:= FileJarName+'$1' + '.class';
              if FileExists(sss1) then Form1.ProcessUTF8_3.Parameters.Add(sss1);
              sss1:= FileJarName+'$2' + '.class';
              if FileExists(sss1) then Form1.ProcessUTF8_3.Parameters.Add(sss1);
              sss1:= FileJarName+'$3' + '.class';
              if FileExists(sss1) then Form1.ProcessUTF8_3.Parameters.Add(sss1);
              sss1:= FileJarName+'$4' + '.class';
              if FileExists(sss1) then Form1.ProcessUTF8_3.Parameters.Add(sss1);
              sss1:= FileJarName+'$5' + '.class';
              if FileExists(sss1) then Form1.ProcessUTF8_3.Parameters.Add(sss1);
              sss1:= FileJarName+'$6' + '.class';
              if FileExists(sss1) then Form1.ProcessUTF8_3.Parameters.Add(sss1);
              sss1:= FileJarName+'$7' + '.class';
              if FileExists(sss1) then Form1.ProcessUTF8_3.Parameters.Add(sss1);
              sss1:= FileJarName+'$8' + '.class';
              if FileExists(sss1) then Form1.ProcessUTF8_3.Parameters.Add(sss1);
              sss1:= FileJarName+'$9' + '.class';
              if FileExists(sss1) then Form1.ProcessUTF8_3.Parameters.Add(sss1);
              sss1:= FileJarName+'$10' + '.class';
              if FileExists(sss1) then Form1.ProcessUTF8_3.Parameters.Add(sss1);
          end;


    Form1.Memo1.Clear;
    Form1.Memo1.Text:= jarstr;

  n:= Form2.ListBox1.Count;
  dop:='';
  if n>=0 then
     begin
         for i := 0 to n-1 do
         begin
              dop:= Form2.ListBox1.Items.Strings[i];
              Form1.ProcessUTF8_3.Parameters.Add(dop);
              Form1.Memo1.Text:= Form1.Memo1.Text + dop;
         end;
     end;


  Form1.ProcessUTF8_3.Execute;


end;


procedure console_cr_fx_linux();
  var DirFX_lib, sss, param, FileExeName :String;
 begin
     Form1.SynEdit1.Lines.SaveToFile(fullFileName);
     ChDir(ExtractFileDir(fullFileName));
     FileName := ExtractFileName(fullFileName);
     Form1.Caption:=fullFileName;
     DirFX_lib:=DirFX + st_lib;
     FileExeName:=Copy(FileName,1,Length(FileName)-5);
     pak:= Name_Package(Form1.SynEdit1.Lines.Strings[Find_Package(Form1.SynEdit1)]);
     param :='';
     if pak <> '' then
        begin
             sss:=pak + '.' + FileExeName;
             param :='-d ./';
        end
     else
         begin
              sss:= FileExeName;
         end;


      list := TStringList.Create;
      list.Clear;
      list.Add('#!/bin/bash');
      list.Add('');
      list.Add('echo " " ');
      list.Add('echo " " ');
      list.Add('echo " " ');
      list.Add('echo " " ');
      list.Add('echo " " ');
      list.Add('echo "------------------------Версия javac-----------------------------"');
      list.Add('echo " " ');
      list.Add(DirJDK+ st_bin + compil_prog + ' -version ');
      list.Add('echo " "');
      list.Add('echo "-----------------------------------------------------------------"');
      list.Add('echo "------------------Компиляция javafx программы----------------------"');
      list.Add('echo " " ');
      list.Add(DirJDK+ st_bin + compil_prog + ' ' + param + ' -encoding utf8 --module-path ' + DirFX_lib + ' --add-modules javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web ' + FileName);
      list.Add('echo " " ');
      list.Add('echo "-------------------Конец компиляции javafx программы---------------"');
      list.Add('echo " "');
      list.Add('echo "---------------------Версия java --------------------------------"');
      list.Add('echo " " ');
      list.Add(DirJDK+ st_bin + java_exe + ' -version ');
      list.Add('echo " " ');
      list.Add('echo "-----------------------------------------------------------------"');
      list.Add('echo " " ');
      list.Add('echo "-----------------Запуск javafx программы---------------------------"');
      list.Add('echo " " ');

      list.Add(DirJDK+ st_bin + java_exe + ' --module-path ' + DirFX_lib + ' --add-modules javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web ' + sss);
      list.Add('echo " " ');
      list.Add('echo "---------------Конец работы javafx программы-----------------------"');
      list.Add('echo " " ');
      list.Add('read p');
      list.SaveToFile(file_con_fx);
      list.Destroy;
end;

 procedure console_run_fx_linux();
   var DirFX_lib, sss, param, FileExeName :String;
 begin
     Form1.SynEdit1.Lines.SaveToFile(fullFileName);
     ChDir(ExtractFileDir(fullFileName));
     FileName := ExtractFileName(fullFileName);
     Form1.Caption:=fullFileName;
     DirFX_lib:=DirFX + st_lib;
     FileExeName:=Copy(FileName,1,Length(FileName)-5);
     pak:= Name_Package(Form1.SynEdit1.Lines.Strings[Find_Package(Form1.SynEdit1)]);
     param :='';
     if pak <> '' then
        begin
             sss:=pak + '.' + FileExeName;
             param :='-d ./';
        end
     else
         begin
              sss:= FileExeName;
         end;


      list := TStringList.Create;
      list.Clear;
      list.Add('#!/bin/bash');
      list.Add('');
      list.Add('echo " " ');
      list.Add('echo " " ');
      list.Add('echo " " ');
      list.Add('echo " " ');
      list.Add('echo " " ');
      list.Add('echo "---------------------Версия java --------------------------------"');
      list.Add('echo " " ');
      list.Add(DirJDK+ st_bin + java_exe + ' -version ');
      list.Add('echo " " ');
      list.Add('echo "-----------------------------------------------------------------"');
      list.Add('echo " " ');
      list.Add('echo "-----------------Запуск javafx программы---------------------------"');
      list.Add('echo " " ');

      list.Add(DirJDK+ st_bin + java_exe + ' --module-path ' + DirFX_lib + ' --add-modules javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web ' + sss);
      list.Add('echo " " ');
      list.Add('echo "---------------Конец работы javafx программы-----------------------"');
      list.Add('echo " " ');
      list.Add('read p');
      list.SaveToFile(file_run_fx);
      list.Destroy;
end;

procedure console_cr_linux(FileName, FileExeName, param: String);
begin
     list := TStringList.Create;
     list.Clear;
     list.Add('#!/bin/bash');
     list.Add('');
     list.Add('echo " " ');
     list.Add('echo " " ');
     list.Add('echo " " ');
     list.Add('echo " " ');
     list.Add('echo "------------------------Версия javac-----------------------------"');
     list.Add('echo " " ');

     if  DirJDK <> '' then
          list.Add(DirJDK + st_bin + 'javac ' + '-version ')
     else
          list.Add('javac ' + '-version ');

     list.Add('echo " "');
     list.Add('echo "-----------------------------------------------------------------"');
     list.Add('echo "------------------Компиляция java программы----------------------"');
     list.Add('echo " " ');

     if  DirJDK <> '' then
          list.Add(DirJDK+ st_bin + 'javac ' + param + ' -encoding utf8 ' + FileName)
     else
          list.Add('javac ' + param + ' -encoding utf8 ' + FileName);

     list.Add('echo " " ');
     list.Add('echo "-------------------Конец компиляции java программы---------------"');
     list.Add('echo " "');
     list.Add('echo "---------------------Версия java --------------------------------"');
     list.Add('echo " " ');
     if  DirJDK <> '' then
         list.Add(DirJDK+ st_bin + 'java ' + '-version ')
     else
         list.Add('java ' + '-version ');

     list.Add('echo " " ');
     list.Add('echo "-----------------------------------------------------------------"');
     list.Add('echo " " ');
     list.Add('echo "-----------------Запуск java программы---------------------------"');
     list.Add('echo " " ');

     if  DirJDK <> '' then
         list.Add(DirJDK+ st_bin + 'java ' + FileExeName)
     else
         list.Add('java ' + FileExeName);

     list.Add('echo " " ');
     list.Add('echo "---------------Конец работы java программы-----------------------"');
     list.Add('echo " " ');
     list.Add('read p');
     list.SaveToFile(file_con);
     list.Destroy;
end;

procedure console_cr_run_linux(FileExeName: String);
begin
     list := TStringList.Create;
     list.Clear;
     list.Add('#!/bin/bash');
     list.Add('');
     list.Add('echo " " ');
     list.Add('echo " " ');
     list.Add('echo "---------------------Версия java --------------------------------"');
     list.Add('echo " " ');
     if  DirJDK <> '' then
         list.Add(DirJDK+ st_bin + 'java ' + '-version ')
     else
         list.Add('java ' + '-version ');

     list.Add('echo " " ');
     list.Add('echo "-----------------------------------------------------------------"');
     list.Add('echo " " ');
     list.Add('echo "-----------------Запуск java программы---------------------------"');
     list.Add('echo " " ');

     if  DirJDK <> '' then
         list.Add(DirJDK+ st_bin + 'java ' + FileExeName)
     else
         list.Add('java ' + FileExeName);

     list.Add('echo " " ');
     list.Add('echo "---------------Конец работы java программы-----------------------"');
     list.Add('echo " " ');
     list.Add('read p');
     list.SaveToFile(file_run);
     list.Destroy;
end;


end.

