#!/bin/sh

PATH=/usr/local/Gambit/bin:$PATH

SHELL_VERSION_COMMANDS="
ksh -c 'echo \$KSH_VERSION'
mksh -c 'echo \$KSH_VERSION'
zsh -c 'echo \$ZSH_VERSION'
dash -c 'echo unknown-possibly-0.5.12'
bash -c 'echo \$BASH_VERSION'
yash -c 'echo \$YASH_VERSION'
"

source_file="$1"
output_file="${source_file%.*}.sh"
temp_file="${source_file%.*}.temp"

echo "*** compiling $source_file -> $output_file"

gcc -E -C -P -DSIX_CC "$source_file" > "$temp_file"

./six-cc.scm "$temp_file" $SIX_CC_OPTIONS > "$output_file"

chmod +x "$output_file"

IFS="
"
for shell_version_command in $SHELL_VERSION_COMMANDS ; do
    shell=`echo "$shell_version_command" | sed -e "s/ .*//"`
    version=`eval $shell_version_command`
    if [ -n "$TEST" ] ; then # If TEST env var is defined, generate result files instead of benchmarking
        echo "Generating $shell result file"
        cat small.c | $shell "$output_file" > "${source_file%.*}_$shell.result"
    elif [ -n "$DEBUG" ] ; then
        $shell "$output_file" < small.c
    else
        echo "`/usr/bin/time $shell "$output_file" < small.c 2>&1 | sed -e 's/ real.*//'` $shell $version"
    fi

done

rm "$temp_file"
