
(********************************************************************)
(*                                                                  *)
(*  tar7.sd7      Tar archiving utility                             *)
(*  Copyright (C) 1994, 2004, 2005, 2010, 2012  Thomas Mertes       *)
(*  Copyright (C) 2013, 2019  Thomas Mertes                         *)
(*                                                                  *)
(*  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 of  *)
(*  the License, 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., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


$ include "seed7_05.s7i";
  include "tar_cmds.s7i";
  include "console.s7i";


const proc: main is func
  local
    var string: option is "";
    var char: command is ' ';
    var string: file_name is "";
    var boolean: do_view is FALSE;
    var boolean: do_zip_unzip is FALSE;
    var boolean: file_arg is FALSE;
    var array string: arg_list is 0 times "";
    var integer: index is 0;
  begin
    OUT := STD_CONSOLE;
    if length(argv(PROGRAM)) >= 1 then
      option := argv(PROGRAM)[1];
      if option[1] = '-' then
        option := option [2 .. ];
      end if;
      if option <> "" then
        command := option[1];
      end if;
      if command in {'t', 'x', 'c'} then
        option := option [2 .. ];
        while option <> "" do
          if option[1] = 'v' then
            do_view := TRUE;
          end if;
          if option[1] = 'z' then
            do_zip_unzip := TRUE;
          end if;
          if option[1] = 'f' then
            file_arg := TRUE;
          end if;
          option := option [2 .. ];
        end while;
        if file_arg then
          if length(argv(PROGRAM)) >= 2 then
            file_name := convDosPath(argv(PROGRAM)[2]);
            arg_list := argv(PROGRAM)[3 .. ];
          else
            writeln("tar7: The option 'f' needs a file name.");
            command := ' ';
          end if;
        else
          writeln("tar7: The option 'f' is missing.");
          command := ' ';
          # file_name := "/dev/flp";
          # arg_list := argv(PROGRAM)[2 .. ];
        end if;
        for key index range arg_list do
          arg_list[index] := convDosPath(arg_list[index]);
        end for;
        case command of
          when {'t'}:
            tarTell(file_name, arg_list, do_view, do_zip_unzip);
          when {'x'}:
            tarXtract(file_name, arg_list, do_view, do_zip_unzip);
          when {'c'}:
            tarCreate(file_name, arg_list, do_view, do_zip_unzip);
        end case;
      else
        write("tar7: Illegal option '");
        write(command);
        writeln("'. Legal options are 't', 'x' or 'c'.");
      end if;
    else
      writeln("Tar7 Version 1.0 - Tar archiving utility");
      writeln("Copyright (C) 1994, 2004, 2005, 2010 Thomas Mertes");
      writeln("This is free software; see the source for copying conditions.  There is NO");
      writeln("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
      writeln("Tar7 is written in the Seed7 programming language");
      writeln("Homepage: http://seed7.sourceforge.net");
      writeln;
      writeln("usage: tar7 command[options] argument");
      writeln;
      writeln("Commands (one of the commands -c, -t or -x must be specified):");
      writeln("  -t   Tell about the contents of an archive");
      writeln("  -x   Extract files from an archive");
      writeln("  -c   Create a new archive");
      writeln("The commands can be altered with the following modifiers:");
      writeln("   v   Verbosely list files processed");
      writeln("   z   Zip an archve with gzip or decompress a gzip, xz, zstd or lzma archive");
      writeln("   f   Use archive file provided as argument");
      writeln;
      writeln("Example of a tar7 usage:");
      writeln("  ./s7 tar7 -tvzf ../../seed7_05_20100221.tgz");
      writeln;
    end if;
  end func;
