#!/bin/sh

mywhich()
{
    which $1|grep -v '^no'
    return $?
}


if tail -n+2 $0 > /dev/null; then
   TAIL="tail -n";
else
   TAIL="tail ";
fi
echo "Using $TAIL"

if which this_should_never_exist_XHSLA12930; then
   echo "Your which command is broken, using workaround!"
   WHICH=mywhich
else
   echo "Your which command works!"
   WHICH=which
fi



if $WHICH gawk; then
    AWK=`which gawk`;
    echo "configure_tools: Found gawk in your search path"
elif $WHICH awk; then
    AWK=`which awk`;
    echo "configure_tools: Cannot find gawk, trying awk (untested)"
else
    echo "configure_tools: No awk available, tools unconfigured."
    echo "configure_tools: Try to get gawk installed in your search path."
    exit 0;
fi

for file in *.awk ; do
    # cp $file $file.bak
    echo "#!$AWK -f" > tmpfile
    $TAIL +2 $file >> tmpfile
    mv tmpfile $file
    chmod ugo+x $file
done


if $WHICH python2.8; then
    PYTHON_DUMMY=`which python2.8`;
    echo "configure_tools: Found Python 2.8 in your search path"
elif $WHICH python2.7; then
    PYTHON_DUMMY=`which python2.7`;
    echo "configure_tools: Found Python 2.7 in your search path"
elif $WHICH python2.6; then
    PYTHON_DUMMY=`which python2.6`;
    echo "configure_tools: Found Python 2.6 in your search path"
elif $WHICH python2.5; then
    PYTHON_DUMMY=`which python2.5`;
    echo "configure_tools: Found Python 2.5 in your search path"
elif $WHICH python2.4; then
    PYTHON_DUMMY=`which python2.4`;
    echo "configure_tools: Found Python 2.4 in your search path"
elif $WHICH python2.3; then
    PYTHON_DUMMY=`which python2.3`;
    echo "configure_tools: Found Python 2.3 in your search path"
elif $WHICH python2.2; then
    PYTHON_DUMMY=`which python2.2`;
    echo "configure_tools: Found Python 2.2 in your search path"
elif $WHICH python2.1; then
    PYTHON_DUMMY=`which python2.1`;
    echo "configure_tools: Found Python 2.1 in your search path"
elif $WHICH python2; then
    PYTHON_DUMMY=`which python2`;
    echo "configure_tools: Found Python 2 in your search path"
elif $WHICH python; then
    PYTHON_DUMMY=`which python`;
    echo "configure_tools: Found Python in your search path (Note: Required version >=2.0, otherwise expect trouble)"
else
    echo "configure_tools: No Python available, tools unconfigured."
    echo "configure_tools: Try to get Python 2.X installed in your search path."
    exit 0;
fi

PYTHON_DUMMY=`basename $PYTHON_DUMMY`

for file in *.py ; do
    # cp $file $file.bak
    echo "#!/usr/bin/env $PYTHON_DUMMY" > tmpfile
    $TAIL +2 $file >> tmpfile
    mv tmpfile $file
    chmod ugo+x $file
done
