#!/bin/sh

##
## We attempt to locate a working m4
##
M4=m4

IFS="${IFS= 	}"; save_ifs="$IFS"; IFS="${IFS}:"
HAVEGNU=no
HAVEWORKS=no

# GNU m4 is named gm4 on FreeBSD
for m4 in gm4 m4
do
  for d in $PATH; do
    test -z "$d" && ac_dir=.

    if test -f $d/$m4; then
      if echo 'divert(-1)changequote([[,]])define(DOIT,ifelse([foo],foo,[DOIT($1)]))DOIT([[m4exit(1)]])' | $d/$m4 >/dev/null; then
        works=yes
      else
        works=no
      fi

      case `echo __gnu__ | $d/$m4` in
        __gnu__) isgnu=no ;;
        *) isgnu=yes ;;
      esac

      case "$HAVEGNU-$isgnu:$HAVEWORKS-$works" in
        *:no-yes|no-yes:*)
          M4=$d/$m4
          HAVEGNU=$isgnu
          HAVEWORKS=$works
        ;;
      esac
    fi
  done
done

IFS="$save_ifs"
(
cat <<\EOF
divert(-1)

dnl generates a script that is supposed to test Pike
dnl note that not everything is tested yet, you are welcome to
dnl submit more tests.

changequote([[,]])

define(DOIT,ifelse([foo],foo,[DOIT($1)]))dnl
DOIT([[errprint([This m4 cannot handle quote strings with more than one character
Please consider installing GNU m4.])
m4exit(1)
]])dnl

changecom

define(SEPARATOR,[[....]])
define(CONDITION,[[]])

define(DOTEST,[[dnl
divert(0)dnl
define([[TESTNO]],incr(TESTNO))dnl
CONDITION()test TESTNO, expected result: [[$1]]
[[$2]]
SEPARATOR
divert(-1)dnl]])

define(test_any, [[DOTEST(EQ,dnl
mixed a() { [[$1]]; }
mixed b() { return [[$2]]; })]])

define(test_any_equal, [[DOTEST(EQUAL,dnl
mixed a() { [[$1]]; }
mixed b() { return [[$2]]; })]])

define(test_eq,[[DOTEST(EQ,dnl
mixed a() { return [[$1]]; }
mixed b() { return [[$2]]; })]])

define(test_equal,[[DOTEST(EQUAL,dnl
mixed a() { return [[$1]]; }
mixed b() { return [[$2]]; })]])

define(test_do,[[DOTEST(RUN,dnl
mixed a() { [[$1]]; })]])

define(test_true, [[DOTEST(TRUE,dnl
mixed a() { return [[$1]]; })]])

define(test_false, [[DOTEST(FALSE,dnl
mixed a() { return [[$1]]; })]])

define(test_compile,[[DOTEST(COMPILE,dnl
void x() { [[$1]] ; } )]])

define(test_compile_any,[[DOTEST(COMPILE,dnl
[[$1]] )]])

define(test_compile_error,[[DOTEST(COMPILE_ERROR,dnl
mixed a() { [[$1]]; })]])

define(test_compile_error_low,[[DOTEST(COMPILE_ERROR,dnl
[[$1]])]])

define(test_eval_error,[[DOTEST(EVAL_ERROR,dnl
mixed a() { [[$1]]; })]])

define(test_define_program,[[DOTEST(RUN,dnl
void a() { add_constant("[[$1]]",class c {
  [[$2]]
});})]])

define(test_program, [[DOTEST(TRUE,dnl
[[$1]])]])

define(cond,[[
define([[CONDITION]],[[COND $1
]])
$2
define([[CONDITION]],[[]])
]])
define(ifefun,[[cond([[all_constants()->$1]],[[$2]])]])

define(TESTNO,0)

// testing < > <= >=
define([[test_cmp]],[[
test_true($1<$2)
test_false($1>$2)
test_true($2>$1)
test_false($2<$1)
test_true($1<=$2)
test_false($1>=$2)
test_true($2>=$1)
test_false($2<=$1)
test_true($2<=$2)
test_true($1>=$1) ]])

define([[test_cmp3]],[[
test_cmp($1,$2)
test_cmp($2,$3)
test_cmp($1,$3)

 test_true(`<($1,$2,$3))
test_false(!`<($1,$2,$3))
test_false(`<($1,$3,$2))
 test_true(!`<($1,$3,$2))
test_false(`<($2,$3,$1))
test_false(`<($2,$1,$3))
test_false(`<($3,$1,$2))
test_false(`<($3,$2,$1))
test_false(`<($3,$2,$2,$1))

 test_true(`<=($1,$2,$3))
test_false(!`<=($1,$2,$3))
test_false(`<=($1,$3,$2))
test_false(`<=($2,$3,$1))
test_false(`<=($2,$1,$3))
 test_true(!`<=($2,$1,$3))
test_false(`<=($3,$1,$2))
test_false(`<=($3,$2,$1))
 test_true(`<=($1,$1,$2,$2,$3,$3))

test_false(`>($1,$2,$3))
test_false(`>($1,$3,$2))
test_false(`>($2,$3,$1))
test_false(`>($2,$1,$3))
test_false(`>($3,$1,$2))
 test_true(`>($3,$2,$1))
test_false(`>($3,$2,$2,$1))

test_false(`>=($1,$2,$3))
test_false(`>=($1,$3,$2))
test_false(`>=($2,$3,$1))
test_false(`>=($2,$1,$3))
test_false(`>=($3,$1,$2))
 test_true(!`>=($3,$1,$2))
 test_true(`>=($3,$2,$1))
 test_true(`>=($3,$3,$2,$2,$1,$1))
test_false(!`>=($3,$3,$2,$2,$1,$1))

test_equal(min($2,$1,$3), $1)
test_equal(max($2,$1,$3), $3)
 ]])

EOF

cat $1
) | (shift ; $M4 "$@" - )

