{$A+,B-,D-,F-,G+,I-,K+,L+,N-,P-,Q-,R-,S-,T-,V-,W+,X+,Y-}
{$M 8192,8192}

Program EWExec;

{$r EWEXEC.RES}

uses
objects,
owindows,
odialogs,
omemory,
strings,
winprocs,
wintypes,
windos,
win31,
bwcc,
ostddlgs,
globsup;

const
id_mesg = 201;

type
buf = array[0..127] of char;

pewemain = ^tewemain;
tewemain = object(tapplication)
  procedure initmainwindow; virtual;
end;

pewe = ^tewe;
tewe = object(tdlgwindow)
  shell,
  prog,
  parms,
  temp: buf;

  mesg: pstatic;

  constructor init(aparent: pwindowsobject; atitle: pchar);
  procedure initchildren;
  procedure setupwindow; virtual;

  procedure go;
  procedure doexec(var msg: tmessage); virtual wm_first + wm_timer;
  procedure ok(var msg: tmessage); virtual id_first + id_ok;
end;

constructor tewe.init(aparent: pwindowsobject; atitle: pchar);
  begin
    tdlgwindow.init(aparent, atitle);
    initchildren;

    shell[0]:=#0;
    prog[0]:=#0;
    parms[0]:=#0;
    temp[0]:=#0;
  end;

procedure tewe.initchildren;
  begin
    mesg:=new(pstatic, initresource(@self, id_mesg, 126));
  end;

procedure tewe.setupwindow;
  begin
    tdlgwindow.setupwindow;

    mesg^.settext('One moment...');

    settimer(hwindow, 1, 1500, nil);
  end;

procedure tewe.go;
 var i: integer;
  begin
    if (paramcount = 0) then
      begin
        mesg^.settext('Invalid command line');
        messagebox(hwindow,
                   'EWEXEC progname [paramaters]'#13#10#13#10+
                   'example:'#13#10+
                   'EWEXEC WOLF3D.BAT -next',
                   'Syntax',
                   mb_iconinformation or mb_ok);
        exit;
      end;

    strcopy(shell, getenvvar('COMSPEC'));

    strcopy(parms, ' /C ');

    strpcopy(prog, paramstr(1));
    strupper(prog);

    strcat(parms, prog);
    strcat(parms, #32);

    if (paramcount > 1) then
      for i:=2 to paramcount do
        begin
          strpcopy(temp, paramstr(i));
          strcat(parms, temp);
          strcat(parms, #32);
        end;


    if not exist(prog) then
      begin
        messagebeep(0);
        strcopy(temp, 'Could not find: ');
        strcat(temp, prog);
        mesg^.settext(temp);
        exit;
      end
       else
         begin
           strcopy(temp, shell);
           strcat(temp, parms);
           mesg^.settext(temp);
         end;

    if not exitwindowsexec(shell, parms) then
      begin
        messagebeep(0);
        strcopy(temp, 'Could not execute: ');
        strcat(temp, prog);
        mesg^.settext(temp);
        exit;
      end;

  end;

procedure tewe.doexec(var msg: tmessage);
  begin
    killtimer(hwindow, 1);

    go;
  end;

procedure tewe.ok(var msg: tmessage);
  begin
    killtimer(hwindow, 1);

    go;

    tdlgwindow.ok(msg);
  end;

{ main window }
procedure tewemain.initmainwindow;
  begin
    MainWindow := New(pewe, Init(nil, 'MAINWINDOW'));
  end;

var
ewe: tewemain;

begin
  ewe.init('EWE');
  ewe.run;
  ewe.done;
end.