
{ *******************************************************************
  ***                                                             ***
  *** P. WOLFERS SOFTWARE - For MXD Version 4.0.0.0 and next ones ***
  ***                                                             ***
  ***             C N R S - Intitut Neel GRENOBLE                 ***
  ***                                                             ***
  ***             Standard MXD-V4 run set up file                 ***
  ***                                                             ***
  ******************************************************************* }

pragma list_on:'TT:', list_off;
{pragma list_on, show_syntax:0, show_expr:0, show_macro:0, show_macrosrc:0;}

string    $$jbc_name    := SYS_GETENV( 'MXDV4_DEBUG' ),
          $$mxdf_name   := 'TT:',               { Command file Specification }
          $$list_name   := '',                  { Listing File Specification }
          $$save_name   := '',                  { MXD load Context File Specification }
          $$appl_name   := SYS_GETENV( 'MXDV4_APPLICATION' ),
          $$appl_pcdf   := 'mxd_dcp.pcdf',      { Output P-CoDe File application }
          $$exec_cmd    := 'include ''',        { To prepare the user file execution command }
          $$mxd_hlpf    := $sys_mxdlib||'mxdv4.help',
          $$syscmd_name := SET_CASE( $sys_command ); { Get the command name in minor case }

integer   $$ver_flag    :=      0,              { Verify/Debug Flag }
          $$mdb_flag    :=      0,              { Debug Macro Flag }
          $$lis_flag    :=      0,              { Listing flag }
          $$mxd_proc    :=      0,              { Flag for the process }
          $$mxd_setup   :=      1;              { Flag to load the mxd_env.mxd_env (LSQ) environment }

macro $$extfile_check fnam, ext;
  if SUBSTR( fnam, 1, 1 ) <> '/' and INDEX( fnam, '.' ) = 0 then
    fnam := fnam||ext
  end
end;

