(* CVT2XXX.PAS -- meta-converter for pcGeos
** Copyright (c) 1995,1996 Jochen Metzinger
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2, or (at your option)
** any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*)

(*$M 8192,0,0 <-- have to be able to execute programs *)
PROGRAM cvt2xxx;

USES DOS, global, geos, coding, errors, jobs, batch;

VAR
 file_name, dir_name: STRING;
 verbose, do_batch: BOOLEAN;
 dir_search: SearchRec;
 info: InfoRec;
 job: STRING;

PROCEDURE usage;
BEGIN
 WriteLn('CVT2XXX  Version 0.3',country,'  Convert GEOS files');
 WriteLn('Copyright (c) 1995,1996 Jochen Metzinger ');
 WriteLn;
 WriteLn(short_usage);
 WriteLn;
 WriteLn('  filename    convert this file(s)');
 WriteLn('  batch       create coverting .BAT file');
 WriteLn('    /V          verbose running commands');
 Write('    /G          german');
 IF default_code = ge_coding THEN Write(' [default]');
 WriteLn;
 Write('    /E          english');
 IF default_code = uk_coding THEN Write(' [default]');
 WriteLn;
 HALT(1);
END; (* usage *)

PROCEDURE Init;
 VAR i, j: WORD; par: STRING;
  bat_name: STRING;
  D: DirStr; N: NameStr; E: ExtStr;
BEGIN
 short_usage := 'CVT2XXX [/?] filename [batch] [options]';
 file_name := ''; bat_name := ''; verbose := FALSE;
 IF ParamCount = 0 THEN
  usage;
 FOR i := 1 TO ParamCount DO BEGIN
  par := ParamStr(i);
  IF (par[1] = '/') OR (par[1] = '-') THEN
   BEGIN
    IF Length(par) = 1 THEN
     FATAL('unknown option '+par);
    FOR j := 2 TO Length(par) DO
     CASE UpCase(par[j]) OF
      '?', 'H': usage;
      'V': verbose := TRUE;
      'G': SetCoding(ge_coding);
      'E': SetCoding(uk_coding);
      ELSE FATAL('unknown option '+par);
     END; (* case *)
   END
  ELSE IF file_name = '' THEN
   file_name := AddExt(par, '.CVT')
  ELSE IF bat_name = '' THEN
   bat_name := AddExt(par, '.BAT')
  ELSE
   FATAL('too many arguments');
 END; (* for *)
 IF file_name = '' THEN usage;
 job_init;
 FSplit(file_name,D,N,E);
 dir_name := D; file_name := N+E;
 do_batch := (bat_name <> '');
 IF do_batch THEN BEGIN
  bat_open(bat_name);
  IF NOT verbose THEN
   bat_line('@ECHO=OFF');
 END; (* if *)
 short_usage := '';
END; (* init *)

FUNCTION get_info(file_name: STRING; VAR info: InfoRec): BOOLEAN;
 VAR cvt_file: FILE; result: WORD; magic: STRING;
BEGIN
 get_info := FALSE;
 (* open file *)
 (*$I-*)
 Assign(cvt_file, file_name);
 FileMode := FileMode_RO;
 Reset(cvt_file,1);
 FileMode := FileMode_RW;
 IF IOResult <> 0 THEN BEGIN
  message('unable to open file ');
  EXIT;
 END; (* if *)
 (*$I+*)
 (* handle .cvt header *)
 BlockRead(cvt_file,info,SizeOf(info),result);
 IF result <> SizeOf(info) THEN BEGIN
  Close(cvt_file);
  message('not a formatted GEOS file V1.0');
  EXIT;
 END; (* if *)
 magic := GetString(info.b[32], 28);
 IF (magic <> 'SEQ formatted GEOS file V1.0') AND
    (magic <> 'PRG formatted GEOS file V1.0') THEN BEGIN
  Close(cvt_file);
  message('not a formatted GEOS file V1.0');
  EXIT;
 END; (* if *)
 (* handle info record *)
 BlockRead(cvt_file, info, SizeOf(info), result);
 Close(cvt_file);
 IF result <> SizeOf(info) THEN BEGIN
  message('file too short');
  EXIT;
 END; (* if *)
 get_info := TRUE;
END; (* get_info *)

PROCEDURE create_batch(job: STRING);
 VAR next: LongInt; use_next, use_end: BOOLEAN;
BEGIN
 IF NOT verbose THEN
  bat_line('ECHO '+file_name+':');
 next := 1;
 use_end := FALSE;
 REPEAT
  use_next := FALSE;
  REPEAT
   bat_line(job);
   job := job_next(0);
   IF job <> '' THEN BEGIN
    bat_line('IF ERRORLEVEL 1 GOTO '+bat_nr+'-'+long2str(next,0));
    use_next := TRUE;
   END; (* if *)
  UNTIL job = '';
  job := job_next(1);
  IF job <> '' THEN BEGIN
   bat_line('IF NOT ERRORLEVEL 1 GOTO end'+bat_nr);
   use_end := TRUE;
  END; (* if *)
  IF use_next THEN BEGIN
   bat_line(':'+bat_nr+'-'+long2str(next,0));
   Inc(next);
  END; (* if *)
 UNTIL job = '';
 IF use_end THEN
  bat_line(':end'+bat_nr);
 bat_inc;
END; (* create_batch *)

PROCEDURE execute(job: STRING);
BEGIN
 REPEAT
  IF verbose THEN message(job);
  SwapVectors;
  Exec(GetEnv('COMSPEC'), '/C '+job);
  SwapVectors;
  IF DosError <> 0 THEN
   FATAL('dos error #'+long2str(DosError,0));
  job := job_next(DosExitCode);
 UNTIL job = '';
END; (* execute *)


BEGIN
 Init;
 FindFirst(dir_name+file_name, Hidden OR SysFile, dir_search);
 IF DosError <> 0 THEN
  FATAL('no file found');
 WHILE DosError = 0 DO BEGIN
  file_name := dir_name+dir_search.Name;
  message('*** '+file_name+' ***');
  IF get_info(file_name, info) THEN BEGIN
   job := job_first(info, file_name);
   IF job = '' THEN
    message('no converter for '+file_name+'defined')
   ELSE IF do_batch THEN
    create_batch(job)
   ELSE
    execute(job);
  END; (* if *)
  FindNext(dir_search);
 END; (* while *)
 IF do_batch THEN
  bat_close;
END. (* cvt2xxx *)
