#!/bin/sh

# RunSwig.sh
#
# Runs the SWIG program to construct source code out of a SWIG
# interface file (ultimately for Python).
#
# This should be invoked from Xcode, or the environment should
# otherwise provide what Xcode does for build rules: in
# particular, variables like ${INPUT_FILE_BASE}.  For more
# information, see "Xcode Build System Guide: Build Rules".
#
# This script exists to act as an interface to Xcode build rules,
# and to correct some stupid things that SWIG does.
#
# Kevin Grant (kmg@mac.com)
# December 13, 2007

awk=/usr/bin/awk
cat=/bin/cat
grep=/usr/bin/grep
mv=/bin/mv
printf=/usr/bin/printf
rm=/bin/rm
swig=${SWIG_PREFIX}/bin/swig
if [ "x${SWIG}" != "x" ] ; then
    # explicit path overrides default search
    swig=${SWIG}
else
    # to make it easier to rely on, say, Homebrew,
    # fall back to an alternate prefix by default
    if [ ! -x "${swig}" ] ; then
        echo "$0: warning: not found or not executable: '${swig}'" >&2
        swig=${SWIG_PREFIX_ALT}/bin/swig
    fi
fi
touch=/usr/bin/touch

input_swig_i=${INPUT_FILE_NAME}
input_swig_i_path=${INPUT_FILE_DIR}/${INPUT_FILE_NAME}
generated_cxx=${INPUT_FILE_BASE}_wrap.cxx
generated_py=${INPUT_FILE_BASE}.py
cxx_tmp=/tmp/${INPUT_FILE_BASE}_wrap.cxx.NEW.$$

if [ "x${DERIVED_FILES_DIR}" = "x" ] ; then
    echo "$0: run this only from Xcode" >&2
    exit 1
fi

# test that swig is runnable, with a suitable version
if [ ! -x "${swig}" ] ; then
    echo "$0: not found or not executable: '${swig}'" >&2
    exit 1
fi
swig_ver_output=`${swig} -version`
if [ "x$?" != "x0" ] ; then
    echo "$0: failed to run 'swig' from '$swig' (if you have Homebrew, try 'brew install swig')" >&2
    exit 1
else
    echo "$0: configured to run '${swig}'" >&2
fi
actual_swig_ver=`echo ${swig_ver_output} | $grep "SWIG Version" | $awk '{print $3}'`
if [ "x$actual_swig_ver" != "x${RECOMMENDED_SWIG_VERSION}" ] ; then
    echo "$0: warning: this project was last tested with SWIG ${RECOMMENDED_SWIG_VERSION} but installed version ($swig) is $actual_swig_ver" >&2
    #exit 1
else
    echo "$0: (this matches the recommended SWIG version, ${RECOMMENDED_SWIG_VERSION})" >&2
fi

# change to the target directory, because SWIG annoyingly
# generates the *.cxx file in the current directory even if
# options like -outdir are given to redirect other files
cd "${DERIVED_FILES_DIR}"
if [ "x$?" != "x0" ] ; then
    echo "$0: unable to change to ${DERIVED_FILES_DIR}" >&2
    exit 1
fi

# Xcode does not seem to know when NOT to call this script
if [ "${input_swig_i_path}" -ot "${generated_cxx}" ] ; then
    if [ "${input_swig_i_path}" -ot "${generated_py}" ] ; then
        echo "$0: '${input_swig_i}' has not changed since its derived files were last built" >&2
        exit 0
    fi
fi

# throw away any previous files
$rm -f \
${generated_cxx} \
${cxx_tmp} \
;

# construct the Python code and C++ wrapper code
${swig} ${1+"$@"}
if [ "x$?" != "x0" ] ; then
    echo "$0: SWIG failed" >&2
    exit 1
fi

#
# now patch SWIG output, to fix certain problems
#

$rm -f ${cxx_tmp}
$touch ${cxx_tmp}
if [ "x$?" != "x0" ] ; then
    echo "$0: unable to create temporary file" >&2
    exit 1
fi

# Apple's AvailabilityMacros.h file defines a stupidly-named
# macro, check(), that clobbers a method of the same name in
# output generated by SWIG; so, throw this macro away
#$printf "#ifdef check\n#undef check\n#endif\n" >> ${cxx_tmp}

# at last, include the actual source from SWIG!
$cat ${generated_cxx} >> ${cxx_tmp}
if [ "x$?" != "x0" ] ; then
    echo "$0: no generated C++ file found" >&2
    exit 1
fi

$mv -f ${cxx_tmp} ${generated_cxx}