begin { *** Open a local variable scope *** }
  string              str;
  integer  iprm, ipc, ind;
  integer      ipar :=  0,
               jpar :=  0,
               kpar :=  0;

  var dirtab: array[*] of string := (
                                '-ver',         { To specify the verify/debug mode }
                                '-mdb',         { To activate the macro debuging exp. list }
                                '-out',         { To specify the output listing }
                                '-sav',         { To specify the load saved file }
                                '-app',         { To specify a special application }
                                '-pcd',         { To specify an output pcdf file }
                                '-nmx',         { To flag as no MXd process (=> do not load the MXD environment) }
                                '-hel'          { To specify a "help mxdv4" request }
                          );

  if $$jbc_name = '' then $$jbc_name := $$mxdf_name
                     else
                       ipc := INDEX( $jbc_name, '.' );
                       if ipc <> 0 then $$mxdf_name := $jbc_name;
                                        $$jbc_name  := SUBSTR( $$jbc_name, 1, ipc - 1 )
                                   end
                     end;

  if $sys_nparm > 0 then
    iprm := 1;
    repeat
      str := $sys_parm[iprm];

      if str ='-h' or str = '-H' then str := '-hel' end;

      ind := ARRAY_SEARCH( dirtab, SET_CASE( SUBSTR( str, 1, 4 ) ), 0 );
      case ind
      when 1: { Verify Option }
        ipc := INDEX( str, ':' );
        if ipc > 0 then $$ver_flag := SUBSTR( str, ipc+1 );
                   else $$ver_flag := 1;
                   end;

      when 2: { Mdb Option }
        $$mdb_flag := 1;

      when 3: { Out Option }
        ipc := INDEX( str, ':' );
        if ipc > 0 then
          $$list_name := SUBSTR( str, ipc+1 );
          $$extfile_check $$list_name, '.list';
        end;

      when 4: { -sav command }INDEX( $jbc_name, '.' )
        ipc := INDEX( str, ':' );
        if ipc > 0 then
          $$save_name := SUBSTR( str, ipc+1 );
          $$extfile_check $$save_name, '.save'
        end;

      when 5: { -appl command }
        ipc := INDEX( str, ':' );
        if ipc > 0 then INDEX( $jbc_name, '.' )
          $$appl_name := SUBSTR( str, ipc+1 )
         end;

      when 6: { -pcdf command }
        ipc := INDEX( str, ':' );
        if ipc > 0 then
          $$appl_pcdf := SUBSTR( str, ipc+1 );
          $$extfile_check $$appl_pcdfINDEX( $jbc_name, '.' ), '.pcdf'
        else
          $$appl_pcdf := ''
        end;

      when 7: { - Not MXD use }
        $$mxd_setup := 0;

      when 8: { Help Command }
        if FILE_EXIST( $$mxd_hlpf ) then
          $$mxd_hlpf := FILE_SPC( $$mxd_hlpf );
          ipc := INDEX( str, ':' );
          if ipc > 0 then
            SYS_RUN_PROC( 'cp '||$$mxd_hlpf||' '||SUBSTR( str, ipc+1 ) )
          else
            SYS_RUN_PROC( 'less '||$$mxd_hlpf )
          end;
          display ' *** Cannot find the displayer of help file ***'
        else
          display ' *** Cannot find the help file "', $$mxd_hlpf, '" ***'
        end;
        EXIT( 0 );

      when otherwise { Code to proceed commande }
        case jpar
          when 0: $$jbc_name  := str;
                  ipc := INDEX( str, '.' );
                  if $$mxdf_name <> '' then
                    if ipc = 0 then $$mxdf_name := $$jbc_name||'.mxd'
                               else $$mxdf_name := $$jbc_name;
                                    $$jbc_name := SUBSTR( $$jbc_name, 1, ipc-1 )
                               end
                  end;

          when 1: if $$list_name = '' then $$list_name := str; $$extfile_check $$list_name, '.list'
                                      else ipar := ipar + 1;  $sys_parm[ipar] := str
                                      end;

          when 2: if $$save_name = '' then $$save_nam := str; $$extfile_check  $$save_name, '.save'
                                      else ipar := ipar + 1;  $sys_parm[ipar] := str
                                      end;

        when otherwise
          ipar := ipar + 1; sys_parm[ipar] := str
        end;
        jpar := jpar + 1
      end;
      iprm := iprm + 1
    until iprm > $sys_nparm;
    $sys_nparm := ipar
  end;

  ipc := INDEX( $$syscmd_name, '/', -1 );
  if ipc > 0 then  $$syscmd_name := SUBSTR( $$syscmd_name, ipc + 1 )  end
end; { End Of Local Scope }

if not FILE_EXIST( $$mxdf_name ) then
  display ' Cannot open the file "', $$mxdf_name, '" (probably no such file).';
  display ' *** Stop ON Error. ***';
  exit( 2 )
end;

if $$appl_pcdf <> '' then $$dcp_pcd_file := $$appl_pcdf  end;


$$exec_cmd := $$exec_cmd||$$mxdf_name;

if $$syscmd_name <> 'mxd' or $$list_name <> '' then
  $$lis_flag := 1;
  $$exec_cmd := $$exec_cmd||''', list_on:'''||$$list_name||''''
else
  $$exec_cmd := $$exec_cmd||''''
end;

if $$ver_flag then
  display ' Run File "', $$mxdf_name, '".';
  if $$appl_name <> '' then  display ' Dcp must call application "', $$appl_name, '".'  end;
  display ' Dcp Out file "', $$appl_pcdf, '".'
end;


if $$mdb_flag then
  $$exec_cmd := $$exec_cmd||', show_macro'
end;

{** of Initialize MXD-V4 Sequence **}

if $$mxd_setup then
  if $$lis_flag then  display ' *** Load MXD LSQ environment ***'  end;
  include $sys_mxdlib||'mxd_env.mxd_env'
end;

$job_name := $$jbc_name;

"$$exec_cmd;

if $$appl_name <> '' then
  SYS_RUN_PROC( $$appl_name );
  display ' Error : MXD_DCP cannot execute the command "', $$appl_name, '"'
end;
