(* CVT2PCX.PAS -- translate GEOS graphic files
** 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.
*)

PROGRAM cvt2pcx;

USES DOS, errors, global, coding, cvt, paint, photo;

VAR out_name: STRING; level: WORD;
 view, wait_key: BOOLEAN;
 chain, min: ShortInt;
 empty: BOOLEAN;

PROCEDURE usage;
BEGIN
 WriteLn('CVT2PCX  Version 0.3',country,'  Convert GEOS graphic files');
 WriteLn('Copyright (c) 1995,1996 Jochen Metzinger ');
 WriteLn;
 WriteLn(short_usage);
 WriteLn;
 WriteLn('  input     GEOS file (geoPaint, geoWrite, photo scrap/album)');
 WriteLn('  output    PCX output file');
 WriteLn('    /F        create output file');
 WriteLn('    /Q        quiet translation');
 WriteLn('    /1        stop on error');
 WriteLn('    /2        continue on error');
 WriteLn('    /3        black/white on error');
 WriteLn('    /4        interactive correction of errors [default]');
 WriteLn('    /N        don''t wait for key');
 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;
  in_name: STRING;
  force_file: BOOLEAN;
BEGIN
 short_usage := 'CVT2PCX [/?] input [output] [options]';
 in_name := ''; out_name := '';
 level := 4;
 view := TRUE; wait_key := TRUE;
 force_file := 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;
      '1': level := 1;
      '2': level := 2;
      '3': level := 3;
      '4': level := 4;
      'F': force_file := TRUE;
      'Q': view := FALSE;
      'N': wait_key := FALSE;
      'G': SetCoding(ge_coding);
      'E': SetCoding(uk_coding);
      ELSE FATAL('unknown option '+par);
     END; (* case *)
   END
  ELSE IF in_name = '' THEN
   in_name := AddExt(par, CVT_EXT)
  ELSE IF out_name = '' THEN
   out_name := AddExt(par, '.PCX')
  ELSE
   FATAL('too many arguments');
 END; (* for *)
 IF in_name = '' THEN usage;
 IF force_file THEN BEGIN
  IF out_name <> '' THEN FATAL('too many arguments');
  out_name := GetFileName(in_name) + '.PCX';
 END; (* if *)
 cvt_open(in_name);
 short_usage := '';
END; (* init *)

FUNCTION ChainPCX(filename: STRING; chain: ShortInt): STRING;
(* return name for chain *)
 VAR D: DirStr; N: NameStr; E: ExtStr;
BEGIN
 IF filename = '' THEN
  ChainPCX := ''
 ELSE BEGIN
  FSplit(filename, D, N, E);
  E := Copy(E,2,1);
  IF E = '' THEN E := 'P';
  filename := D + N + '.' + E + HexStr(chain,2);
  ChainPCX := filename;
 END; (* else *)
END; (* ChainPCX *)


BEGIN
 Init;
 IF IsPaint THEN
  DoPaint(out_name, view, wait_key, level)
 ELSE IF IsPhotoScrap THEN
  BEGIN
   OpenScrap(view, wait_key, level);
   IF NOT view THEN Write(cvt_name,' ');
   DoScrap(0, out_name);
   IF NOT view THEN Write('.');
   CloseScrap;
   IF NOT view THEN WriteLn;
  END
 ELSE
  BEGIN
   IF IsPhotoAlbum THEN
    min := 0
   ELSE IF IsWrite THEN
    min := 64
   ELSE
    FATAL('not a graphic file');
   empty := TRUE;
   FOR chain := min TO 126 DO
    IF cvt_size(chain) >= 0 THEN
     empty := FALSE;
   IF empty THEN
    message('no photo scrapts in "'+cvt_name+'"')
   ELSE BEGIN
    IF NOT view THEN Write(cvt_name,' ');
    OpenScrap(view, wait_key, level);
    FOR chain := min TO 126 DO
     IF cvt_size(chain) >= 0 THEN BEGIN
      IF NOT view THEN Write('.');
      DoScrap(chain, ChainPCX(out_name,chain));
     END; (* if *)
    CloseScrap;
    IF NOT view THEN WriteLn;
   END; (* if *)
  END; (* else *)
 cvt_close;
END. (* cvt2pcx *)
