(********************************************************************)
(*                                                                  *)
(*  make7.sd7     Make utility to manage the compilation process    *)
(*  Copyright (C) 2010 - 2014  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 "make.s7i";


const type: optionHash is hash [string] string;

const proc: main is func
  local
    var integer: number is 0;
    var string: curr_arg is "";
    var optionHash: make_option is optionHash.value;
    var string: makefile is "";
    var stringHash: macros is stringHash.value;
    var integer: equalPos is 0;
    var array string: targets is 0 times "";
    var makeFlags: flags is makeFlags.value;
    var boolean: okay is TRUE;
  begin
    number := 1;
    while number <= length(argv(PROGRAM)) do
      curr_arg := argv(PROGRAM)[number];
      if length(curr_arg) >= 2 and curr_arg[1] = '-' then
        if curr_arg = "-C" and number < length(argv(PROGRAM)) then
          incr(number);
          chdir(convDosPath(argv(PROGRAM)[number]));
        elsif curr_arg = "-f" and number < length(argv(PROGRAM)) then
          incr(number);
          make_option @:= [curr_arg[.. 2]] argv(PROGRAM)[number];
        else
          make_option @:= [curr_arg[.. 2]] curr_arg[3 ..];
        end if;
      elsif pos(curr_arg, '=') <> 0 then
        equalPos := pos(curr_arg, '=');
        macros @:= [curr_arg[.. pred(equalPos)]] curr_arg[succ(equalPos) ..];
      else
        targets &:= curr_arg;
      end if;
      incr(number);
    end while;
    if "-v" in make_option or "-h" in make_option or "-?" in make_option then
      writeln("Make7 Version 1.0 - Make utility to manage the compilation process");
      writeln("Copyright (C) 2010 - 2014 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("Make7 is written in the Seed7 programming language");
      writeln("Homepage: http://seed7.sourceforge.net");
      writeln;
      writeln("usage: make7 [options] [targets]");
      writeln;
      writeln("Options:");
      writeln("  -C dir");
      writeln("     Change to dir before reading the makefile or doing anything else.");
      writeln("  -f file");
      writeln("     Use file as a makefile.");
      writeln("  -i");
      writeln("     Ignore all errors in commands executed.");
      writeln("  -n");
      writeln("     Print the commands that would be executed, but do not execute them.");
      writeln("  -s");
      writeln("     Silent operation. Do not print the commands as they are executed.");
      writeln;
    else
      if "-f" in make_option then
        makefile := convDosPath(make_option["-f"]);
      elsif fileType("makefile") = FILE_REGULAR then
        makefile := "makefile";
      elsif fileType("Makefile") = FILE_REGULAR then
        makefile := "Makefile";
      else
        okay := FALSE;
        if length(targets) = 0 then
          writeln(" *** Target missing and no makefile found.");
        else
          writeln(" *** No makefile found.");
        end if;
        writeln("Use  make7 -?  to get information about make7.");
      end if;
      if "-i" in make_option then
        incl(flags, ignoreErrors);
      end if;
      if "-n" in make_option then
        incl(flags, dontExecute);
      end if;
      if "-s" in make_option then
        incl(flags, silentMode);
      end if;
      if okay then
        block
          make(makefile, targets, flags, macros);
        exception
          catch FILE_ERROR:
            writeln(" *** Make7 terminated.");
        end block;
      end if;
    end if;
  end func;