#! /bin/csh -f

# 1 = mex script filepath
# 2 = mex include path
# 3 = script path (containing this file)
# 4 = setup script filepath
# 5 = input filepath
# 6 = option
# 7 = intermediate filepath
# 8 = output filepath

# Thanks to James Malcolm for the "nvmex" command and files.

source $4

if ($CUDA != 1) exit 0

if (! -e $5) then
    echo cannot find $5
    exit 1
endif

if ($6 == compile) then

    $3/nvmex -f $3/nvopts.sh -output $8 $5 $CUDA_LINK_LIB

    if (-e $8) then
        echo CUDA compilation successful
        exit 0
    else
        echo 'CUDA compilation [nvmex] failed'
        exit 1
    endif

else

    if ($6 == preprocess) then
        nvcc -E -I $2 -use_fast_math -o $7 $5
    else if ($6 == info) then
        nvcc -cubin -I $2 -use_fast_math -o $7 $5
    endif

    if (-e $7) then
        echo CUDA preprocessing successful
    else
        echo 'CUDA preprocessing [nvcc] failed'
        exit 1
    endif

endif

