############################################################################
##
#W  xnumsgp.gi                       Manuel Delgado <mdelgado@fc.up.pt>
#W                                   Pedro A. Garcia-Sanchez <pedro@ugr.es>
#W                                   Jose Morais <josejoao@fc.up.pt>
##
##
#H  @(#)$Id: xnumsgp.gi,v 0.95 $
##
#Y  Copyright 2005 by Manuel Delgado,
#Y  Pedro Garcia-Sanchez and Jose Joao Morais
#Y  We adopt the copyright regulations of GAP as detailed in the
#Y  copyright notice in the GAP manual.
##
##
###########################################################################
##
#F  XNumericalSemigroup()
##
##  Tcl/Tk interface to specify a numerical semigroup
##
InstallGlobalFunction(XNumericalSemigroup, function(arg)
    local func, d, f, xsemigroupstream, otu, A, args, i, j, s, s2, t, a, q, var, S, gens, dg, l, r,
          list, zero, zind, partial, len, wish,
          rel, leng;

    func := function(ix)
        local i, result, s;

        s := ReadLine(xsemigroupstream);
        while true do
            if s = "quit\n" or s = "quit\r\n" then
                UnInstallCharReadHookFunc( xsemigroupstream, func );
                CloseStream(xsemigroupstream);
                CloseStream(otu);
                break;
            elif s = "end\n" or s = "end\r\n" then
                break;
            else
                Print(s);
                result := READ_COMMAND( InputTextString(s) , true );
                if not result = SuPeRfail then
                    View(result);
                    Print("\n");
                fi;
                PrintTo(otu, "gap> ");
            fi;
            s := ReadLine(xsemigroupstream);
        od;
    end;

    d := DirectoriesPackageLibrary( "numericalsgps", "src" )[1];
    f := "xnumsgp.tcl";
    wish := Filename(DirectoriesSystemPrograms(), "wish");
    if wish = fail then
        Error("Please install Tcl/Tk and make the 'wish' executable reachable by the 'PATH' environment variable");
    fi;
    if arg = [] then
        xsemigroupstream := InputOutputLocalProcess(d,wish,[f]);
    else
        S := arg[1];
        if not IsNumericalSemigroup(S) then
        	Error("The argument must be a numerical semigroup");
        fi;
        if IsNumericalSemigroupByGenerators(S) then
        	gens := GeneratorsOfNumericalSemigroup(S);
        	args := ["1", String(Length(gens))];
        	for i in gens do
        		Add(args, String(i));
        	od;
        elif IsModularNumericalSemigroup(S) then
        	args := ["2", String(S!.modularcondition[1]), String(S!.modularcondition[2])];
        elif IsProportionallyModularNumericalSemigroup(S) then
        	args := ["3", String(S!.proportionallymodularcondition[1]),
        		String(S!.proportionallymodularcondition[2]),
        		String(S!.proportionallymodularcondition[3])];
        else
        	gens := GeneratorsOfNumericalSemigroup(S);
        	args := ["1", String(Length(gens))];
        	for i in gens do
        		Add(args, String(i));
        	od;
        fi;
        xsemigroupstream := InputOutputLocalProcess(d,wish,Concatenation([f], args));
    fi;
    otu := OutputTextUser( );

    InstallCharReadHookFunc( xsemigroupstream, "r", func );
end);
