#!/bin/sh

dir="$1"; shift
base="$1"; shift
alternatives="$1"; shift

try=1

(
  echo 'int main()'
  echo '{'
  echo '  return 0;'
  echo '}'
) > "$dir/alternatives-$base.c"
scripts/cdcompile "$dir" -fvisibility=hidden -c "alternatives-$base.c"

while [ "$try" -le "$alternatives" ]
do
  basetry=${base}_$try
  echo "trying $dir $basetry ..."

  echo ' ' scripts/cdcompile "$dir" -fvisibility=hidden -I ../include -D "$basetry" -c "$base.c" \
  && scripts/cdcompile "$dir" -fvisibility=hidden -I ../include -D "$basetry" -c "$base.c" \
  && echo ' ' scripts/cdcompile "$dir" -fvisibility=hidden -o "alternatives-$base" "alternatives-$base.o" "$base.o" "$@" \
  && scripts/cdcompile "$dir" -fvisibility=hidden -o "alternatives-$base" "alternatives-$base.o" "$base.o" "$@" \
  && echo "success $dir $basetry" \
  && break

  rm -f "$dir/$base.o"

  try=`expr "$try" + 1`
done
