#!/usr/bin/perl
# Copyright (C) 1997 Ullrich Hustadt
#                    Max-Planck-Institut fuer Informatik
#                    Im Stadtwald
#                    66123 Saarbruecken, Germany
#
# prepare-flotter 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 1, or (at your option)
# any later version.
#
# prepare-flotter 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.

$file = shift(@ARGV);
system("cp $file /tmp");
$selection = 1;
$tmp_file = "/tmp/spass.$$";
$no_flags = 1;
open(OUT,">$tmp_file");
select(OUT);
open(IN,$file);
while (<IN>) {
  if ((/end\_problem/) &&
      $no_flags) {
    print<<"EndOfFlags"
list_of_settings(SPASS).
set_flag(TimeLimit,2200).
set_flag(Memory,100000).
set_flag(PProblem,0).
set_flag(PKept,0).
set_flag(PGiven,0).
set_flag(PEmptyClause,0).
set_flag(DocProof,0).
set_flag(Splits,-1).
end_of_list.

EndOfFlags

} elsif (/set_flag/) {
  s/set_flag\(TimeLimit,[^\)]*\)/set_flag(TimeLimit,1000)/;
  s/set_flag\(Memory,[^\)]*\)/set_flag(Memory,100000)/;
  s/set_flag\(DocProof,[^\)]*\)/set_flag(DocProof,0)/;
  s/set_flag\(FPModel,[^\)]*\)/set_flag(FPModel,0)/;
  s/set_flag\(PProblem,[^\)]*\)/set_flag(PProblem,0)/;
  s/set_flag\(PKept,[^\)]*\)/set_flag(PKept,0)/;
  s/set_flag\(PGiven,[^\)]*\)/set_flag(PGiven,0)/;
  s/set_flag\(SelAll,[^\)]*\)/set_flag(SelAll,$selection)/;
  s/set_flag\(PSortDel,[^\)]*\)/set_flag(PSortDel,0)/;
  s/set_flag\(Splits,[^\)]*\)/set_flag(Splits,-1)/;
  s/set_flag\(PEmptyClause,[^\)]*\)/set_flag(PEmptyClause,0)/;
  $no_flags = 0;
  };
  s/\(false\_\d+\s*\,\s*\d+\s*\)//;
  s/\(true\_\d+\s*\,\s*\d+\s*\)//;
  s/^.*or.*not\(false.*$//;
  s/^.*or.*\,true.*$//;
  s/^.*or\(true.*$//;
  s/not\(true[^\(]*\([^\)]+\)\)//g;
  s/\,false[^\(]*\([^\)]+\)//g;
  s/\(false[^\(]*\([^\)]+\)\,/\(/g;
  s/\(false[^\(]*\([^\)]+\)/\(/g;
# The following two lines are only for complexity checks
#  s/^.*clause.*[\(\,]a\d+.*$//g;
#  s/^.*clause.*forall\(\[[^\]].*//g;
  if (/clause/) {
    $number = 0;
#    print(STDOUT "CLS: $_");
    while (/\(/) {
      s/\(([^\(\)]+)\)/\|$number\+$1\|$number\-/;
      $number++;
#      print(STDOUT "CL1: $_");
    };
    s/not\(SkC/not-SkE/g;
    s/(SkC\d+)/not($1)/g;
    s/not-SkE(\d+)\)/SkC$1/g;
    while (/not\|(\d+)\+SkP/) {
      s/not\|(\d+)\+SkP/not-$1-SkE/;
    };
    s/(SkP\d+)\|(\d+)\+(.*)\|\2\-/not($1($3))/g;
#    print(STDOUT "CL2: $_");
    s/not-(\d+)-SkE(\d+.*)\|\1\-/SkP$2/g;
#    print(STDOUT "CL3: $_");
    s/\|\d+\+/\(/g;
    s/\|\d+\-/\)/g;
#    print(STDOUT "CLF: $_");
  };
  s/functions\[/functions\[\(app\,2\)\,/;
  s/\(app\,2\)\]/\]/;
  s/\,\s*\,+/\,/g;
  print if (/[^\s\n]/);
}
close(IN);
close(OUT);
system("mv $tmp_file $file");

