program cmdsimres3;
uses {$IFDEF UNIX}
  cthreads, {$ENDIF}
  Classes,
  uConstantesSimSEE,
  usimres3,
  uauxiliares,
  (*
  ucoloresbasicos,
  uversiones,
  ufechas,
  uversion_architexto,
  uExcelFile, *)
  SysUtils;


{$R *.res}

  procedure WriteHelp;
  begin
    writeln('Por ayuda visite la web: ' );
    writeln('  https://simsee.org/simsee/verdoc.php?topico=cmdsimres3&idioma=es' );
    writeln('For help visit the web: ' );
    writeln('  https://simsee.org/simsee/verdoc.php?topico=cmdsimres3&idioma=en' );
  end;


  procedure EjecutarLinea(ParamStr: TStringList);
  var
    ArchiDefs: string;
    idSubCarpetaSalida: string;
    idEjecutor: string;
    tmp_base: string;
    salida_sr3: string;
    ipos, k: integer;

    function leopar(kPar: integer): string;
    begin
      Result := ParamStr[kPar - 1];
    end;

    function ParamCount: integer;
    begin
      Result := ParamStr.Count;
    end;

  begin

    writeln('ejecutando línea ..... ');
    for  k := 1 to ParamCount do
      writeln('ParamStr( ', k, ') = ', ParamStr[k - 1]);
    writeln('...........');

    ArchiDefs := LeoPar(1); //archivo con definiciones simres3
    salida_sr3 := 'salida_sr3';
    if paramCount >= 2 then
    begin
      idSubCarpetaSalida := LeoPar(2);
      ipos := pos('#', idSubCarpetaSalida);
      if ipos > 0 then
      begin
        salida_sr3 := copy(idSubCarpetaSalida, ipos + 1, length(
          idSubCarpetaSalida) - ipos);
        Delete(idSubCarpetaSalida, ipos, length(idSubCarpetaSalida) - ipos + 1);
      end;
    end
    else
      idSubCarpetaSalida := '';

    if paramCount >= 3 then
      idEjecutor := LeoPar(3) //identificador del robot del ejecutador
    else
      idEjecutor := '';

    if idEjecutor = '_ninguno_' then
      idEjecutor := '';

    if paramCount >= 4 then
      tmp_base := LeoPar(4) //directorio base
    else
      tmp_base := getDefault_tmp_base;

    tmp_rundir := CrearDirectorioTemporal(tmp_base, idEjecutor, idSubCarpetaSalida);
    chdir(tmp_rundir);
    usimres3.run(ArchiDefs, salida_sr3, False);
  end;

  function leopar(kPar: integer): string;
  var
    s: string;
  begin
    s := ParamStr(kPar);
    if (length(s) >= 2) and (s[1] = '"') and (s[length(s)] = '"') then
    begin
      Delete(s, 1, 1);
      Delete(s, length(s), 1);
    end;
    Result := s;
  end;

var
  archiDefs: string;
  fext: string;
  Parametros: TStringList;
  kPar: integer;
  fls3: textfile;
  r: string;

begin

  writeln('Parámetros cmdsimres3 ..... ');
  for kPar := 1 to ParamCount do
    writeln('kPar[', kPar, ']: ', ParamStr(kPar));
  writeln;

  if Paramcount > 0 then
  begin
    ArchiDefs := LeoPar(1); //archivo con definiciones simres3
    fext := ExtractFileExt(ArchiDefs);
    if fext = '.txt' then
    begin
      Parametros := TStringList.Create;
      for kPar := 1 to ParamCount do
        Parametros.add(LeoPar(kPar));
      EjecutarLinea(Parametros);
      Parametros.Free;
    end
    else if fext = '.ls3' then
    begin
      assignfile(fls3, archiDefs);
      reset(fls3);
      while not EOF(fls3) do
      begin
        readln(fls3, r);
        if trim(r) <> '' then
        begin
          Parametros := getPalabras_lst(r);
          for kPar := 2 to ParamCount do
            Parametros.Add(leoPar(kPar));
          EjecutarLinea(Parametros);
          Parametros.Free;
        end;
      end;
      closefile(fls3);
    end;
  end
  else
    WriteHelp;
end.
